Java Serial Communication —— RXTXComm Maven
1 2 3 4 5 6 7 8 9 10 public String init (int baudRate) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); System.out.println("发现端口:" + portId.getName()); return portId.getName(); } return null ; }
Get Connection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public void connect (String portName) throws Exception { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if (portIdentifier.isCurrentlyOwned()) { System.out.println("Error: Port is currently in use" ); } else { CommPort commPort = portIdentifier.open(this .getClass().getName(), 2000 ); if (commPort instanceof SerialPort) { SerialPort serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(9600 , SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); InputStream in = serialPort.getInputStream(); SerialReader serialReader = new SerialReader(in); serialReader.setCallBack(this ); (new Thread(serialReader)).start(); } else { System.out.println("Error: Only serial ports are handled by this example." ); } } }
从buffer字节数组中获取数据最好使用do while的形式。防止丢失数据
getValue以+=
进行拼串getValue += new String(buffer,0,len);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public static class SerialReader implements Runnable { InputStream in; DataCallBack callBack; public void setCallBack (DataCallBack callBack) { this .callBack = callBack; } public SerialReader (InputStream in) { this .in = in; } String getValue = "" ; public void run () { byte [] buffer = new byte [1024 ]; int len = -1 ; try { do { len = this .in.read(buffer); getValue += new String(buffer,0 ,len); if (getValue != null && getValue.length() > 2 ){ System.out.println(getValue); System.out.println("#############################" ); if (callBack != null ) { callBack.onGetData(getValue); getValue = "" ; } } }while (len > -1 ); } catch (IOException e) { e.printStackTrace(); } }
Call Back DataCallBack.interface
1 2 3 4 5 package com.hziee.scanner.domain.tools;public interface DataCallBack { void onGetData (String data) ; }
Test4.java
1 2 3 4 5 6 @Override public void onGetData (String data) { this .result = data; System.out.println("callback--" + data); runMethod(result); }
1 2 3 4 5 DataCallBack callBack; public void setCallBack (DataCallBack callBack) { this .callBack = callBack; }
1 2 3 4 if (callBack != null ) { callBack.onGetData(getValue); getValue = "" ; }
1 serialReader.setCallBack(this );
Transfer 1 2 3 4 5 6 7 8 Test4 t = new Test4(); String port = t.init(9600 ); try { t.connect(port); } catch (Exception e) { e.printStackTrace(); }
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !