Communication RPi 4(Slave) and PC(Master) Modbus RTU

599 views
Skip to first unread message

Sarah Llido

unread,
Nov 2, 2020, 11:58:13 AM11/2/20
to pymodbus

Hello,

I would like to send from my PC (with Docklight or QModMaster) a value (random for the moment) via an USB-RS485 converter at an address in order to read/recuparate this value with my Raspberry (Slave) and display it on an LCD screen.

To do that, I used the “Synchronous Server Example” below: 

import ... 

EN_485 = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(EN_485,GPIO.OUT)
GPIO.output(EN_485,GPIO.LOW) 

# --------------------------------------------------------------------------- #
# configure the service logging
# --------------------------------------------------------------------------- #
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s'
' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)

def run_server():

store = ModbusSlaveContext(
di=ModbusSequentialDataBlock(0, [0]*0xFF),
co=ModbusSequentialDataBlock(0, [0]*0xFF),
hr=ModbusSequentialDataBlock(0, [0]*0xFF),
ir=ModbusSequentialDataBlock(0, [0]*0xFF))


context = ModbusServerContext(slaves=store, single=True)

# ----------------------------------------------------------------------- #
# initialize the server information
# ----------------------------------------------------------------------- #
identity = ModbusDeviceIdentification()
identity.VendorName = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/riptideio/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '2.3.0'

# RTU:
StartSerialServer(context, framer=ModbusRtuFramer, identity=identity,
port='/dev/ttyAMA0', timeout=.005, baudrate=9600)

GPIO.cleanup()

if __name__ == "__main__":
run_server()

From Docklight I send the frame in hex: 01 06 0097 0003 A077 to write at the address 97 the value “3”.

This is my shell window when I run my code and I send the frame with Docklight :


My RPi receive the frame with the good address and value.

My question is: How can I do to recuperate this value I sent from my PC to display it on my LCD screen?

Thank you 

PS: I don't have a lot of knowledges but i'm trying to learn :)

Sanju !!

unread,
Nov 4, 2020, 12:06:24 AM11/4/20
to pymo...@googlegroups.com
 You probably are looking for updating_server. Write your logic to display the incoming message in updating_writer function before writing it to the data block.

--
You received this message because you are subscribed to the Google Groups "pymodbus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pymodbus+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pymodbus/2bf3c891-dde5-4a9f-8ad1-29e44828aa11n%40googlegroups.com.

Sarah Llido

unread,
Nov 4, 2020, 10:57:50 AM11/4/20
to pymodbus
Thank you for your quick response ! 
With the Updating Server Example, it's working. 

Thank you, have a good week !

kurt.kl...@ketek.ca

unread,
Jan 12, 2021, 4:21:45 AM1/12/21
to pymodbus
I want to use a raspberry pi as a gateway from Magnum proprietary protocol to modbus TCP slave
(implemented in pyMagnum which pulls a data stream via RS485 and then parses
that stream into parameter values )  and then associate those parameters with "registers"
such that polling the modbus slave "device" transmits those register values to the master

I have a sense that updating server would be the appropriate "driver" but am not certain
and am not quite clear on how to structure the code

I am used to doing modbus traffic in a PLC environment in which the tag/Register relationship
is clear, but in this environment, I'm not clear if that data block is all of system memory or
a defined block.
Reply all
Reply to author
Forward
0 new messages