dts-py-app/client.py

48 lines
1.1 KiB
Python
Raw Permalink Normal View History

2024-04-24 02:24:33 +00:00
import struct
import socket
# 서버의 호스트와 포트 설정
SERVER_HOST = '192.168.0.20'
SERVER_PORT = 5001
HEART_BIT = 0x48
HEART_BIT_CMD = 0xF000
HEADER_LEN = 20
STX = 0x2
ETX = 0x3
# 소켓 생성
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# 서버에 연결
s.connect((SERVER_HOST, SERVER_PORT))
while True:
# # 서버로부터 데이터 수신
data = s.recv(1024)
print('수신된 데이터:', data)
data = []
length = HEADER_LEN
payload = []
deviceId = 5
deviceSerial = 0
classType = HEART_BIT
cmd = HEART_BIT_CMD
if data is not None:
payload = data
length = HEADER_LEN + 4 * len(payload)
string_format = f"<{7+len(payload)}I"
# 서버에게 메시지 전송
s.sendall(struct.pack(
string_format,
STX,
length,
deviceId,
deviceSerial,
classType,
cmd,
*payload,
ETX,
))
print("전송 완료!!")