modbus_test/test_socket_server.py

31 lines
1.1 KiB
Python

import socket
import json
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
try:
data = conn.recv(1024)
if data:
req = json.loads(data)
res = {"queryData": ["ok"]}
if "queryAddr" in req.keys() and "axis-0" in req["queryAddr"]:
res["queryData"] = [-59.696, 39.438, -7.478, -58.198, -76.606, 198.533]
if "queryAddr" in req and "world-0" in req["queryAddr"]:
res["queryData"] = [643.622, -1289.604, 254.682, 124.70, 24.209, -58.492]
if req["reqType"] == "command":
res["cmdReply"] = ['ok']
conn.sendall(json.dumps(res).encode())
except Exception as e:
print(f"by {addr} error {e}")