Jump to content

Read Serial Printer output with Raspberry Pi

Go to solution Solved by LewisSpring,

Looks like it's sorted from here! All I need to do is set the alarm off a couple times, work out the outputs and write some code to catch it!

Thank you ever so much @Alfihar (welcome to the fourm!), and everyone else.

Hello,

I have an Alarm System that can output messages via a serial printer.

How could I use a Raspberry Pi to read those messages that the Alarm System outputs?

Is all I need a USB Serial adapter and some code? Or do I need special software to make the Pi pretend to be a printer?

 

Thanks!

UTC/GMT (Except during BST)

Link to post
Share on other sites

6 minutes ago, RageTester said:

You would defo have more luck asking on RPi forums:

Maybe this is useful: https://www.raspberrypi.org/forums/viewtopic.php?t=216842

 

I have 3B+ myself, but I am only using it as TV replacement for mah parents...

Hi there. Thanks for that link. I do own what I think is a USB to Serial adapter, but can't get it to work on Windows (even with driver).

It does show as a Human Interface Device or similar, so maybe it's not what I think it is.

When plugged in to the alarm system, it seems to think the printer is not ready, or something along those lines.  

 

Are serial printer outputs just plain text?

 

Thanks!

UTC/GMT (Except during BST)

Link to post
Share on other sites

Most serial printer outputs I've come across will output plain ASCII text (human readable),

But this might not always be the case, hopefully this one does.

 

You'll need to get the serial adapter to show up as a COM port on Windows, or as /dev/ttyUSBx under Linux.

So you might need to find some drivers for your USB serial adapter first.

 

Note that for this to all work you'll almost certainly need a null modem/crossover serial cable (the transmit from the alarm system connects to the receive of the serial dongle), not a straight through cable (like the one connecting the alarm system to the printer).

 

I would also use the PuTTY software to try and read from it on your PC.
You might need to change the serial port settings, though hopefully it outputs at the standard 9600/8N1 (9600 baud, 8 data bits, No parity bit, 1 stop bit).

Link to post
Share on other sites

3 minutes ago, mahyar said:

pi does have serial pins on gpio ports 

if you know the speed you might just need some code to read it

Might need to be careful doing that though, as it will probably need a level shifter.

As I think the Raspberry Pi's UARTs are 3.3V vs RS232 which can be around -12V to +12V.

Link to post
Share on other sites

5 minutes ago, Alfihar said:

Might need to be careful doing that though, as it will probably need a level shifter.

As I think the Raspberry Pi's UARTs are 3.3V vs RS232 which can be around -12V to +12V.

an op amp and a few resistors will do it

if it was useful give it a like :) btw if your into linux pay a visit here

 

Link to post
Share on other sites

Hi @Alfihar/ @mahyar

I'm more likely to use a USB serial adapter, as it's a bit more versatile, and easier to set up. I also had to buy a socket for the RS232 connector, so I want to make use of that!

2 hours ago, Alfihar said:

Most serial printer outputs I've come across will output plain ASCII text (human readable),

But this might not always be the case, hopefully this one does.

 

You'll need to get the serial adapter to show up as a COM port on Windows, or as /dev/ttyUSBx under Linux.

So you might need to find some drivers for your USB serial adapter first.

 

Note that for this to all work you'll almost certainly need a null modem/crossover serial cable (the transmit from the alarm system connects to the receive of the serial dongle), not a straight through cable (like the one connecting the alarm system to the printer).

 

I would also use the PuTTY software to try and read from it on your PC.
You might need to change the serial port settings, though hopefully it outputs at the standard 9600/8N1 (9600 baud, 8 data bits, No parity bit, 1 stop bit).

Thanks for your information RE this Alfihar, I think I have this set up with the aforementioned RS232 connector.

 

 

On further inspection, the "USB Serial Adapter" I am using, is a "Cypress Semiconductor Corp. HID->COM RS232 Adapter", and shows in Windows as a "USB to Serial Mouse" adapter (or something similar). So, I'll end up buying an adapter or level shifter either way it seems.

