Examples

ModbusTcp

In this example, a ModbusTCP object is created for tcp communication with a slave device
################################################################################
# Modbus communications via tcp.
#
# Created: 2020-10-15
# Author: S. Torneo
################################################################################

import streams

# Needed for internet connectivity.
# This is dependent on your device.
from wireless import wifi 
from espressif.esp32net import esp32wifi as wifi_driver
from modbus import modbus

wifi_driver.auto_init()
streams.serial()

try:
    try:
        wifi.link(SSID, 3, PASSWORD)
    except Exception as e:
        print("Exception while trying to connect to the network ", e)
            
    master = modbus.ModbusTCP(1)

    try:
        master.connect(IP_ADDRESS)
        print("Connected.")
    except Exception as e:
        print("Couldn't connect to the slave device ", e)

    result = master.write_coil(2, 0xff00)
    print("Written coil 2: ", result)

    result = master.write_register(55, 745)
    print("Written discrete register: ", result)

    result = master.write_multiple_coils(10, 10, [1, 0, 1, 1, 0, 0, 1, 1, 1, 0])
    print("Written ", result, " coils")

    result = master.write_multiple_registers(20, 10, [55, 555, 123, 42, 352, 546, 754, 34, 643, 23])
    print("Written ", result, " registers")

    coils = master.read_coils(10, 10)
    print("Coils 10-19 status: ", coils)

    discrete = master.read_discrete(10, 5)
    print("Discrete inputs 10-14 values: ", discrete)

    holding = master.read_holding(20, 10)
    print("Holding registers 20-29 values: ", holding)

    inputs = master.read_input(20, 10)
    print("Input registers 20-29 values: ", inputs)

    master.close()
    print("Closed.")
	
except Exception as e:
	print("Error ", e)

ModbusSerial

In this example, a device class is defined and used for serial communication.

It is also shown how to use the library in TCP communication.
################################################################################
# Modbus communications via serial line.
#
# Created: 2020-10-12
# Author: S. Torneo
################################################################################

from semtech.sx1503 import sx1503
import gpio
from modbus import modbus
import streams

RS485EN  = D40
LED_R    = D47
LED_G    = D54
LED_B    = D55

# add pins definition for the expander
pinmap = {
    LED_R   : 7, # LED R mapped to pin 7 on sx1503
    LED_G   : 14, # LED G mapped to pin 14 on sx1503
    LED_B   : 15, # LED B mapped to pin 15 on sx1503
    RS485EN : 0, # RS485 pin trasmit/recieve
}

streams.serial()

try:
    port_expander = sx1503.SX1503(I2C0, 400000)
    gpio.add_expander(1, port_expander, pinmap)
    
    config_serial = modbus.ConfigSerial(SERIAL1, 9600, rs485en=RS485EN)
    master = modbus.ModbusSerial(1, cfg = config_serial)
    
    register = master.write_register(2, 10)
    print("Value written in the register: ", register)
    
    holding = master.read_holding(2, 1)
    print("Value of holding register read: ", holding)

    result = master.write_multiple_registers(3, 4, [55, 555, 123, 42])
    print(result, "registers written")
    
    holding = master.read_holding(2, 5)
    print("Values of holding register read: ", holding)
    
    coil = master.read_coils(1, 1)
    print("Status of coil read: ", coil)
    
    discrete = master.read_discrete(1, 1)
    print("Status of discrete register read: ", discrete)

    master.close()
    print("Closed.")
	
except Exception as e:
	print("Exception ", e)