问题描述
我正在尝试编写一个示例程序,以使用带有 apache plc4x 库的串行端口从 SHT20 温度传感器中检索温度数据。
private void plcRtuReader() {
String connectionString =
"modbus:serial://COM5?unit-identifier=1&baudrate=19200&stopBits=" + SerialPort.ONE_STOP_BIT + "&parityBits=" + SerialPort.NO_PARITY + "&dataBits=8";
try (PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionString)) {
if (!plcConnection.getMetadata().canRead()) {
System.out.println("This connection doesn't support reading.");
return;
}
PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
builder.addItem("value-1","holding-register:258[2]");
PlcReadRequest readRequest = builder.build();
PlcReadResponse response = readRequest.execute().get();
for (String fieldName : response.getFieldNames()) {
if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {
int numValues = response.getNumberOfValues(fieldName);
// If it's just one element,output just one single line.
if (numValues == 1) {
System.out.println("Value[" + fieldName + "]: " + response.getobject(fieldName));
}
// If it's more than one element,output each in a single row.
else {
System.out.println("Value[" + fieldName + "]:");
for (int i = 0; i < numValues; i++) {
System.out.println(" - " + response.getobject(fieldName,i));
}
}
}
// Something went wrong,to output an error message instead.
else {
System.out.println(
"Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
}
}
System.exit(0);
} catch (PlcConnectionException e) {
e.printstacktrace();
} catch (Exception e) {
e.printstacktrace();
}
}
使用串行通信与设备建立连接。但它无法获取数据,而是不断打印以下警告消息。
调试器挂在下面一行:
PlcReadResponse response = readRequest.execute().get();
以下日志连续打印。
2021-06-03-17:41:48.425 [nioEventLoopGroup-2-1] WARN io.netty.channel.nio.NioEventLoop - Selector.select() returned prematurely 512 times in a row; rebuilding Selector org.apache.plc4x.java.transport.serial.SerialPollingSelector@131f8986.
2021-06-03-17:41:55.080 [nioEventLoopGroup-2-1] WARN io.netty.channel.nio.NioEventLoop - Selector.select() returned prematurely 512 times in a row; rebuilding Selector org.apache.plc4x.java.transport.serial.SerialPollingSelector@48c328c5.
使用 modpoll.exe 使用相同的 URL 数据(即波特率、停止位等),它可以工作并通过 RTU 返回数据。我不确定这里缺少什么。 请在此说明一些问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)