Any tips on Modbus over RS485 on MDBT42Q?

Posted on
  • I'm thinking of using a MDBT42Q module to poll data from a Modbus device over RS485.

    My hardware will use the MAX13487 that implements auto tx/rx switch, I have used this in many designs before and it works just fine with modbus. So no need for TX control pin.

    I found this, https://github.com/makejs/modbusTCP-Espruino, but cant make out if its just a clone of modbus-serial or if it's actually for Espruino.. anyone?

  • Hmm, I'm not sure about modbusTCP-EsprĀ­uino at all - but that sounds like it's over TCPIP?

    I've only done one thing with Modbus in the past, but for that I just used Serial1 directly - I didn't think there was that much else to it once the Serial port was set up correctly.

  • How did you sort out the CRC calculation??

    Thats the only part that can be a bit tricky for a simple modbus implementation, I can build the packet by hand and parse the response my self.

    I often get devices scattered all over the place that has a RS485 port with modbus, often its just a few bytes of data that I want from it. But running a hardwire etc etc to this makes it a lot of work. A small BLE device that polls data and sends it as BLE manufacture data would be perfect to just get the status and monitor the device values. WiFi is often hard to get working, and BLE actually has longer range in real life :-) Being able to reprogram the device wireless makes it just perfect.

  • Ahh - so in my case I was controlling an electricity meter and there were only 4 commands - so I just pulled those - including CRC - out of the datasheet.

    But the calculation looks ok - there's even a flowchart on page 40 of https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf which seems to be implemented in https://stackoverflow.com/a/19994882/1215872

    So... this would appear to work - at least it produces the values they have in their example:

    function addCRC16(buf) {
      // https://stackoverflow.com/a/19994882/1215872
      var crc = 0xFFFF;
      for (var pos=0;pos<buf.length;pos++) {
        crc ^= buf[pos];    // XOR byte into least sig. byte of crc
        for (var i = 8; i != 0; i--) {    // Loop over each bit
          if ((crc & 0x0001) != 0) {      // If the LSB is set
            crc >>= 1;                    // Shift right and XOR 0xA001
            crc ^= 0xA001;
          }
          else                            // Else LSB is not set
            crc >>= 1;                    // Just shift right
          }
        }
    
      buf.push(crc&0xFF, crc>>8);
      return buf;
    }
    
    var message = [0x02, 0x07];
    addCRC16(message);
    Serial1.write(message);
    
  • Sometimes one has to ask the question to realise the simple answer... It's an electricity meter I'm talking to as well, and yes the easy way is to just hard code the query :-)
    But it might be good to check the CRC on the reply.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Any tips on Modbus over RS485 on MDBT42Q?

Posted by Avatar for JohanWinas @JohanWinas

Actions