import serial
import serial.tools.list_ports
import numpy as np
Datalist= []
DataLength_FS= 824
file_name= 'Rawdata_from_sensor.txt'
port_list = list(serial.tools.list_ports.comports())
if len(port_list) == 0:
print("无可用串口!")
else:
for i in range(0, len(port_list)):
print(port_list[i])
'''-------------------------------------------------'''
port_num= 'COM'+ input('Port Number = COM:')
while True:
with serial.Serial() as ser:
ser.baudrate = 921600
ser.port = port_num
ser.timeout= 3
ser.open()
Datalist.append(ser.read(DataLength_FS).hex())
npDatalist= np.array(Datalist)
ser.close()
with open(file_name, 'w+',encoding='utf-8') as rawdata_file:
rawdata_file.write(str(npDatalist))