factory/apps/em/cd.py

26 lines
619 B
Python

import socket
from rest_framework.exceptions import ParseError
import json
def get_tyy_data(*args):
sc = socket.socket()
try:
sc.connect((args[0], int(args[1])))
except Exception:
raise ParseError("无法连接到采集器")
sc.send(b"R")
resp = sc.recv(1024)
if len(resp) < 8:
raise ParseError("设备未启动")
try:
json_data = resp[5:-4]
json_str = json_data.decode('utf-8')
res = json.loads(json_str)
except Exception:
raise
finally:
sc.close()
return res
if __name__ == '__main__':
print(get_tyy_data())