I am reading data from 3 serial ports concurrently using pyserial and logging this data to 3 separate time-stamped text files with python. However, after some time(less than about 5 minutes), the data written to the text files is not printed correctly.
example of the accurate data:
2017-10-16 10:41:27
Sensor Reading Avg Reading/Avg 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
example of the inaccurate data:
2017-10-16 10:55:11
Sensor Readin 3 g Reading/Avg 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
here is the code I'm using:
# 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()
I have also tried using multiple scripts without threading and the same problem continues.
Any theories as to why this is happening? Any solutions?
Thanks in advance.