實作功能,按不同的端口号,記錄并生成稱重記錄
maven導入依賴
<!-- 序列槽jar -->
<dependency>
<groupId>gnu.io</groupId>
<artifactId>com-lib</artifactId>
<version>2.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jar/RXTXcomm.jar</systemPath>
</dependency>
核心代碼:
//儲存序列槽對象
public static synchronized ConcurrentHashMap<String, SerialPort> getWebSocketMap() {
return serialPortMap;
}
/**
* 連接配接序列槽
*
* @return
*/
public Msg serialPortAction(String name) {
//設定序列槽的listener
try {
final SerialPort serialPort = serialPortUtil.openSerialPort(name, 9600);
//儲存序列槽對象
serialPortMap.put(name, serialPort);
serialPortUtil.setListenerToSerialPort(serialPort, event -> {
//資料通知
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
byte[] bytes = serialPortUtil.readData(serialPort);
if (bytes != null) {
String weight = new String(bytes);
if (StringUtils.isNotBlank(weight)) {
System.out.println(name);
System.out.println("收到的資料長度:" + bytes.length);
System.out.println("重量為:" + weight);
}
}
}
});
} catch (TooManyListenersException | NoSuchPortException | UnsupportedCommOperationException e) {
//不存在序列槽
e.printStackTrace();
} catch (PortInUseException e) {
//序列槽斷開異常
e.printStackTrace();
}
return Msg.success();
}
/**
* 關閉序列槽
*
* @param name
* @return
*/
public Msg closeSerialPort(String name) {
SerialPort serialPort = serialPortMap.get(name);
serialPortUtil.closeSerialPort(serialPort);
serialPortMap.remove(name);
return Msg.success();
}
序列槽工具類
public class SerialPortUtil {
/**
* 關閉序列槽
*
* @param serialPort 要關閉的序列槽對象
*/
public void closeSerialPort(SerialPort serialPort) {
if (serialPort != null) {
serialPort.close();
}
}
/**
* 給序列槽設定監聽
*
* @param serialPort serialPort 要讀取的序列槽
* @param listener SerialPortEventListener監聽對象
* @throws TooManyListenersException 監聽對象太多
*/
public void setListenerToSerialPort(SerialPort serialPort, SerialPortEventListener listener) throws TooManyListenersException {
//給序列槽添加事件監聽
serialPort.addEventListener(listener);
//序列槽有資料監聽
serialPort.notifyOnDataAvailable(true);
//中斷事件監聽
serialPort.notifyOnBreakInterrupt(true);
}
/**
* 關閉監聽
*
* @param port
*/
private void removeListener(SerialPort port) {
port.notifyOnRingIndicator(false);
port.notifyOnParityError(false);
port.notifyOnOverrunError(false);
port.notifyOnOutputEmpty(false);
port.notifyOnFramingError(false);
port.notifyOnDSR(false);
port.notifyOnDataAvailable(false);
port.notifyOnCTS(false);
port.notifyOnCarrierDetect(false);
port.notifyOnBreakInterrupt(false);
port.removeEventListener();
}
/**
* 從序列槽讀取資料
*
* @param serialPort 要讀取的序列槽
* @return 讀取的資料
*/
public byte[] readData(SerialPort serialPort) {
InputStream is = null;
byte[] bytes = null;
try {
//獲得序列槽的輸入流
is = serialPort.getInputStream();
//獲得資料長度
int bufflenth = is.available();
while (bufflenth != 0) {
//初始化byte數組
bytes = new byte[bufflenth];
is.read(bytes);
bufflenth = is.available();
}
} catch (IOException e) {
log.error("序列槽斷開關閉監聽");
removeListener(serialPort);
closeSerialPort(serialPort);
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}
}