Patching pyserial to Fix Serial Baud Rate Limitations in Linux

I ran into an interesting issue when trying to connect to a new printer with my Linux laptop.  The printer firmware was set up for 250000 baud and for some reason I couldn’t connect to it.  After a little searching, I found that this was a known issue in pyserial with newer kernels and there’s a quick fix.

This issue is equally relevant for any USB serial device at these speeds, and of note, the printer firmware I was trying to connect to was a common Arduino-based setup.

This post had the relevant steps for applying the fix. It’s worth noting, however that the path was a little different on my system (CrunchBang Waldorf).  Instead of

/usr/lib/python2.7/site-packages/serial

the correct path was

/usr/lib/python2.7/dist-packages/serial

It’s a subtle difference, but not being familiar with the structure there it took a moment to figure out.

The patch itself can be found here.

Also, in case you’re not familiar with applying patches, I’ll spand a little on step 3, “edit serialposix.py using patch” from the above link.

The file that contains the fix is a patch, which is a specifically formatted file that notes the differences from one version of a file to another.  It is a set of modifications for the file.  When you apply a patch you are applying those modifications.  The reason for using a patch rather than just grabbing a new copy of the modified file is that from one system to another or from one version to another there may be other differences between a copy someone may distribute and what’s on your system, which would likely cause things to break if you moved the entire file over.

Applying the patch is relatively painless.  As noted in the post, make sure to back up the file you’re patching in case something goes wrong.  Everything you do in this directory will have to be done with sudo because these files are owned by root.  In my case, after navigating to the directory, I backed the file up using this command:

sudo cp serialposix.py serialposics.py.bak

I also copied the patch file into this directory for convenience, both for applying the patch, and for reverting it, if necessary (it wasn’t).  That copy required sudo as well, of course.

With the patch and the file to be patched in the same directory, the patch was applied with this command:

sudo patch serialposix.py pyserial.patch

The patch completes rather instantaneously with no fanfare, but immediately after I was able to connect to the printer at 250000 bps.

For those just learning how to patch, who are unfamiliar with programming, note that in this case the last step was applying the patch, this may not always be the case, depending on the language of the code.  This works for Python code because Python is an interpreted language that is read and executed as it goes.  If you’re using a compiled language you would have to successfully compile the code before it could be tested or used.

Leave a Reply