ESP8266 / Arduino modbus RTU缓冲区数据转换

问题描述 投票:0回答:1

我正在使用ESP8266和ModbusMaster.h库与支持RS485的功率计进行通信。沟通工作正常,但回应是令我困惑的,我无法得到正确的价值观。我的功率计显示1.49千瓦时,但Modbus的响应是16318.这是我的代码:

    #include <ArduinoOTA.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SimpleTimer.h>
    #include <ModbusMaster.h>
    #include <ESP8266WiFi.h> 
    /*
    Debug. Change to 0 when you are finished debugging.
    */
    const int debug = 1; 
    #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

    int timerTask1, timerTask2, timerTask3;
    float battBhargeCurrent, bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
    float stats_today_pv_volt_min, stats_today_pv_volt_max;
    uint8_t result; 
    // this is to check if we can write since rs485 is half duplex
    bool rs485DataReceived = true;

    float  data[100];

    ModbusMaster node;
    SimpleTimer timer;

    // tracer requires no handshaking
    void preTransmission() {}
    void postTransmission() {}

    // a list of the regisities to query in order
    typedef void (*RegistryList[])();
    RegistryList Registries = { 
    AddressRegistry_0001       // samo potrosnju
    };
    // keep log of where we are
    uint8_t currentRegistryNumber = 0;
    // function to switch to next registry
    void nextRegistryNumber() {
    currentRegistryNumber = (currentRegistryNumber + 1) % ARRAY_SIZE( Registries);
    }

    void setup()
    {
    // Serial.begin(115200);
    Serial.begin(9600, SERIAL_8E1); //, SERIAL_8E1

    // Modbus slave ID 1
    node.begin(1, Serial);
    node.preTransmission(preTransmission);
    node.postTransmission(postTransmission);
    // WiFi.mode(WIFI_STA); 

    while (Blynk.connect() == false) {}
    ArduinoOTA.setHostname(OTA_HOSTNAME);
    ArduinoOTA.begin();

    timerTask1 = timer.setInterval(9000, updateBlynk); 
    timerTask2 = timer.setInterval(9000, doRegistryNumber);
    timerTask3 = timer.setInterval(9000, nextRegistryNumber);
    }

    // -------------------------------------------------------------------------------- 

    void doRegistryNumber() {
    Registries[currentRegistryNumber]();
    }


    void AddressRegistry_0001() {  

  uint8_t j;
  uint16_t dataval[2];

 result = node.readHoldingRegisters(0x00, 2); 
if (result == node.ku8MBSuccess)
{  
   for (j = 0; j < 2; j++)                        // set to 0,1 for two 
datablocks
    {
        dataval[j] = node.getResponseBuffer(j);
    }


     terminal.println("---------- Show power---------");
     terminal.println("kWh: ");
     terminal.println(dataval[0]);
     terminal.println("crc: ");
     terminal.println(dataval[1]);


     terminal.println("-----------------------"); 
     terminal.flush();   
     node.clearResponseBuffer();
     node.clearTransmitBuffer(); 
} else {
  rs485DataReceived = false;
} 

}

    void loop()
    {
    Blynk.run();
    // ArduinoOTA.handle();
    timer.run();
    }

我尝试了类似的东西,但使用Raspberry Pi和USB-RS485,它的工作原理。 NodeJS代码示例如下。它看起来类似于Arduino代码。

    // create an empty modbus client
    var ModbusRTU = require("modbus-serial");
    var client = new ModbusRTU();

    // open connection to a serial port
    client.connectRTUBuffered("/dev/ttyUSB0", { baudRate: 9600, parity: 'even' }, read);

    function write() {
        client.setID(1);

        // write the values 0, 0xffff to registers starting at address 5
        // on device number 1.
        client.writeRegisters(5, [0 , 0xffff])
            .then(read);
    }

    function read() {
        // read the 2 registers starting at address 5
        // on device number 1.
            console.log("Ocitavanje registra 0000: ");
        client.readHoldingRegisters(0000, 12)
            .then(function(d) {
                var floatA = d.buffer.readFloatBE(0);
            // var floatB = d.buffer.readFloatBE(4);
            // var floatC = d.buffer.readFloatBE(8);
            // console.log("Receive:", floatA, floatB, floatC); })
            console.log("Potrosnja u kWh: ", floatA); })
            .catch(function(e) {
                console.log(e.message); })
            .then(close);
    }

    function close() {
        client.close();
    }

此代码在控制台中显示1.493748298302。

我怎样才能在Arduino中实现这个var floatA = d.buffer.readFloatBE(0);?看起来readFloatBE(0)可以解决问题,但只能在NodeJS / javascript中使用。

这里我是我的设备enter image description here数据表的一部分

enter image description here

以下是我从设备随附的原始软件获得的结果:enter image description here

如果有人可以指出我更好的方向,那么我就会这么做。

更新:

我找到了ShortBus Modbus扫描仪软件并测试了读数。库读取结果为无符号整数,但需要交换浮点和字顺序。它显示在下面的图像中。

有人可以告诉你如何设置正确的转换。

enter image description here

node.js esp8266 modbus rs485
1个回答
0
投票

是的,确实问题是由var floatA = d.buffer.readFloatBE(0);Modbus完成的部分返回一个字节数组,客户端必须解释这些字节,理想情况下由您正在使用的库完成,但如果在Arduino上不可用,您可以手动尝试字节解码功能,具有以下考虑:

Modbus寄存器的长度为16位,因此长度1 = 16位,长度2 = 32位,因此在文档中标注为float32的数据类型表示“用于此值的2个寄存器,解释为float”。

因此,在client.readHoldingRegisters(0000, 12)you要求读取地址00和大小12的寄存器...所以这没有意义,你只需要2个寄存器。

在您的示例节点代码上,首先您在client.writeRegisters(5, [0 , 0xffff])寄存器5 = 0中写入2个寄存器到地址5,并且寄存器6 = 0xFFFF,为什么?然后你去读取地址0,读取(),这是你的文件的总KwH的地址。

所以,你应该得到一个字节数组,你需要将它们解码为浮点数。 Modbus是单词和字节的Big Endian,因此您需要在解码函数中使用它们。我不知道究竟Arduino中有什么可用,但希望你能用这些额外的信息来解决它。

我想如果你只是发送缓冲区进行打印,你将获得值的整数解释,因此问题


0
投票

你有没有正确使用这个功能?我面临同样的问题,并试图弄清楚如何从modbus地址8192得到一个浮动值2位小数,这是2个数据块。

void loop()
{
  uint8_t j, result;
  uint16_t dataval[2];

  result = node.readHoldingRegisters(8192, 2);      // slave: read (6) 16-bit registers starting at register .. to RX buffer , this address is in Decimal, so convert hex to decimal to use correct address
  if (result == node.ku8MBSuccess)                  // do something with data if read is successful
  {
    for (j = 0; j < 2; j++)                        // set to 0,1 for two datablocks
    {
      dataval[j] = node.getResponseBuffer(j);


    }

    //********************************
    String myValuea =  String(dataval[0], HEX); //Convert it into Hexadecimal
    String myValueb =  String(dataval[1], HEX); //Convert it into Hexadecimal
© www.soinside.com 2019 - 2024. All rights reserved.