2

I try to setup my raspberry pi 3 as a virtual printer. My scenario is the following:

Legacy System (running Windows 7) -> COM port -> cross-wired rs232 cable -> rpi rs232 extension board for GPIO -> /dev/ttyAMA0 port on latest Raspbian

I try to read the incoming printing jobs at the rpi end. I cannot get this working. When using pyserial, I can exchange data between my windows system and my rpi using the following code:

Raspberry (receiving end):

import serial

with serial.Serial(port='/dev/ttyAMA0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0) as ser:
  x = ser.readline()
  print x

Windows (sending end):

PS> [System.IO.Ports.SerialPort]::getportnames()
COM1,9600,None,8,one
PS> $port= new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one
PS> $port.open()
PS> $port.WriteLine("Hello world")
PS> $port.Close()

Result: "Hello world" is printed on the rpi's console.
Issue: However, the same COM port does not work as a "Generic / Text only" printer. No data is delivered to the rpi.

Question: What is missing on the receiving end to act as a standard printer (such as those using the ESC/POS printer control language)? It seems like Windows does not treat the rpi as a printer. What am I missing here?

2 Answers2

2

Thanks for the comments and answers which guided me into the right direction!

It turns out that the following was the issue: The Windows software which sends the printing jobs uses Hardware flow-control and the rpi does not support it out of the box with common rs232-gpio adapters such as [1].

Since I did not write the software nor was I able to decompile it, I had to turn on a COM port sniffer and observe the low-level API calls to find out what was going on.

I used HDD Serial Port Monitor [2]. The following showed up:

005163: I/O Request (DOWN): 2019-06-12 23:41:07,2618080 +0,0000064
 IOCTL_SERIAL_SET_HANDFLOW: Set handshake information
    · Control lines        = SERIAL_DTR_CONTROL | SERIAL_CTS_HANDSHAKE (0x9)
    · Flow control         = SERIAL_RTS_HANDSHAKE (0x80)
    · Xon Limit            = 256
    · Xoff Limit           = 256


005164: I/O Request (UP): 2019-06-12 23:41:07,2618112 +0,0000032
 IOCTL: IOCTL_SERIAL_SET_HANDFLOW (0x1b0064)

The SERIAL_CTS_HANDSHAKE part indicates the use of hardware flow control. Another control message which indicates the use of hardware flow control is the following one:

005178: I/O Request (UP): 2019-06-12 23:41:12,4249200 +0,0000112
 IOCTL_SERIAL_GET_COMMSTATUS: Retrieve COM status
    · Errors                    = 0
    · Hold reasons              = SERIAL_TX_WAITING_FOR_CTS (0x1)
    · In queue                  = 0
    · Out queue                 = 0
    · EOF received              = false
    · Wait for immediate        = false

The Windows PC was waiting for the CTS signal to go high (as indicated by SERIAL_TX_WAITING_FOR_CTS).

I solved this issue by shorting the CTS and RTS lines in the serial cable on the Windows PC side.

[1] https://www.reichelt.de/raspberry-pi-konverter-gpio-auf-seriell-rpi-gpio-2-rs232-p154883.html
[2] https://www.hhdsoftware.com/serial-monitor

1

Question

Why Rpi cannot pretend to be a serial printer, receiving printer data stream from WinPC?

Answer

Well, WinPC to Rpi can send and receive text over UART without any problem.

But for WinPC to print something, Win need to handshake with the printer first. For example, WinPC might say, "OK, so are you a printer?" If WinPC gets the reply from the other side such as "Yes, I am a printer, send over stuff to print". Then WinPC starts sending something.

In other words, WinPC and Rpi need to speak a printer language.

Moreover, the simplest WinPC/Rpi serial cable may consists of only 3 lines, TxD, RxD, and Ground. But many serial cables for printers may contains up to 9 lines for miniature serial cables (DB9) and up to 25 pins for standard serial cables (DB25). The extra signals beside TxD, RxD are CTS (Clear To Send), RTS (Request to send) etc. See Appendix A below for more details.

References

Epson Standard Code for Printers - Wikipedia

Hewlett-Packard Printer Command Language - Wikipedia

IBM Personal Printer Data Stream - Wikipedia

Appendices

Appendix A - Honeywell Printer Cable Example

honeywell printer cable

tlfong01
  • 4,847
  • 3
  • 12
  • 24