新手求助,8266 modbus通信问题-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4108|回复: 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, 下载次数: 43

发表于 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 ...

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

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 08:39 , Processed in 0.172470 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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