Skip to content

Instantly share code, notes, and snippets.

@gpulido
Last active April 10, 2024 20:54
Show Gist options
  • Save gpulido/e3518ff90396ae6d4cd186fd41c9a2d1 to your computer and use it in GitHub Desktop.
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
[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
#!/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()
@hopsor
Copy link

hopsor commented Aug 10, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment