【问题标题】:Modbus TCP/IP on ArduinoArduino 上的 Modbus TCP/IP
【发布时间】:2016-09-06 10:47:40
【问题描述】:

我正在尝试使用以下代码在 arduino uno + ethernet shield 上实现 Modbus TCP。 我在 pc 上使用 modbus slave 模拟器来检查以下代码。但是,代码似乎不起作用。我已经从http://myarduinoprojects.com/modbus.html 下载了代码和库。如有必要,请建议我更正。还有另一个可用于 arduino 上的 modbus tcp/ip 的工作示例。

谢谢。

#include <SPI.h>
#include <Ethernet.h>
#include "MgsModbus.h"

MgsModbus Mb;
int inByte = 0; // incoming serial byte

// Ethernet settings (depending on MAC and Local network)
byte mac[] = {0x00, 0x1A, 0xB6, 0x02, 0xD1, 0x14 };
IPAddress ip(192, 168, 0, 35);



void setup()
{

  // serial setup
  Serial.begin(9600);
  Serial.println("Serial interface started");

  // initialize the ethernet device
  Ethernet.begin(mac, ip);   // start etehrnet interface
  Serial.println("Ethernet interface started"); 

  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();

  // slave address
  Mb.remSlaveIP = (192,168,0,1);

  // Fill MbData
//  Mb.SetBit(0,false);
  Mb.MbData[0] = 1;
  Mb.MbData[1] = 2;
  Mb.MbData[2] = 3;
  Mb.MbData[3] = 4;
  Mb.MbData[4] = 5;
  Mb.MbData[5] = 6;
  Mb.MbData[6] = 0;
  Mb.MbData[7] = 0;
  Mb.MbData[8] = 0;
  Mb.MbData[9] = 0;
  Mb.MbData[10] = 0;
  Mb.MbData[11] = 0;

  // print MbData
  for (int i=0;i<12;i++) {
    Serial.print("address: "); Serial.print(i); Serial.print("Data: "); Serial.println(Mb.MbData[i]);
  }
  // print menu
  Serial.println("0 - print the first 12 words of the MbData space");
  Serial.println("1 - FC 1 - read the first 5 coils from the slave and store them in the lower byte of MbData[1]");
  Serial.println("2 - FC 2 - read the first 5 discrete inputs from the slave and store them in the higer of the MbData[1]");
  Serial.println("3 - FC 3 - read the first 5 registers from the slave and store them in MbData[3..7");
  Serial.println("4 - FC 4 - read the first 5 input registers from the slave and store them in MbData[8..12]");
  Serial.println("5 - FC 5 - write coil 0 of the slave with the bit valeu of MbData[0.0]");
  Serial.println("6 - FC 6 - write register 0 of the slave with MbData[2]");
  Serial.println("7 - FC 15 - write 5 coils of the slave starting with coil 0 with GetBit(16..20");
  Serial.println("8 - Fc 16 - write 5 registers of the slave starting on register 0 with MbData[0..4]");

  Serial.println(Mb.remSlaveIP);
}

void loop()
{
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    if (inByte == '0') {                                          // print MbData
      for (int i=0;i<12;i++) {
        Serial.print("address: "); Serial.print(i); Serial.print("Data: "); Serial.println(Mb.MbData[i]);
      }
    }  
    if (inByte == '1') {Mb.Req(MB_FC_READ_COILS,               6,6,6);} // 1 // ref, count, pos
    if (inByte == '2') {Mb.Req(MB_FC_READ_DISCRETE_INPUT,      6,6,6);} // 2
    if (inByte == '3') {Mb.Req(MB_FC_READ_REGISTERS,           6,6,6);} // 3
    if (inByte == '4') {Mb.Req(MB_FC_READ_INPUT_REGISTER,      6,6,6);} // 4
    if (inByte == '5') {Mb.Req(MB_FC_WRITE_COIL,               0,0,0);} // 5 // count can be x
    if (inByte == '6') {Mb.Req(MB_FC_WRITE_REGISTER,           7,0,0);} // 6 // count can be x
    if (inByte == '7') {Mb.Req(MB_FC_WRITE_MULTIPLE_COILS,     0,6,0);} // 15
    if (inByte == '8') {Mb.Req(MB_FC_WRITE_MULTIPLE_REGISTERS, 0,6,0);} // 16
  }

  Mb.MbmRun();
//  Mb.MbsRun();
}

【问题讨论】:

  • “但是,代码似乎不起作用”。以什么方式?
  • 当我将它连接到PC并运行模拟器时,在串口上看不到通信。 @ghaham.reeds
  • 好的。你用什么检查没有通讯?你的程序没有检测到任何东西,或者你正在使用超级终端/串行监视器之类的东西来收听?
  • 我使用了来自以下网站modbustools.com/download.html的modbus从属模拟器。我期望的是PC的从(服务器)模拟器中的数据应该显示在Arduino IDE的串行监视器上。还有什么没有说清楚的吗?请告诉我。谢谢。 @graham.reeds
  • 你需要检查你是否正在通信(使用超级终端或其他东西),然后你需要检查你是否以相同的波特率运行,等等。

标签: arduino ethernet modbus-tcp


【解决方案1】:

串行监视器工作正常。在第一次调试程序时打印以下行

Serial interface started Ethernet interface started My IP address: 192.168.0.35. address: 0Data: 1 address: 1Data: 2 address: 2Data: 3 address: 3Data: 4 address: 4Data: 5 address: 5Data: 6 address: 6Data: 0 address: 7Data: 0 address: 8Data: 0 address: 9Data: 0 address: 10Data: 0 address: 11Data: 0 0 - print the first 12 words of the MbData space 1 - FC 1 - read the first 5 coils from the slave and store them in the lower byte of MbData[1] 2 - FC 2 - read the first 5 discrete inputs from the slave and store them in the higer of the MbData[1] 3 - FC 3 - read the first 5 registers from the slave and store them in MbData[3..7 4 - FC 4 - read the first 5 input registers from the slave and store them in MbData[8..12] 5 - FC 5 - write coil 0 of the slave with the bit valeu of MbData[0.0] 6 - FC 6 - write register 0 of the slave with MbData[2] 7 - FC 15 - write 5 coils of the slave starting with coil 0 with GetBit(16..20 8 - Fc 16 - write 5 registers of the slave starting on register 0 with MbData[0..4] 1.0.0.0

但似乎 TCP 通信不起作用,因为在此 @graham.reeds 之后串行监视器上没有任何内容

【讨论】:

    【解决方案2】:

    我在办公室使用 mgsmodbus 进行 modbuswork。我有一个主 TI 板和一个从模拟器。但是我使用的是 Wifi.h 而不是 ethernet.h。我可以做所有的操作。

    您是否检查过以太网连接是否正确建立?

    如果是这样,请尝试 IBH Modbus 从模拟器。这很完美。您必须将设置更改为 TCP 并选择端口。有时您尝试的端口号可能是错误的。我使用默认端口 502。

    另外,请确保 .cpp 代码中的从 IP 也已更改。

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 2018-08-22
      • 1970-01-01
      • 2020-07-16
      • 2021-12-05
      • 1970-01-01
      • 2020-12-01
      • 2019-11-27
      • 2018-06-25
      相关资源
      最近更新 更多