糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > java串口通信读取电子秤RS232

java串口通信读取电子秤RS232

时间:2023-03-12 16:13:56

相关推荐

java串口通信读取电子秤RS232

实现功能,按不同的端口号,记录并生成称重记录

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>

核心代码:

private static ConcurrentHashMap<String, SerialPort> serialPortMap = new ConcurrentHashMap<>();@Autowiredprivate SerialPortUtil serialPortUtil;//保存串口对象public static synchronized ConcurrentHashMap<String, SerialPort> getWebSocketMap() {return serialPortMap;}/*** 连接串口** @return*/public Msg serialPortAction(String name) {//设置串口的listenertry {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;}}

ps: 电子秤通讯协议一般分为连续、稳定、命令、接听,需根据实际需求选择电子秤,不同品牌的电子秤支持的通讯协议是不一样的。

调试软件和maven导入的jar:/download/qq_41902908/24190954

如果觉得《java串口通信读取电子秤RS232》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。