Read data from serial port using Python

Hello everyone!

I would like to intercept data going to a serial printer using Python and read it...

If this is possible which kind of tools / resources you suggest me to use / learn?
LVL 1
ltpittAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gelonidaCommented:
Please answer / clarify each question so that it will be easier to help:

1.) you have a serial printer connected to a PC?
2.) What is your operating system?
3.) You have an application, that can print to your printer?
4.) printing is successful with that application?
5.) you want to print from that application and monitor what is going on the serial port, while the printer is connected and printing?
0
ltpittAuthor Commented:
1) Yes, the serial printer is connected to a pc
2) The pc connected to the printer is linux, the other pc I'd want to use will be probably a Raspberry Pi with Linux too
3 & 4) The pc connected to the printer can print without problems
5) No: I simply want to connect the Raspberry Pi instead of the printer and get the data instead of the printer
0
gelonidaCommented:
I don't know a lot about serial printers.
The main question is whether the printer sends data back to the PC or not.

If the printer is a passive device and does not send any data, then things should be simple.
There's a module called pyserial  (https://pypi.python.org/pypi/pyserial) , which should do the job.

Do you know the configuration of the serial printer?
baudrate
number of data bits
number of stop bits
configuration of parity bit

I would suggest to install python virtualenv on your raspberry pi.
It's better for testing / debugging / keeping your python setup clean.

so
# install virtualenv
sudo apt-get install python-virtualenv

# install python dev.  and build-essential This is normally required if you want to install python packages with C-code.
sudo apt-get install python-dev build-essential

# create a virtualenv
virtualenv ~/mypython

# activate your virtualenv. You have to do this everytime you want use this particular  virtualenv
source ~/mypython/bin/activate

# install pyserial
pip install pyserial   # if this fails, then please send the error messages.
                                    # perhaps some build dependencies are missing.


a simple script to read data and write it to the screen should look like:

#/usr/bin/env python
import serial

ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
while True:
    char = ser.read()
    if char:
        print(repr(char))

Open in new window


check http://pythonhosted.org/pyserial/shortintro.html for a short intro
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ltpittAuthor Commented:
It looks clear and straightforward!

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.