下载地址:http://fazecast.github.io/jSerialComm/
import com.fazecast.jSerialComm.*; public class Test { public static void main(String[] args) { //System.out.println("Hello World!"); SerialPort mSerialPort; mSerialPort = SerialPort.getCommPort("COM3"); mSerialPort.openPort(); mSerialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED); mSerialPort.setComPortParameters(9600, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY); mSerialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING | SerialPort.TIMEOUT_WRITE_BLOCKING, 1000, 1000); byte[] data = {(byte)0x01,(byte)0x02,(byte)0x03}; mSerialPort.writeBytes(data, data.length); byte[] info = new byte[100]; try { Thread.sleep(3000); } catch(Exception e) {} int len = mSerialPort.readBytes(info, info.length); System.out.println(len); for (int i = 0; i < len; i++) { System.out.format("%x ", info[i]); } mSerialPort.closePort(); //更多用法 查看 Fazecast-jSerialComm-87413a4\src\test\java\com\fazecast\jSerialComm\SerialPortTest.java } }
#编译 javac -cp jSerialComm-2.9.2.jar; Test.java #运行 java -cp jSerialComm-2.9.2.jar; Test #说明 javac -cp 指明了.java文件里import的类的位置 Windows系统下多个jar用分号";"分隔 Linux系统下多个jar用分号":"分隔