我必须使用类似于Arduino的TI Launchpad板来实现Modbus TCP。我有下面的片段。
MbmByteArray[0] = 0x00;
MbmByteArray[1] = 0x01;
MbmByteArray[2] = 0x00;
MbmByteArray[3] = 0x00;
MbmByteArray[4] = 0x00;
MbmByteArray[5] = 0x0B;
MbmByteArray[6] = 0x01;
MbmByteArray[7] = 0x10;
MbmByteArray[8] = 0x00;
MbmByteArray[9] = 0x00;
MbmByteArray[10] = 0x00;
MbmByteArray[11] = 0x02;
MbmByteArray[12] = 0x04;
MbmByteArray[13] = 0x00;
MbmByteArray[14] = 0x08;
MbmByteArray[15] = 0x00;
MbmByteArray[16] = 0x00;
Serial.println("Written:");
for(int i=0;i<MbmByteArray[5]+6;i++) {
int a=0;
a = MbmClient.write(MbmByteArray[i]);
if(a)
{
// if something is written to the client I check what it is !
Serial.println(MbmByteArray[i]);
}
}这是我的客户
您可以看到没有连续地接收到字节。但我的整个数组就像对客户端的命令。不管怎么说,有这样的方法吗?
2016-06-17 14:28:00.252:会议已创建
2016-06-17 14:28:00.254:会议开幕
2016-06-17 14:28:00.256: 17字节
00 01 00 00 00 0B 01 10 00 00 00 02 04 00 07 00
2016-06-17 14:28:00.269: 12字节发送
< 00 01 00 00 00 06 01 10 00 00 00 02
请帮帮我!
发布于 2016-06-23 08:41:51
通常,在任何给定的行、文件或媒体上进行通信时,您可能会破坏数据。以太网实际上有一个包大小(MTU),可以/将不中断地交付。但那是另一个故事。如果您处理这个问题,无论是协议、硬件还是平台,都会更好。(至少要注意到这一点。)
当您阅读您的ModbusTCP时,应该创建类似于以下伪代码的内容:
//read all ModbusTCP header
while less than 6 bytes received and not timed out and not error
read bytes
//read data
data_length = modbustcp_header position 5 and 6
while less than data_length and not timed out and not error
read bytes上述功能将收集整个包,然后“释放”到您的引擎。该算法将适用于您的设置的两边。(德克萨斯州的hw和PC都是。)
您还可以(很可能)摆弄TI TCP堆栈,并使其返回更大的块。我猜这就是你想要的。但我还是不建议走那条路。
发布于 2016-06-23 16:37:28
谢谢你伊莱什尔!
我找到了办法。在Arduino中有一个客户端函数,它可以发送整个数组而不每次发送一个值。
byte command[17] = {0x00,0x01,0x00,0x00,0x00,0x0B,0x01,0x10,0x00,0x00,0x00,0x02,0x04,0x00,0x07,0x00,0x00};
MbmClient.write(command,17);这个client.write(缓冲区,元素数)帮助我在一个包中发送整个内容。它就像一种魅力:)
https://stackoverflow.com/questions/37975604
复制相似问题