/*********************** 下載 jar 後, javac -encoding utf-8 -cp jSerialComm-1.3.11.jar;. jRxTx.java java -cp jSerialComm-1.3.11.jar;. jRxTx ***********************/ import com.fazecast.jSerialComm.*; import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; public class jRxTx extends JFrame implements ActionListener { Container c; JButton bot; static JTextArea tea_in,tea_out; JScrollPane jsp_in,jsp_out; static String outs=""; Timer t; public jRxTx() { super("jRxTx 1.5"); c=getContentPane(); c.setLayout(new FlowLayout()); tea_in=new JTextArea(10,40); tea_out=new JTextArea(10,40); jsp_in=new JScrollPane(tea_in); jsp_out=new JScrollPane(tea_out); bot=new JButton("傳送"); c.add(new JLabel("輸出")); c.add(jsp_out); c.add(bot); c.add(new JLabel("輸入")); c.add(jsp_in); bot.addActionListener(this); setSize(640,480); setVisible(true); } public void paint(Graphics g) { super.paint(g); } public void actionPerformed(ActionEvent e){ if (e.getSource()==bot){ outs=tea_out.getText(); tea_out.setText(""); } } void connect( String portName ) throws Exception { SerialPort ubxPort = SerialPort.getCommPort(portName); boolean openedSuccessfully = ubxPort.openPort(); if (!openedSuccessfully) { System.out.println(portName+ "Opening fail!"); return; } ubxPort.setBaudRate(115200); ubxPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0); //ubxPort.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 1000, 0); //ubxPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 100, 0); InputStream in = ubxPort.getInputStream(); OutputStream out = ubxPort.getOutputStream(); ( new Thread( new SerialReader( in ) ) ).start(); ( new Thread( new SerialWriter( out ) ) ).start(); } public static class SerialReader implements Runnable { InputStream in; public SerialReader( InputStream in ) { this.in = in; } public void run() { byte[] buffer = new byte[ 1024 ]; byte[] readBuffer = new byte[ 1024 ]; int readBufferPosition=0; int len = -1; byte delimiter=10; //where to stop try { while( ( len = this.in.read( buffer ) ) > -1 ) { for(int i=0;i