Closed icysky closed 3 years ago
请问什么情况下 计算出来的长度和 设备返回的长度不一样? 那是不是因为计算错了?
https://github.com/zhaopeiym/IoTClient/blob/master/IoTClient/Clients/Modbus/ModbusRtuOverTcpClient.cs 第135行 功能码是2的时候,发送的指令总长度为8时,自动会把一个5追加到8上面,结果13会做为最后返回读取总长度,但我的设备实际返回只有6 在 https://github.com/zhaopeiym/IoTClient/blob/master/IoTClient/SocketBase.cs 中117行的循环中,第一次取6位后,已经全部取出,但是会小于参数13,二次再读取就会报错
功能码为2是读取离散,追加长度应该是1。 5+1 刚好是6。
你说的5加8 那8是怎么来的?
或者你可以用Modbus Slave 模拟,我试了是可以的。
client.ReadDiscrete("33")
你这样调试进去
你贴出了的 读写代码看看。
SOGA! 虽然设备有8个口,但是返回后其实只有一位(byte),是2进制后8位,理解错误!
当functionCode=2,会计算出一个readLenght=13,但实际设备返回的可长度可能小于13,比如我用的的设备返回的只有长度只有6,这会导致在SocketTryRead中的while循环时,第一次已经把所有buffer读回来了,结果也是正确的,但因为判定长度小于13会再次读取,然后再下次循环中进行 socket.Receive时就报错了,应该在Recevie之前判断一下socket.Available?