Serial data logging with python

13,354

maybe you can adapt something like this to your code :

filename="myFile.txt"
datafile=open(filename, 'a')
while True:
    data = ser.readline()
    datafile.write(data)

datafile.close()
ser.close()

EDIT if everything was working well just to write in a file try :

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

addr = "COM1" ## serial port to read data from
baud = 9600 ## 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="myFile.txt"
datafile=open(filename, 'a')

## this will store each line of data
seq = []
count = 1 ## row index

while True:
    for i in ser.read():
        seq.append(i) ## convert from ACSII?
        joined_seq = ''.join(str(v) for v in seq) ## Make a string from array

        if i == '\n':
            datafile.write("Line: " + str(count) + "" + str(datetime.datetime.now()) + joined_seq) ## append a timestamp to each row of data
            seq = []
            count += 1
            break
datafile.close()
ser.close()
Share:
13,354
philiporlando
Author by

philiporlando

Updated on June 04, 2022

Comments

  • philiporlando
    philiporlando almost 2 years

    Intro:

    I need to write a small program that reads serial data in real-time and writes it to a text file. I have made some progress with reading the data, but I haven't had any success storing this information in a new file.

    Here's my code:

    from __future__ import print_function
    import serial, time, io, datetime
    from serial import Serial
    
    addr = "COM1" ## serial port to read data from
    baud = 9600 ## 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)
    
    ## this will store each line of data
    seq = []
    count = 1 ## row index
    
    while True:
        for i in ser.read():
            seq.append(i) ## convert from ACSII?
            joined_seq = ''.join(str(v) for v in seq) ## Make a string from array
    
            if i == '\n':
                print("Line: " + str(count) + "" + str(datetime.datetime.now()) + joined_seq) ## append a timestamp to each row of data
                seq = []
                count += 1
                break
    
    ser.close()
    

    Thanks!

  • philiporlando
    philiporlando about 7 years
    This is a good starting point, but your example removes the timestamp that I added. This introduced a new hurdle that I haven't managed to overcome yet. I'm looking into using numpy and the np.append function to add a timestamp column to the raw serial data instead. I'm realizing now that using the array format might be the best option for me.
  • Dadep
    Dadep about 7 years
    ok, otherwise, you can but datafile.write("stampedDataHere") after created your time stamp....
  • philiporlando
    philiporlando about 7 years
    Ok thanks, I will look into creating a stampedData object. I just realized my example code only prints the timestamp, but it doesn't append it to each row of data yet. Currently trying to figure out how to get that to work properly.
  • Dadep
    Dadep about 7 years
    stamptedData is just a name, what I mean is put where there is actually print ... something like : datafile.write("Line: " + str(count) + "" + str(datetime.datetime.now()) + joined_seq)
  • Dadep
    Dadep about 7 years
    I edit my post ... I can't try this code here in my computer.... but the main idea is here....
  • philiporlando
    philiporlando about 7 years
    Thanks for the help! I was only one line off ha! I was definitely overthinking it at first. Everything is working on my end now!