Avatar of ltpitt
ltpitt
 asked on

Parsing printer ASCII data from serial on Python: need to separate prints

Hello everybody...

I am building some kind of small virtual printer with python.

I read all data coming from a serial port in ASCII format and I can save into a file the prints coming.

I'd need to find a method to correctly separate one print from the other...

Any ideas?

Here's the code so far:

#!/usr/bin/python
# get lines of text from serial port, save them to a file

from __future__ import print_function
import serial, io

addr  = '/dev/ttyUSB0'  # serial port to read data from
baud  = 115200            # baud rate for serial port
fname = 'serial.dat'   # log file to save data in
fmode = 'a'             # log file mode = append

with serial.Serial(addr,baud) as pt, open(fname,fmode) as outf:
    spb = io.TextIOWrapper(io.BufferedRWPair(pt,pt,1),
        encoding='ascii', errors='ignore', newline='\r',line_buffering=True)
    spb.readline()  # throw away first line; likely to start mid-sentence (incomplete)
    while (1):
        x = spb.readline()  # read one line of text from serial port
        print (x,end='')    # echo line of text on-screen
        outf.write(x)       # write line of text to file
        outf.flush()        # make sure it actually gets written out

Open in new window

PythonPrinters and ScannersHardware

Avatar of undefined
Last Comment
pepr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
pepr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ltpitt

ASKER
Very interesting read and shed of light, pepr!

But this is row-related or whole transmission-related?

I've read more from your suggestion and here I find a list of the characters I should look for:

https://en.wikipedia.org/wiki/ASCII#ASCII_control_characters

The problem is that I don't get an Escape Code for (example) Start of Text: how can I detect it in my data?
SOLUTION
pepr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ltpitt

ASKER
Thanks for all this precious information!
pepr

You are welcome ;)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck