使用python从3个串行端口记录的数据不一致。

时间:2017-10-16 16:30:44

标签: python pyserial

我正在使用pyserial同时从3个串口读取数据,并使用python将此数据记录到3个单独的带时间戳的文本文件中。但是,经过一段时间(少于约5分钟),写入文本文件的数据无法正确打印。

准确数据的示例:

  

2017-10-16 10:41:27

     

传感器读数平均读数/平均topLeft:1
  7.00 0.14 midLeft:2 8.00 0.25 botLeft:3 9.00 0.33 topRight:4 10.00
  0.40 midRight:5 11.00 0.45 botRight:6 12.00 0.50

不准确数据的示例:

  

2017-10-16 10:55:11

     

传感器读取3 g读数/平均topLeft:1
  7.00 0.14 midLeft:2 8.00 0.25 botLeft:3 9.00 00.50   ht:4 10.00 0.40 midRight:5 11.00
  0.45 botRight:6 12.00 0.50       0.45 botRight:6 12.00 0.50

这是我正在使用的代码:

# to use microseconds, delete ".replace(microsecond = 0)"

## includes
from __future__ import print_function
import threading, serial, time, io, datetime
from serial import Serial

device1 = "_base_station" ## device name used for filename
addr1 = "COM39" ## edit serial port here

device2 = "_collector" ## device name used for filename
addr2 = "COM40" ## edit serial port here

device3 = "_receiver" ## device name used for filename
addr3 = "COM42" ## edit serial port here

baud = 57600 ## edit baud rate here

## function for initializing serial ports
def init_port(addr, baud):
    return serial.Serial(
        port = addr,\
        baudrate = baud,\
        parity=serial.PARITY_NONE,\
        stopbits=serial.STOPBITS_ONE,\
        bytesize=serial.EIGHTBITS,\
        timeout=.01)

## initialize serial ports
ser1 = init_port(addr1, baud)
ser2 = init_port(addr2, baud)
ser3 = init_port(addr3, baud)

## print port numbers so the user knows the script is working
print("Collecting data from port: " + ser1.portstr + "\r\n") ## give status update
print("Collecting data from port: " + ser2.portstr + "\r\n") ## give status update
print("Collecting data from port: " + ser3.portstr + "\r\n") ## give status update

def handle_data(ser, device):
    while True:
        line = ser.readline() ## get line from serial port
        filename = datetime.datetime.now().strftime("%Y-%m-%d-%H") + device + ".txt" ## create filename based on device and time. updates at midnight.
        filehandle = open(filename, 'a') ## append data to file.

        if str(line) == "#\r\n": ## logging delimiter
            filehandle.write("\r\n" + str(datetime.datetime.now().replace(microsecond = 0)) + "\r\n") ## start each data entry with a timestamp
        else:
            filehandle.write(str(line)[:-1]) ## log line (strip off extra line, data is already being parsed by line)
            filehandle.flush()

        filehandle.close() ## close file
    ser.close() ## close serial port

## create threads
thread1 = threading.Thread(target = handle_data, args = [ser1, device1])
thread2 = threading.Thread(target = handle_data, args = [ser2, device2])
thread3 = threading.Thread(target = handle_data, args = [ser3, device3])

## start threads
thread1.start()
thread2.start()
thread3.start()

我也尝试过使用多个脚本而不进行线程处理,同样的问题仍在继续。

关于为什么会发生这种情况的任何理论?任何解决方案?

提前致谢。

1 个答案:

答案 0 :(得分:0)

超时看起来很快,我会延长串口的超时时间。我怀疑你得到的碎片数据来自超时超过传入数据的时间。

def init_port(addr, baud):
    return serial.Serial(
        port = addr,\
        baudrate = baud,\
        parity=serial.PARITY_NONE,\
        stopbits=serial.STOPBITS_ONE,\
        bytesize=serial.EIGHTBITS,\
        timeout=1.0)

您正在使用.readline(),它将寻找' \ n'终止,但如果超过超时,它将提前退出。