Question

I am using the GhostPDL pcl6 application to print text files to PDFs. The text files contain captured text from a serial port connected to a PLC controller. Some of the print data is from an older system that used only line feed LF for end of line termination and did not provide the CR carriage return character. The results were the PDF prints had the "Stair Stepping" problem. A laser printer can be set to AUTO CR to allow the prints to print normally. However, I could find no solution to set up the -sDEVICE=pdfwrite settings in the pcl6 print command to add the auto CR.

My serial capture program is based on a modified version of pySerial tcp_serial_redirect.py.

https://github.com/jaredly/pydbgp/blob/master/symbian/serial_tcp_redirect.py

This is used on a Raspberry Pi to be used as a programmable Device Server. The serial print of forwarded to an Ethernet printer port 9100.

Along with other features, in order to capture the serial data to a text file, I added a write to file command to the program:

                if data:
                    logfile = open("/var/www/active_log.txt","a")
                    logfile.write(data)
                    logfile.close()

The problem was the text file did not have the required CR characters. The Python program in Windows creates a text file and converts the LF to LF/CR but not on the Raspberry Pi.

My solution was found in the original pyserial program:

http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/tcp_serial_redirect.py

I simplified the join and split formula.

                if data:
                    data = '\r\n'.join(data.split('\n'))
                    logfile = open("/var/www/active_log.txt","a")
                    logfile.write(data)
                    logfile.close()

After hours of searching for a solution, this one seems to be the simplest. I tried stty settings and other avenues. Perhaps this may help someone or maybe there is a better solution?

Was it helpful?

Solution

As I understand it you are trying to replace all the \n with \r\n. How about something like this?

data = data.replace('\n', '\r\n')

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top