zhaopeiym / IoTClient

This is an IoT device communication protocol implementation client, which will include common industrial communication protocols such as mainstream PLC communication reading, ModBus protocol, and Bacnet protocol. This component is open source and free for life, using the most relaxed MIT open source agreement, you can modify and commercial use at will (commercial use please evaluate and test). 这是一个物联网设备通讯协议实现客户端,将会包括主流PLC通信读取、ModBus协议、Bacnet协议等常用工业通讯协议。本组件终身开源免费,采用最宽松的MIT开源协议,您可以随意修改和商业使用(商业使用请做好评估和测试)。
MIT License
1.27k stars 408 forks source link

SendPackage错误 #5

Closed icysky closed 3 years ago

icysky commented 3 years ago

当functionCode=2,会计算出一个readLenght=13,但实际设备返回的可长度可能小于13,比如我用的的设备返回的只有长度只有6,这会导致在SocketTryRead中的while循环时,第一次已经把所有buffer读回来了,结果也是正确的,但因为判定长度小于13会再次读取,然后再下次循环中进行 socket.Receive时就报错了,应该在Recevie之前判断一下socket.Available?

zhaopeiym commented 3 years ago

请问什么情况下 计算出来的长度和 设备返回的长度不一样? 那是不是因为计算错了?

icysky commented 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,二次再读取就会报错

zhaopeiym commented 3 years ago

功能码为2是读取离散,追加长度应该是1。 5+1 刚好是6。

你说的5加8 那8是怎么来的?

或者你可以用Modbus Slave 模拟,我试了是可以的。

client.ReadDiscrete("33")你这样调试进去

zhaopeiym commented 3 years ago

你贴出了的 读写代码看看。

icysky commented 3 years ago

SOGA! 虽然设备有8个口,但是返回后其实只有一位(byte),是2进制后8位,理解错误!