Simulate IR and DI

46 views
Skip to first unread message

Diogo Oliveira

unread,
Jan 19, 2022, 10:46:18 AM1/19/22
to pymodbus
Hello, I'm trying to simulate a modbus server with pymodbus, and need to update the values of IR and DI registers, while the program is working. For example, it would turn on/turn off a DI and increment an IR.

Is there any way to do this without changing the code of my client device?

My guess would be to define some function codes to write to those registers, but still haven't made much progress.

Sanjay KV

unread,
Jan 19, 2022, 10:56:23 AM1/19/22
to pymo...@googlegroups.com

ಬುಧ, ಜನವರಿ 19, 2022 21:16 ದಿನಾಂಕದಂದು Diogo Oliveira <doli...@eneida.io> ಅವರು ಬರೆದಿದ್ದಾರೆ:
Hello, I'm trying to simulate a modbus server with pymodbus, and need to update the values of IR and DI registers, while the program is working. For example, it would turn on/turn off a DI and increment an IR.

Is there any way to do this without changing the code of my client device?

My guess would be to define some function codes to write to those registers, but still haven't made much progress.

--
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/d7ca6a81-5584-4d2d-90bb-3a6f9adbe0f7n%40googlegroups.com.

Diogo Oliveira

unread,
Jan 19, 2022, 11:17:34 AM1/19/22
to pymodbus
That really doesn't help me because in that example they use the writeFunction = 0x10 and readFunction = 0x03. But that, on the modbus specification, are the function codes for writing to and reading from a holding register. 
I'm looking to be able to do that to discrete inputs and input registers, as I'm trying to simulate a server that randomly updates its values.

Diogo Oliveira

unread,
Jan 20, 2022, 6:34:24 AM1/20/22
to pymodbus

I figured out a simple solution, just in case someone needs this in the future.

The way I solved this was by adding custom messages which mimic the behavior of writing to a coil or input register.
And don't forget to add your custom message as an argument when starting the server.

I followed the source code for writing to a coil: https://github.com/riptideio/pymodbus/blob/master/pymodbus/bit_write_message.py, and just changed to write to a discrete input. That way I can manipulate those registers how I want to simulate the behavior of a physical modbus server device.  
Reply all
Reply to author
Forward
0 new messages