-->
Page 1 of 1

Micropython - Modbus RTU communication help

PostPosted: Tue Jan 26, 2021 6:24 am
by 0_djek
Hello, I have this meter: https://www.fif.com.pl/en/usage-electri ... -01mr.html and I want to read it's values using modbus. According to that documentation, I have to send command 3 (read holding registers, if I'm correct), but I have trouble making the query. Could anyone help, please? Here's more documentation: https://shop.marcomweb.it/images/storie ... 0%20EN.pdf. I mainly want to read active power (kWh). And here's my code:
Code: Select allimport utime
from machine import UART
from machine import Pin

import uos


# query: slave address\function code\start address (hi)\start address (lo)\number of points (hi)\number of points (lo)
# query: b'\x01\x03\x04\x00\x00\x08'

def test():
    print("modbus")
    uos.dupterm(None, 1)
    modbus = UART(0)
    modbus.init(baudrate=9600, parity=0, bits=8, stop=1, timeout=500, timeout_char=2, tx=Pin(1), rx=Pin(3))
    print("Reading from modbus: {}".format(modbus))
    while True:
      modbus.write(b'\x01\x03\x04\x00\x00\x08')
      print("Can read: {}".format(modbus.any()))
      utime.sleep(0.1)
      result = modbus.readline()
      if result is not None:
        print("Value of reading: {}, type of {}".format(result, type(result)))
      utime.sleep(0.5)
    uart = UART(0, 115200)
    uos.dupterm(uart, 1)


I tried making the query, but I don't understand it as good therefore it's not working. Also, the result I get after reading is None, and accoding to micropython documentation, it shows, that it timeouted. Thanks again for any help!!!