from socket import *
import struct
def bytesToFloat(h1,h2,h3,h4):
ba = bytearray()
ba.append(h1)
ba.append(h2)
ba.append(h3)
ba.append(h4)
return struct.unpack("!f",ba)[0]
def floatToBytes(f):
bs = struct.pack("f",f)
return bs[0],bs[1],bs[2],bs[3] #低字节在前
#测试数据: 1.0,100.0,200.0,300.0,400.0,500.0,600.0
# 发送7个float 数据给服务端
def XQArmTest():
tcp_client_sock = socket(AF_INET,SOCK_STREAM)
tcp_client_sock.connect(("127.0.0.1",6666))
# 是否连接成功
try:
tcp_client_sock.send("connect ok!".encode())
print("**********************************************")
print("______socket 连接成功______")
except Exception as e:
print("socket 连接失败",e.args)
while True:
sendlst = [] ## 空列表
meg = input("输入坐标信息>> 命令号,X,Y,Z,Rx,Ry,Rz:")
if meg == 'q'or meg =="Q":
tcp_client_sock.close()
print("_____关闭socket 用户退出 bye____\r\n\r\n")
break
Posdatas = meg.split(',')
for pos in Posdatas:
f_data =float(pos)
sendlst.extend(floatToBytes(f_data))
data = bytes(sendlst)
print(data)
tcp_client_sock.send(data)
if __name__ == '__main__':
XQArmTest()