MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / run_server

Function run_server

examples/Modbus PLC Simulator/plc_simulator.py:834–884  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

832
833
834async def run_server():
835 sim = HydraulicTestStand()
836
837 if USE_SIM_API:
838 # Registers are refreshed lazily: pymodbus calls this on every request,
839 # so each poll sees the live simulation state.
840 async def refresh_registers(_fc, _start, _address, _count, registers, _values):
841 regs = sim.get_registers()
842 registers[: len(regs)] = regs
843 return None
844
845 context = SimDevice(
846 0,
847 simdata=[
848 SimData(0, count=NUM_REGISTERS, values=0, datatype=DataType.REGISTERS)
849 ],
850 action=refresh_registers,
851 )
852 publish = None
853 else:
854 store = SlaveContext(
855 di=ModbusSequentialDataBlock(0, [0] * NUM_REGISTERS),
856 co=ModbusSequentialDataBlock(0, [0] * NUM_REGISTERS),
857 hr=ModbusSequentialDataBlock(0, [0] * NUM_REGISTERS),
858 ir=ModbusSequentialDataBlock(0, [0] * NUM_REGISTERS),
859 )
860 context = ModbusServerContext(store, single=True)
861
862 def publish(regs):
863 store.setValues(3, 0, regs)
864
865 asyncio.create_task(update_simulation(sim, publish))
866
867 print()
868 print("=" * 72)
869 print(" HYDRAULIC TEST STAND SIMULATOR")
870 print("=" * 72)
871 print(
872 f" Server: {SERVER_HOST}:{SERVER_PORT} | Update: {UPDATE_INTERVAL*1000:.0f}ms ({1/UPDATE_INTERVAL:.0f}Hz)"
873 )
874 print()
875 print(" Registers (FC03 Holding):")
876 print(" 0:E-Stop 1:Start 2:Temp(°F) 3:PSI 4:RPM 5:Valve(%)")
877 print(" 6:GPM(÷10) 7:Load(%) 8:Vibration(÷10 mm/s)")
878 print()
879 print(" Phases: STARTUP -> RUNNING -> PRESSURE_TEST -> (FAILURE -> SHUTDOWN)")
880 print(" Flags: E=E-Stop A=Alarm(>2800PSI) F=Failure")
881 print("=" * 72)
882 print()
883
884 await StartAsyncTcpServer(context=context, address=(SERVER_HOST, SERVER_PORT))
885
886
887if __name__ == "__main__":

Callers 1

plc_simulator.pyFile · 0.85

Calls 2

HydraulicTestStandClass · 0.85
update_simulationFunction · 0.85

Tested by

no test coverage detected