UTC/GMT (Except during BST)

Link to post
Share on other sites

I can't find exactly what each pin on the header means (the screw terminal is labelled, so i should use that instead really), but I remember beeping it out. This is how I have it connected so far. Any ideas how (in)correct this is?

image.thumb.png.1dd591806cd91e9503f85a9e635d8d07.pngimage.thumb.png.6f8113396077329b6ae8a4cf6214eb62.pngimage.thumb.png.b753e6de1375ef86b172718d104b7bb7.png

Thanks!

 

UTC/GMT (Except during BST)

Link to post
Share on other sites

The TX and RX lines look good and are crossed over so TX <> RX, and RX <> TX which is correct.

 

If it's using flow control then the device asserts on RTS and listens on CTS, so they need to be crossed as well.

I think you'll need to swap those wires on one end.

 

The only other thing is that it's missing is the ground connection (5 GND) which should be connected.

 

The other connections on your adapter (RI, DSR, DTR and DCD) can be ignored.

 

In software you'll need to enable hardware flow control (RTS/CTS).

 

Regarding your USB serial adapter, I would try plugging it into your Raspberry Pi and see if a /dev/ttyUSBx device is created.

Link to post
Share on other sites

3 minutes ago, Alfihar said:

The TX and RX lines look good and are crossed over so TX <> RX, and RX <> TX which is correct.

 

If it's using flow control then the device asserts on RTS and listens on CTS, so they need to be crossed as well.

I think you'll need to swap those wires on one end.

 

The only other thing is that it's missing is the ground connection (5 GND) which should be connected.

 

The other connections on your adapter (RI, DSR, DTR and DCD) can be ignored.

 

In software you'll need to enable hardware flow control (RTS/CTS).

 

Regarding your USB serial adapter, I would try plugging it into your Raspberry Pi and see if a /dev/ttyUSBx device is created.

There is a ttyUSB0 device created.

image.png.105f1ca5df6ba70c2df258dd41453951.pngThis is as much settings I have on the Alarm system for serial.

Baud goes up to 57600, 5/6/7/8 Data Bits, 1/2 Stop bits, Odd/Even/No Parity

UTC/GMT (Except during BST)

Link to post
Share on other sites

Ok I'd leave those settings alone (1200 8N1).

 

If you have a graphical environment on your Raspberry Pi, I would install PuTTY on it. I think it's in the default repos.

You can then set it to a serial connection on /dev/ttyUSB0, with the settings in your screenshot, plus turn on hardware flow control.

 

If not then maybe take a look at the minicom command line application,

as I think that will be easier to configure versus trying to get screen to work.

Link to post
Share on other sites

32 minutes ago, Alfihar said:

Ok I'd leave those settings alone (1200 8N1).

 

If you have a graphical environment on your Raspberry Pi, I would install PuTTY on it. I think it's in the default repos.

You can then set it to a serial connection on /dev/ttyUSB0, with the settings in your screenshot, plus turn on hardware flow control.

 

If not then maybe take a look at the minicom command line application,

as I think that will be easier to configure versus trying to get screen to work.

I'll have a go soon - I'm having to reinstall since there was corrupted packages installs. I'll let you know how it goes with minicom - I'm on a 1B, so don't want it to be overloaded with a desktop too.

UTC/GMT (Except during BST)

Link to post
Share on other sites

Hi @Alfihar

Update: I switched to using the screw terminals instead. The RS232 light on the alarm board lit up! 

I asked it to print, and it said it was- but no output to minicom.

 

Update 2: I used cat /dev/ttyUSB0 instead, and while it did not output anything, the keypad went back to the menu instead of staying on "printing".

 

Update 3: I tried  cat < /dev/ttyUSB0 and got image.png.7065e1a07e5cb75872058b50a78adcfe.png. Must be incorrect baud etc.

UTC/GMT (Except during BST)

Link to post
Share on other sites

