0

I am reading a string from the serial port using pySerial and then writing data to a file with a time stamp. For some reason a new line is written with an empty data entry ( with the time stamp) every time I connect the serial port. I have set the write to file as append so that every time I read data from the port I can use the same file. Is there anything fundamental that I am missing in setting up the serial port? I have attached the code and the output written in the file. Thanks a lot!

sPrt = serial.Serial( port = 5, baudrate = 9600 , bytesize = 8 )
sPrt.flushInput()

while True:
        data = sPrt.readline().decode('utf-8')[:-2]
        print(data)
        dateTimeObj = datetime.now()
        timeStamp = dateTimeObj.strftime("%d-%b-%Y %H:%M")
        with open(fileName,"ab") as f:
                writer = csv.writer(f,delimiter=",")
                writer.writerow([timeStamp,data])

and the output in the file is: Here I started data logging at 14:43, disconnected the port after two data points and then connected it again at 14.44. Each time a new connection was made a line without any data got added to the saved file

16-Nov-2022 14:43,
16-Nov-2022 14:43,"A"
16-Nov-2022 14:43,"B"
16-Nov-2022 14:44,
16-Nov-2022 14:44,"A"

The output of the print(data) line is:


A
B

A

I tried to check if the data variable is a "\n" and to only write to file if it isnt but that did not seem to do anything .

Aditya
  • 3
  • 2
  • try data = data.strip(). That should remove any extra spaces before you write to the CSV. – Captain Caveman Nov 16 '22 at 21:08
  • Hi, I added the data.strip() before the print(data) command I have but it doesnt change the extra space I am getting – Aditya Nov 16 '22 at 21:21
  • where specifically is the extra space? It's not clear to me in your output. And the command is data = data.strip(). – Captain Caveman Nov 16 '22 at 21:23
  • 1
    Hi sorry, by extra space I meant there is an extra line which has the time stamp but no data ( line 1 and 4 ) in the output. Thanks I have edited the question for updating this. – Aditya Nov 16 '22 at 21:27
  • What is the output of print(data)? – Captain Caveman Nov 16 '22 at 21:29
  • the output of print(data) has an empty line when I read the serial port in Putty as I am not printing the time stamp over there. I wasnt able to add the print output here in the comment so I added it to the main question – Aditya Nov 16 '22 at 21:32
  • Ok, so you know the source of the empty line is from your data source. I don't see where in your code you are trying to remove the new line. – Captain Caveman Nov 16 '22 at 21:35
  • The data source doesnt have any empty lines as that is just writing the string to the serial port, so that is just a string every 10-15 seconds. That is what is confusing me as to where an empty line is getting added to the read on the serial port – Aditya Nov 16 '22 at 21:38
  • I see that you "tried to check if the data variable is a "\n" but I don't see it in your code. – Captain Caveman Nov 16 '22 at 21:45
  • Oh I tried a if data!='\n' before writing to file but that was not changing anything so I removed that line – Aditya Nov 16 '22 at 22:14

1 Answers1

0

I don't see the problem here, apparently data is either something like 'A' (no newline) or '' (no newline, just an empty string). In either case, .writerow() will write a full row, followed by a newline.

If you don't want newlines written to the output file:

with open(fileName, "a", newline='') as f:
    ...

I don't see it working with "ab" anyway, since the csv.writer expects to be writing to a text file, not a binary one. As do you apparently, since you called .decode()

Grismar
  • 27,561
  • 4
  • 31
  • 54