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

anyine 发表于 2020-4-11 23:52

新手求助,8266 modbus通信问题

有那位大哥做过这个东西,我用arduinoIDE + esp8266 + im1281b,目的是想读取1281b里面的寄存器数据,但是一直读不到,求指点一下,谢谢!


void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
unsigned charTx_Buffer;
unsigned charRx_Buffer;
unsigned charread_enable, receive_finished, reveive_number;
unsigned longVoltage_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 charbyte;
    }crcnow;
Tx_Buffer = 0x01; //模块的 ID 号,默认 ID 为 0x01
Tx_Buffer = 0x03;
Tx_Buffer = 0x00;
Tx_Buffer = 0x48;
Tx_Buffer = 0x00;
Tx_Buffer = 0x08;
crcnow.word16 = chkcrc(Tx_Buffer, 6);
Tx_Buffer = crcnow.byte; //CRC 效验低字节在前
Tx_Buffer = crcnow.byte;

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;
}



cxoo 发表于 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 编译时出错。

anyine 发表于 2020-4-12 23:55

搞定了,两个模块串口干扰问题

google 发表于 2020-7-10 14:12

确实,你这代码试了没问题的

冇头嘤 发表于 2021-9-16 07:48

求个教程 能带0103功能码示例更好

wifi2004@ 发表于 2022-8-6 15:36

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

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

遇到同样问题,同问
页: [1]
查看完整版本: 新手求助,8266 modbus通信问题