首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Modbus TCP - Arduino

Modbus TCP - Arduino
EN

Stack Overflow用户
提问于 2016-06-22 18:22:30
回答 2查看 307关注 0票数 1

我必须使用类似于Arduino的TI Launchpad板来实现Modbus TCP。我有下面的片段。

代码语言:javascript
复制
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

请帮帮我!

EN

回答 2

Stack Overflow用户

发布于 2016-06-23 08:41:51

通常,在任何给定的行、文件或媒体上进行通信时,您可能会破坏数据。以太网实际上有一个包大小(MTU),可以/将不中断地交付。但那是另一个故事。如果您处理这个问题,无论是协议、硬件还是平台,都会更好。(至少要注意到这一点。)

当您阅读您的ModbusTCP时,应该创建类似于以下伪代码的内容:

代码语言:javascript
复制
//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堆栈,并使其返回更大的块。我猜这就是你想要的。但我还是不建议走那条路。

票数 1
EN

Stack Overflow用户

发布于 2016-06-23 16:37:28

谢谢你伊莱什尔!

我找到了办法。在Arduino中有一个客户端函数,它可以发送整个数组而不每次发送一个值。

代码语言:javascript
复制
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(缓冲区,元素数)帮助我在一个包中发送整个内容。它就像一种魅力:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37975604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档