Python Forum
Reading serial data and saving to a file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Reading serial data and saving to a file (/thread-27084.html)



Reading serial data and saving to a file - Mohan - May-25-2020

Hi ,
I am trying to read a serial data and save that in a file. I could not print any data to the file.

from __future__ import print_function
import serial, time, io, datetime
from serial import Serial
import time

addr = "COM5" ## serial port to read data from
baud = 921600 ## baud rate for instrument

ser = serial.Serial(
    port = addr,\
    baudrate = baud,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
    timeout=0)


print("Connected to: " + ser.portstr)
filename="data_file.txt"
f   = open("data_file.txt", "a")

while True:
    s    = ser.readline()
    line = s.decode('utf-8').replace('\r\n','')
    time.sleep(.1)
    f.write(line+"\r\n")	# Appends output to file
Can someone help me. Thank you.


RE: Reading serial data and saving to a file - pyzyx3qwerty - May-25-2020

Please use proper code tags while posting your thread, it will be much easier to understand. See BBCode to know more