极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14406|回复: 3

关于Arduino与信捷PLC的modbus问题

[复制链接]
发表于 2016-5-16 09:08:47 | 显示全部楼层 |阅读模式
我现在正在做ARDUINO与国产的信捷PLC的modbus协议通讯。

                     用的是ttl232转RS232的模块,然后将TX RX,等线连接好后。

       在PLC处进行了编辑,写了一个读程序和一个写程序,然后用格西烽火软件通过USB串口线看到确实有数据出来

       然后我用了下面的这一个程序。

#include <ModbusRtu.h>

uint16_t au16data[16]; //!< data array for modbus network sharing
uint8_t u8state; //!< machine state
uint8_t u8query; //!< pointer to message query

/**
*  Modbus object declaration
*  u8id : node id = 0 for master, = 1..247 for slave
*  u8serno : serial port (use 0 for Serial)
*  u8txenpin : 0 for RS-232 and USB-FTDI
*               or any pin number > 1 for RS-485
*/
Modbus master(0,0,0); // this is master and RS-232 or USB-FTDI

/**
* This is an structe which contains a query to an slave device
*/
modbus_t telegram[2];

unsigned long u32wait;

void setup() {
  // telegram 0: read registers--------du han shu
  telegram[0].u8id = 4; // slave address
  telegram[0].u8fct = 3; // function code (this one is registers read)
  telegram[0].u16RegAdd = 5; // start address in slave
  telegram[0].u16CoilsNo = 2; // number of elements (coils or registers) to read
  telegram[0].au16reg = au16data; // pointer to a memory array in the Arduino

  // telegram 1: write a single register----xie han shu
  telegram[1].u8id = 3; // slave address
  telegram[1].u8fct = 6; // function code (this one is write a single register)
  telegram[1].u16RegAdd = 2; // start address in slave
  telegram[1].u16CoilsNo = 10; // number of elements (coils or registers) to read
  telegram[1].au16reg = au16data; // pointer to a memory array in the Arduino
       
  master.begin( 19200 ); // baud-rate at 19200
  master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
  u32wait = millis() + 100;
  u8state = u8query = 1;
}

void loop() {
  switch( u8state ) {
  case 0:
    if (millis() > u32wait) u8state++; // wait state
    break;
  case 1:
    master.query( telegram[u8query] ); // send query (only once)
    u8state++;
        u8query++;
        if (u8query > 2) u8query = 0;
    break;
  case 2:
    master.poll(); // check incoming messages
    if (master.getState() == COM_IDLE) {
      u8state = 0;
      u32wait = millis() + 1000;
    }
    break;
  }

  au16data[4] = analogRead(1);
  
}



结果我无论如何也无法进行通讯,希望有大神帮忙看看

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2016-5-18 08:18:51 | 显示全部楼层
直接用格西对plc调试啊~不要自己写程序先
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-5-18 08:30:33 | 显示全部楼层
maxims 发表于 2016-5-18 08:18
直接用格西对plc调试啊~不要自己写程序先

我也是按照格式来的,可是怎么调试也是对不上通讯
回复 支持 反对

使用道具 举报

发表于 2016-5-19 16:27:48 | 显示全部楼层
学习学习学习学习
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-20 20:18 , Processed in 0.040944 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表