查看: 3649|回复: 5

新手求助,8266 modbus通信问题

[复制链接]

该用户从未签到

发表于 2020-4-11 23:52 | 显示全部楼层 |阅读模式
有那位大哥做过这个东西,我用arduinoIDE + esp8266 + im1281b,目的是想读取1281b里面的寄存器数据,但是一直读不到,求指点一下,谢谢!


void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}
unsigned char  Tx_Buffer[8];
unsigned char  Rx_Buffer[24];
unsigned char  read_enable, receive_finished, reveive_number;
unsigned long  Voltage_data, Current_data, Power_data, Energy_data, Pf_data, CO2_data;
// the loop function runs over and over again forever
void loop() {

  union crcdata {
    unsigned int word16;  
    unsigned char  byte[2];
    }crcnow;
  Tx_Buffer[0] = 0x01; //模块的 ID 号,默认 ID 为 0x01
  Tx_Buffer[1] = 0x03;
  Tx_Buffer[2] = 0x00;
  Tx_Buffer[3] = 0x48;
  Tx_Buffer[4] = 0x00;
  Tx_Buffer[5] = 0x08;
  crcnow.word16 = chkcrc(Tx_Buffer, 6);
  Tx_Buffer[6] = crcnow.byte[1]; //CRC 效验低字节在前
  Tx_Buffer[7] = crcnow.byte[0];

  Serial.write(Tx_Buffer, 8);
  delay(1000);
  while (Serial.available() > 0) {
    //Serial.readBytes(Rx_Buffer, 24);
    //Serial.flush();
    int x = Serial.read();
    Serial.print(x,HEX);
  }
}

unsigned int calccrc(unsigned char crcbuf, unsigned int crc) {
  unsigned char i;
  unsigned char chk;
  crc = crc ^ crcbuf;
  for (i = 0; i < 8; i++)  {
    chk = ( unsigned char)(crc & 1);
    crc = crc >> 1;
    crc = crc & 0x7fff;
    if (chk == 1) crc = crc ^ 0xa001;
    crc = crc & 0xffff;
  } return crc;
}
unsigned int chkcrc(unsigned char *buf, unsigned char len) {
  unsigned char hi, lo;
  unsigned int i;   
  unsigned int crc;   
  crc = 0xFFFF;  
  for (i = 0; i < len; i++)  {
    crc = calccrc(*buf, crc);
    buf++;
  }
  hi = ( unsigned char)(crc % 256);  
  lo = ( unsigned char)(crc / 256);
  crc = (((unsigned int)(hi)) << 8) | lo;
  return crc;
}



IM1281B电能计量模块技术手册V1.5.rar

898.34 KB, 下载次数: 41

该用户从未签到

发表于 2020-4-23 12:41 | 显示全部楼层
你最后搞通了么?我也在弄modbus一直编译失败,用的 arduinomodbus.h 库

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp: In member function 'virtual void RS485Class::begin(long unsigned int, uint16_t)':

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:53:34: error: invalid conversion from 'uint16_t {aka short unsigned int}' to 'SerialConfig' [-fpermissive]

   _serial->begin(baudrate, config);

                                  ^

In file included from C:\Users\zhaom\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/Arduino.h:244:0,

                 from C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.h:23,

                 from C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:20:

C:\Users\zhaom\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/HardwareSerial.h:78:10: error:   initializing argument 2 of 'void HardwareSerial::begin(long unsigned int, SerialConfig)' [-fpermissive]

     void begin(unsigned long baud, SerialConfig config)

          ^

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp: In member function 'void RS485Class::sendBreak(unsigned int)':

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:149:36: error: invalid conversion from 'uint16_t {aka short unsigned int}' to 'SerialConfig' [-fpermissive]

   _serial->begin(_baudrate, _config);

                                    ^

In file included from C:\Users\zhaom\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/Arduino.h:244:0,

                 from C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.h:23,

                 from C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:20:

C:\Users\zhaom\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/HardwareSerial.h:78:10: error:   initializing argument 2 of 'void HardwareSerial::begin(long unsigned int, SerialConfig)' [-fpermissive]

     void begin(unsigned long baud, SerialConfig config)

          ^

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp: In member function 'void RS485Class::sendBreakMicroseconds(unsigned int)':

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:159:36: error: invalid conversion from 'uint16_t {aka short unsigned int}' to 'SerialConfig' [-fpermissive]

   _serial->begin(_baudrate, _config);

                                    ^

In file included from C:\Users\zhaom\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/Arduino.h:244:0,

                 from C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.h:23,

                 from C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:20:

C:\Users\zhaom\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/HardwareSerial.h:78:10: error:   initializing argument 2 of 'void HardwareSerial::begin(long unsigned int, SerialConfig)' [-fpermissive]

     void begin(unsigned long baud, SerialConfig config)

          ^

In file included from C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:20:0:

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp: At global scope:

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.h:35:30: error: 'A6' was not declared in this scope

#define RS845_DEFAULT_DE_PIN A6

                              ^

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:169:62: note: in expansion of macro 'RS845_DEFAULT_DE_PIN'

RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN);

                                                              ^

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.h:36:30: error: 'A5' was not declared in this scope

#define RS845_DEFAULT_RE_PIN A5

                              ^

C:\Users\zhaom\Documents\Arduino\libraries\ArduinoRS485\src\RS485.cpp:169:84: note: in expansion of macro 'RS845_DEFAULT_RE_PIN'

RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN);

                                                                                    ^

exit status 1
为开发板 Generic ESP8266 Module 编译时出错。

该用户从未签到

 楼主| 发表于 2020-4-12 23:55 | 显示全部楼层
搞定了,两个模块串口干扰问题

该用户从未签到

发表于 2020-7-10 14:12 | 显示全部楼层
确实,你这代码试了没问题的

该用户从未签到

发表于 2021-9-16 07:48 来自手机 | 显示全部楼层
求个教程 能带01  03功能码示例更好

该用户从未签到

发表于 2022-8-6 15:36 | 显示全部楼层
cxoo 发表于 2020-4-23 12:41
你最后搞通了么?我也在弄modbus一直编译失败,用的 arduinomodbus.h 库

C:%users\zhaom\Documents\Arduin ...

遇到同样问题,同问
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐

求助VSCode+PlatformIO Arduino怎么把文件包含到工程中
求助VSCode+PlatformIO Ar
请教一下论坛大佬 PlatformIO对文件进行编译过程中报错“undefined reference to xxx
一款基于Arduino平台的零代码就可驱动的串口彩屏分享
一款基于Arduino平台的零
多年前在论坛上看到一个帖子: 引起了我的共鸣:能不能有一种彩屏,可以像Arduino I
自制麦轮小车robomasterV4详细教程
自制麦轮小车robomasterV4
一 介绍 这个帖子中,我将介绍用arduino制作一台PID调制的麦轮小车底盘部分的方法 资
MS39233 无刷直流马达—完美替代 ST SPIN233
MS39233 无刷直流马达—完
MS39233 是一款低压三个半桥驱动器。完美替代ST SPIN233。它可应用于低电压及电池供电
用arduino nano 连接esp8266,代码写不进esp8266里面,有偿求解决
用arduino nano 连接esp82
哪位大佬能帮我解决下,发二十元红包请喝杯奶茶。+V:lcmazjq 接线图和错误信息在附件
Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
快速回复 返回顶部 返回列表