In your screenshot for minicom it looks like it didn't take the command line settings.

Can you try it again but change setting A to /dev/ttyUSB0 and E to 1200 8N1.

Then try getting the alarm system to print again.

 

To use cat you would need to change the port settings using stty.

Minicom should be the easier option.

Link to post
Share on other sites

1 minute ago, Alfihar said:

In your screenshot for minicom it looks like it didn't take the command line settings.

Can you try it again but change setting A to /dev/ttyUSB0 and E to 1200 8N1.

Then try getting the alarm system to print again.

 

To use cat you would need to change the port settings using stty.

Minicom should be the easier option.

No luck. Still blank.

UTC/GMT (Except during BST)

Link to post
Share on other sites

Just saw your last update on your previous post.

It's a good sign, just need to get the settings correct.

 

As cat seems to be showing some signs of life I'd try running:

stty -F /dev/ttyUSB0 1200

 

You can also run the following to check the port settings:

stty -F /dev/ttyUSB0 -a

 

Once the setting looks right try with cat again.

Link to post
Share on other sites

12 minutes ago, Alfihar said:

Just saw your last update on your previous post.

It's a good sign, just need to get the settings correct.

 

As cat seems to be showing some signs of life I'd try running:

stty -F /dev/ttyUSB0 1200

 

You can also run the following to check the port settings:

stty -F /dev/ttyUSB0 -a

 

Once the setting looks right try with cat again.

-a returns:

stty -F /dev/ttyUSB0 -a
speed 1200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc

 

UTC/GMT (Except during BST)

Link to post
Share on other sites

Yeah not a lot to go on there. The first couple of lines might be garbage.

 

But line 3 looks like it's a time, I wonder what the rest is.

Almost looks like it could be firmware version, or maybe some internal system parameters.

It's the sort of thing I'm used to seeing some serial devices output when you request their status,

but without a manual to tell you what it means I don't think we'll get much further on that.

 

How are you testing this, is it via a printer test function?

If you can it might output more human readable text if you can trigger some events, like arming/disarming the system.

 

I'd also try and use 'screen' rather than 'cat' so you can keep monitoring the serial output, I think it'll keep the rest of the port settings.

screen /dev/ttyUSB0 1200

Link to post
Share on other sites

7 minutes ago, Alfihar said:

Yeah not a lot to go on there. The first couple of lines might be garbage.

 

But line 3 looks like it's a time, I wonder what the rest is.

Almost looks like it could be firmware version, or maybe some internal system parameters.

It's the sort of thing I'm used to seeing some serial devices output when you request their status,

but without a manual to tell you what it means I don't think we'll get much further on that.

 

How are you testing this, is it via a printer test function?

If you can it might output more human readable text if you can trigger some events, like arming/disarming the system.

 

I'd also try and use 'screen' rather than 'cat' so you can keep monitoring the serial output, I think it'll keep the rest of the port settings.

screen /dev/ttyUSB0 1200

I told it to print a user list via the keypad, so should be human readable.

 

What will be the best way to take output from the alarm and act upon it? I was thinking of using python, but I haven't fully tested pyserial yet.

 

I've also got a wierd problem where the serial adapter has disappeared - and won't reappear by rebooting. Not sure why that is.

UTC/GMT (Except during BST)

Link to post
Share on other sites

Python will be a good choice for this, assuming you want to do more than just log the output.

I've not used pyserial much myself though, as I tend to be more on embedded systems with C/C++ or Qt5 using QSerialPort.

 

Once you've got the text output from the system looking correct, as there may still be some issues with settings.

Then you need to look carefully at what it is sending.

 

Look at the text in hex and check to see if there are hidden characters as they may help or cause you issues if you don't know they are there.
But probably you'll just have to read a line delimited by a carriage return, line feed. Then either split it on something like spaces or just match the whole thing.

 

It really depends on what it is outputting and what information you need from it.

 

Edit: Not sure about the serial adapter issue, does it appear in the list of USB devices if you run 'lsusb'?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×