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!

philiporlando
  • 941
  • 4
  • 19
  • 31

1 Answers1

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()
Dadep
  • 2,796
  • 5
  • 27
  • 40
  • 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. – philiporlando Apr 12 '17 at 18:56
  • 1
    ok, otherwise, you can but `datafile.write("stampedDataHere")` after created your time stamp.... – Dadep Apr 12 '17 at 19:08
  • 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. – philiporlando Apr 12 '17 at 19:11
  • 1
    `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 Apr 12 '17 at 19:13
  • 1
    I edit my post ... I can't try this code here in my computer.... but the main idea is here.... – Dadep Apr 12 '17 at 19:16
  • 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! – philiporlando Apr 12 '17 at 21:10