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
#2
Please use proper code tags while posting your thread, it will be much easier to understand. See BBCode to know more
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 11 21,403 Sep-27-2024, 12:18 PM
Last Post: NigelHalse
  Reading an ASCII text file and parsing data... oradba4u 2 1,380 Jun-08-2024, 12:41 AM
Last Post: oradba4u
Sad problems with reading csv file. MassiJames 3 2,486 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 2,042 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 2,074 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 8,353 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Reading a file JonWayn 3 1,927 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 1,788 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 1,638 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 3,017 Nov-30-2022, 10:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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