I have a working connection with my serial device via PySerial, but I also want to transfer files via the xmodem protocol as part of my program.
Which would be the most platform-neutral way to do this? Worst case, I could close()
my serial.Serial
object in Python and use subprocess
to call upon /usr/bin/sb
, but that seems inelegant.
I'm currently on Ubuntu 9.10 and am using a USB-TTY adapter.
Any ideas?
There is xmodem module on PyPi. It takes two functions in constructor for reading and writing data, implement them to work with your opened serial port. Below is simple sample of its usage:
I'm not familiar with the details of the xmodem protocol, but one person answering the xmodem-for-python question appears to be, and has even provided what looks like a crude implementation of xmodem in Python. Perhaps you could use that, or ideas from other the answers there.
It is super simple to use the XMODEM protocol implementation found on PyPi. A few things to note about the example above is there are some things that are not needed. (Maybe this worked for the author or with a previous version of the module?)
The documentation found here is extremely helpful, so don't let it scare you. You will need a sender and a receiver obviously, and since I do not know which one the Python script will be, here are two examples I have tested and are working below. (basically copied and pasted from the examples on PyPi)
To receive from the serial device:
To send to the serial device:
The key here is to be sure the baud rate is set on both sides (defaulting here). Do NOT add any delay or sleep as this is not time based, but transaction based. the prints will allow you to see the transaction real time as the data flows in or out of the file/serial port.