api文档
http://fazecast.github.io/jSerialComm/javadoc/com/fazecast/jSerialComm/package-summary.html
maven依赖
1<!-- https://mvnrepository.com/artifact/com.fazecast/jSerialComm --> 2<dependency> 3 <groupId>com.fazecast</groupId> <artifactId>jSerialComm</artifactId> <!--<version>2.5.2</version>--> <version>[2.0.0,3.0.0)</version> </dependency>
基于事件的阅读用法示例
可供读取的数据示例
以下示例显示了使用此事件触发器的一种方法:
1 SerialPort comPort = SerialPort.getCommPorts()[0]; 2 comPort.openPort(); 3 comPort.addDataListener(new SerialPortDataListener() { 4 @Override 5 public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; } 6 @Override 7 public void serialEvent(SerialPortEvent event) 8 { 9 if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) 10 return; 11 byte[] newData = new byte[comPort.bytesAvailable()]; 12 int numRead = comPort.readBytes(newData, newData.length); 13 System.out.println("Read " + numRead + " bytes."); 14 } 15 16 });
非阻塞阅读用法示例
1package com.bhu.utils; 2 3import com.fazecast.jSerialComm.SerialPort; 4 5 6public class SerialComm { 7 8 /*public static void main(String[] args) throws InterruptedException { 9 SerialComm serialComm = new SerialComm("COM3"); 10 serialComm.openPort(); 11 while (true) { 12 byte[] bs = serialComm.writeAndRead("010300000002"); 13 Double[] d = Utils.analysisData(bs, 3, 5, 2); 14 if (d != null) { 15 for (Double a : d) { 16 System.out.println(a); 17 } 18 } 19 20 Thread.sleep(1000); 21 } 22 }*/ 23 24 25 public SerialComm(String portDescriptor) { 26 this.portDescriptor = portDescriptor; 27 } 28 29 public SerialComm(String portDescriptor, Integer baudRate) { 30 this.portDescriptor = portDescriptor; 31 this.baudRate = baudRate; 32 } 33 34 private String portDescriptor; 35 private Integer baudRate; 36 37 private SerialPort comPort; 38 39 public boolean isOpen() { 40 return comPort.isOpen(); 41 } 42 43 /** 44 * 打开串口 45 * 46 * @return 打开的状态 47 */ 48 public boolean openPort() { 49 return openPort(portDescriptor, baudRate); 50 } 51 52 /** 53 * 打开串口 54 * 55 * @param portDescriptor COM口 56 * @return 打开的状态 57 */ 58 public boolean openPort(String portDescriptor) { 59 return openPort(portDescriptor, null); 60 } 61 62 /** 63 * 打开串口 64 * 65 * @param portDescriptor COM口 66 * @param baudRate 波特率 67 * @return 打开的状态 68 */ 69 public boolean openPort(String portDescriptor, Integer baudRate) { 70 comPort = SerialPort.getCommPort(portDescriptor); 71 if (baudRate != null) { 72 comPort.setBaudRate(baudRate); 73 } 74 if (!comPort.isOpen()) { 75 return comPort.openPort(); 76 } else { 77 return true; 78 } 79 } 80 81 82 /** 83 * 关闭串口 84 * 85 * @return 86 */ 87 public boolean closePort() { 88 if (comPort != null && comPort.isOpen()) { 89 return comPort.closePort(); 90 } 91 return true; 92 } 93 94 /** 95 * 向com口发送数据并且读取数据 96 */ 97 public byte[] writeAndRead(String instruct) { 98 byte[] reslutData = null; 99 try { 100 if (!comPort.isOpen()) throw new BhudyException(BhudyExceptionCode.CODE_18); 101 102 int numWrite = comPort.writeBytes(Utils.getCRC(instruct), Utils.getCRC(instruct).length); 103 if (numWrite > 0) { 104 int i = 0; 105 while (comPort.bytesAvailable() > 0 && i++ < 5) Thread.sleep(20); 106 byte[] readBuffer = new byte[comPort.bytesAvailable()]; int numRead = comPort.readBytes(readBuffer, readBuffer.length); if (numRead > 0) { reslutData = readBuffer; } } } catch (InterruptedException e) { throw new BhudyException(e.getMessage()); } return reslutData; } }