2

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!

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

6 Comments

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.
ok, otherwise, you can but datafile.write("stampedDataHere") after created your time stamp....
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.
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)
I edit my post ... I can't try this code here in my computer.... but the main idea is here....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.