send file using XMODEM in python

aldim picture aldim · Jul 19, 2013 · Viewed 8.6k times · Source

I'm trying to transfer a file using the XMODEM protocol I saw the solution provided in this link:Please help in using xmodem protocol but in my case, the file can be sent but not with this contents, please help me...

here is my send code :

import serial
from xmodem import XMODEM
from time import sleep

s = serial.Serial(port='/dev/ttyUSB0', baudrate=115200, bytesize=8, parity='N',     stopbits=1, timeout=None, xonxoff=0, rtscts=0)
s.open()

def getc(size, timeout=1):
    return s.read(size)
def putc(data, timeout=1):
    s.write(data)
modem = XMODEM(getc, putc)

f = open('file.txt', 'rb')
stream = f.readlines()
status = modem.send(stream, retry=8)
s.close()
stream.close()

and here is my recv code :

import serial
from xmodem import XMODEM
from time import sleep

s = serial.Serial(port='/dev/ttyUSB0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
s.open()

def getc(size, timeout=1):
    return s.read(size)
def putc(data, timeout=1):
    s.write(data)
modem = XMODEM(getc, putc)

stream = open('file.txt', 'wb')
modem.recv(stream)
s.close()

Answer

elpincho picture elpincho · Aug 7, 2013

I think you just opened the file but never read it in your send routine.

Try this:

f = open('file.txt', 'rb')

stream = f.readlines()