Last active
April 10, 2024 20:54
-
-
Save gpulido/e3518ff90396ae6d4cd186fd41c9a2d1 to your computer and use it in GitHub Desktop.
Python script to redirect data from TCP/IP to a serial port and vice versa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Innobus python tcp serial port gateway | |
After=multi-user.target | |
[Service] | |
Type=idle | |
Restart=always | |
RestartSec=3 | |
ExecStart=/usr/bin/python3 /home/pi/innobus/serial_forwarder.py | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Pymodbus Synchronous Serial Forwarder | |
-------------------------------------------------------------------------- | |
We basically set the context for the tcp serial server to be that of a | |
serial client! This is just an example of how clever you can be with | |
the data context (basically anything can become a modbus device). | |
""" | |
# --------------------------------------------------------------------------- # | |
# import the various server implementations | |
# --------------------------------------------------------------------------- # | |
from pymodbus.server.sync import StartTcpServer as StartServer | |
from pymodbus.client.sync import ModbusSerialClient as ModbusClient | |
from pymodbus.datastore.remote import RemoteSlaveContext | |
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext | |
from pymodbus.transaction import ModbusRtuFramer | |
import pymodbus.constants | |
# --------------------------------------------------------------------------- # | |
# configure the service logging | |
# --------------------------------------------------------------------------- # | |
import logging | |
logging.basicConfig() | |
log = logging.getLogger() | |
log.setLevel(logging.INFO) | |
#fh = logging.FileHandler('serial.log') | |
#fh.setLevel(logging.DEBUG) | |
#log.addHandler(fh) | |
def run_serial_forwarder(): | |
# ----------------------------------------------------------------------- # | |
# initialize the datastore(serial client) | |
# ----------------------------------------------------------------------- # | |
client = ModbusClient(method='rtu', port='/dev/ttyUSB0', baudrate=19200, parity='E') | |
store = RemoteSlaveContext(client) | |
context = ModbusServerContext(slaves=store, single=True) | |
# ----------------------------------------------------------------------- # | |
# run the server you want | |
# ----------------------------------------------------------------------- # | |
StartServer(context, address=('0.0.0.0', 5020)) | |
if __name__ == "__main__": | |
pymodbus.constants.Defaults.UnitId = 0x01 | |
run_serial_forwarder() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi @gpulido
I'm going to try this approach in my not pro Aidoo to make it manageable through Home Assistant.
I got the USB RS485 adapter to connect it to my Raspi Zero. It's the first time I work with this so I'm not really sure about the wiring. My main question is, do I need to connect the + and the gnd? Or it's just A and B ports?
Thanks in advance