views:

708

answers:

4
+1  Q: 

xmodem for python

Hi All:

I am wiriting a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?

Al

+1  A: 

I think you’re stuck with rolling your own.

You might be able to use sz, which implements X/Y/ZMODEM. You could call out to the binary, or port the necessary code to Python.

ieure
+1  A: 

Here is a link to XMODEM documentation that will be useful if you have to write your own. It has detailed description of the original XMODEM, XMODEM-CRC and XMODEM-1K.

You might also find this c-code of interest.

some
A: 

You can try using SWIG to create Python bindings for the C libraries linked above (or any other C/C++ libraries you find online). That will allow you to use the same C API directly from Python.

The actual implementation will of course still be in C/C++, since SWIG merely creates bindings to the functions of interest.

codelogic
+1  A: 
def xmodem_send(serial, file):
t, anim = 0, '|/-\\'
serial.setTimeout(1)
while 1:
    if serial.read(1) != NAK:
        t = t + 1
        print anim[t%len(anim)],'\r',
        if t == 60 : return False
    else:
        break

p = 1
s = file.read(128)
while s:
    s = s + '\xFF'*(128 - len(s))
    chk = 0
    for c in s:
        chk+=ord(c)
    while 1:
        serial.write(SOH)
        serial.write(chr(p))
        serial.write(chr(255 - p))
        serial.write(s)
        serial.write(chr(chk%256))
        serial.flush()

        answer = serial.read(1)
        if  answer == NAK: continue
        if  answer == ACK: break
        return False
    s = file.read(128)
    p = (p + 1)%256
    print '.',
serial.write(EOT)
return True