Skip to content

Instantly share code, notes, and snippets.

@sourceperl
Created November 5, 2024 17:07
Show Gist options
  • Save sourceperl/7361eb541c4b08960c47e58b7bb463e1 to your computer and use it in GitHub Desktop.
Save sourceperl/7361eb541c4b08960c47e58b7bb463e1 to your computer and use it in GitHub Desktop.
A basic example for serving multiple unit IDs on the same server
import time
from collections import defaultdict
from typing import Dict
from pyModbusTCP.server import DataBank, ModbusServer
class MyDataBank(DataBank):
class Data:
def __init__(self) -> None:
self.h_regs = {}
def __init__(self):
# turn off allocation of memory for standard modbus object types
# only "holding registers" space will be replaced by dynamic build values.
super().__init__(virtual_mode=True)
# public
self.uids: Dict[int, MyDataBank.Data] = defaultdict(MyDataBank.Data)
def get_holding_registers(self, address: int, number: int, srv_info: ModbusServer.ServerInfo):
# returns None if any of the registers are missing
try:
req_uid = srv_info.recv_frame.mbap.unit_id
return [self.uids[req_uid].h_regs[addr] for addr in range(address, address+number)]
except KeyError:
return
if __name__ == '__main__':
# init a custom data bank, fill it with static data for unit id #1
data_bank = MyDataBank()
data_bank.uids[1].h_regs[0] = 42
# init modbus server and start it
ModbusServer(host='localhost', port=5020, data_bank=data_bank, no_block=True).start()
# update dynamic data at @0 for unit id #2
while True:
for i in range(1_000):
data_bank.uids[2].h_regs[0] = i
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment