当前位置: 代码迷 >> 综合 >> java读取modbus(串口,DTU(WIFI )也可 DTU(gprs)也可)协议数据 解释详细 踩坑完整 拿来就可以用的demo(下)
  详细解决方案

java读取modbus(串口,DTU(WIFI )也可 DTU(gprs)也可)协议数据 解释详细 踩坑完整 拿来就可以用的demo(下)

热度:18   发布时间:2024-02-07 02:54:52.0

福兮祸所伏, 祸兮福所倚。(现在发生在你身上的都是好事,福祸相伴)  

这些源码应该都可以看的懂  我就不解释了。

public void Connect(String comPort) throws Exception {// 类似于串口连接(个人理解)this.portIdentifier = CommPortIdentifier.getPortIdentifier(comPort);if (this.portIdentifier.isCurrentlyOwned()) {System.out.println("Error: Port is currently in use");} else {int timeout = 2000;// 串口对象CommPort commPort = this.portIdentifier.open(this.getClass().getName(), timeout);if (commPort instanceof SerialPort) {this.serialPort = (SerialPort)commPort;this.serialPort.setSerialPortParams(9600, 8, 1, 2);this.serialPort.enableReceiveTimeout(1000);this.serialPort.disableReceiveThreshold();this.in = this.serialPort.getInputStream();this.