Python Forum
Reading serial data and saving to a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading serial data and saving to a file
#1
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.
Reply


Messages In This Thread
Reading serial data and saving to a file - by Mohan - May-25-2020, 04:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 657 Nov-16-2023, 03:41 PM
Last Post: snippsat
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,167 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Reading a file name fron a folder on my desktop Fiona 4 931 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,118 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,115 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Reading a file JonWayn 3 1,111 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 1,003 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 904 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,361 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Replace columns indexes reading a XSLX file Larry1888 2 997 Nov-18-2022, 10:16 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020