Jan-10-2022, 06:58 AM
Hi,
I am using the below code to store data on Raspberry Pi using UART whenever it receives any data. Raspberry Pi receives data continuously for 6 seconds. I want to store this data in a single file and open a new one when Raspberry Pi receives data at a later time.
But in my code, whenever Raspberry Pi receives data, a new file is getting created every second and data is being stored in each of those files instead of storing the whole data in the same file. Can someone help/suggest how I can solve the issue? Thanks.
I am using the below code to store data on Raspberry Pi using UART whenever it receives any data. Raspberry Pi receives data continuously for 6 seconds. I want to store this data in a single file and open a new one when Raspberry Pi receives data at a later time.
But in my code, whenever Raspberry Pi receives data, a new file is getting created every second and data is being stored in each of those files instead of storing the whole data in the same file. Can someone help/suggest how I can solve the issue? Thanks.
import time
import serial
import datetime
import os
ser = serial.Serial(port='/dev/serial0', baudrate=900000, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS, rtscts=1,)
while (True):
if (ser.inWaiting() > 0):
outputFilePath = os.path.join(os.path.dirname(__file__),
datetime.datetime.now().strftime("%dT%H.%M.%S") + ".csv")
text_file = open(outputFilePath, 'a')
data_str = ser.read(ser.inWaiting())
text_file.write(data_str)
#text_file.flush()
if ("D" in data_str):
text_file.close()
time.sleep(0.01)
