It is known that the data I need to read is 9926 bytes of data, and my hardware and software communication protocols are serial and Modbus ASCII. Modbus ASCII protocol does not seem to be able to read such a large amount of data at one time, can I take a packet reading approach, or do you have any better suggestions?
I'm trying to read data from my Thermopile Infrared Array, its size is 80622+6. the software protocol is Modbus ASCII, the hardware protocol is UART.
here is my code:
import serial
import time
try:
# 打开串口
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
stopbits=1,
parity='N',
bytesize=8,
timeout=1
)
while True:
# 发送Modbus ASCII请求
request_message = "51 75 65 72 79 54 0D 0A"
request_bytes = bytes.fromhex(request_message.replace(" ", ""))
print("Sent data:", request_bytes)
ser.write(request_bytes)
# 等待响应
time.sleep(20)
expected_length = 80 * 62 * 2 + 6
response_bytes = b''
while len(response_bytes) < expected_length:
remaining_bytes = expected_length - len(response_bytes)
print("Before reading response")
try:
chunk = ser.read(remaining_bytes)
len_response_bytes = len(response_bytes)
print("读到的数据:", chunk)
print("读到的数据的长度:", len_response_bytes)
except Exception as e:
print(f"Error reading response:{e}")
print("After reading response")
response_bytes += chunk
# 处理响应数据
# 显示响应数据的十六进制表示
print("Received data (hex):", response_bytes)
response_hex = ' '.join(format(byte, '02X') for byte in response_bytes)
print("Received data (hex):", response_hex)
# 间隔5秒再次读取
time.sleep(5)
except Exception as e:
print(f"Error: {e}")
finally:
# 关闭串口
ser.close()