import time
import serial
import chardet
def ser_get():
'''串口连接'''
portx = 'com3' #端口号
bps = 115200 # 波特率
timeout = None #超时时间设置,None代表永远等待操作
ser = serial.Serial(portx,bps,timeout = timeout) #打开串口,并得到串口对象
#print('串口详情参数:',ser)
return ser
def ser_read():
'''串口读取'''
print('正在读取串口内容!')
data = ''
while True:
data = ser_get().reanline() #获取串口内容
print('***********')
time1 = time.time()
t = time.ctime()
print(t,':')
print(data)
with open('D:/test.txt','a') as f:
f.writelines(t)
f.writelines(':\n')
try:
f.writelines(data.decode(encofing = 'utf-8'))
except Exception as e :
print(e)