You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.3 KiB
Python
86 lines
2.3 KiB
Python
import struct
|
|
import socket
|
|
import time
|
|
# commond
|
|
# 1、Start
|
|
# 2、Stop
|
|
# 3、Quit
|
|
# 4、Data
|
|
|
|
class RTDTCdataPacket(object):
|
|
def __init__(self, commond: int, PacketSerialNo: int, lis: list):
|
|
self.PacketSerialNo = PacketSerialNo
|
|
self.value = struct.pack('d' * len(lis), *lis)
|
|
self.commond = commond
|
|
print(lis)
|
|
self.buf = [
|
|
0xF312, self.commond, self.PacketSerialNo, self.value, 0xF314
|
|
]
|
|
# print(self.buf)
|
|
def packBytes(self):
|
|
packstyle = '>HHL{}sH'.format(str(len(self.value)))
|
|
# print(packstyle)
|
|
req = struct.pack(packstyle, *self.buf)
|
|
return req
|
|
|
|
|
|
class RTDTCClient(object):
|
|
def __init__(self, url):
|
|
# print(url)
|
|
host, port = url.split(':')
|
|
self._host = host
|
|
self._port = int(port)
|
|
self.socket = socket.socket()
|
|
self.packNo = 0X000001
|
|
|
|
def connect(self):
|
|
try:
|
|
self.socket.connect((self._host, self._port))
|
|
except:
|
|
pass
|
|
|
|
def start(self):
|
|
self.connect()
|
|
self.packNo = 0X000001
|
|
pack = RTDTCdataPacket(1, self.packNo, [0] * 16)
|
|
self.socket.send(pack.packBytes())
|
|
self.packNo += 1
|
|
res = self.socket.recv(8)
|
|
data = struct.unpack('>HLH', res)
|
|
return data
|
|
|
|
def stop(self):
|
|
pack = RTDTCdataPacket(2, self.packNo, [0] * 16)
|
|
self.socket.send(pack.packBytes())
|
|
self.packNo += 1
|
|
res = self.socket.recv(8)
|
|
data = struct.unpack('>HLH', res)
|
|
return data
|
|
|
|
def quit(self):
|
|
pack = RTDTCdataPacket(3, self.packNo, [0] * 16)
|
|
self.socket.send(pack.packBytes())
|
|
self.packNo += 1
|
|
res = self.socket.recv(8)
|
|
data = struct.unpack('>HLH', res)
|
|
self.socket.close()
|
|
return data
|
|
|
|
def writeData(self, lis: list):
|
|
pack = RTDTCdataPacket(4, self.packNo, lis)
|
|
self.socket.send(pack.packBytes())
|
|
self.packNo += 1
|
|
res = self.socket.recv(8)
|
|
data = struct.unpack('>HLH', res)
|
|
return data
|
|
|
|
if __name__ == '__main__':
|
|
a = RTDTCClient('127.0.0.1:6350')
|
|
# a.connect()
|
|
a.start()
|
|
# print(1)
|
|
for x in range(50):
|
|
time.sleep(1)
|
|
a.writeData([x + 0.001] * 16)
|
|
# print(2)
|
|
a.stop() |