0

I'm trying to read a binary file into a buffer and then transmit it using pyserial/xmodem.

my test code:

send_buf = open('test.py', 'rb') 
xmInst = XMODEM(self.getc, self.putc)
xmInst.send (send_buf)  

xmodem send code:

...
data = stream.read(packet_size)
if not data:
    break/
total_packets += 1
data = data.ljust(packet_size, self.pad)  

However, when it executes that last line it says 'must be a byte string of length 1, not str'. Presumably a byte / string / unicode issue?

The xmodem package was written for python 2.7, so how do I read/pass the file in Python 3.4 such that xmodem can work with it?

ben
  • 473
  • 2
  • 9
  • 21
  • probably `self.pad` is not a single byte character but a `str`. try printing `repr(self.pad)` – mata Mar 17 '15 at 23:15
  • putting a 'print( repr(self.pad) )' just before the last line outputs: '\x1a'. So looks like it's a single byte. So if the idea is that it's trying to pad the contents of the file with 0x1A until it's a full packet size? Is there another way to do this? (BTW, I tried using 2to3 on the xmodem file but it didn't convert) – ben Mar 18 '15 at 17:03
  • Byte strings in Python 3.4 look like `b'\x1a'` (type `bytes`). `self.pad` is a Unicode string (type `str`). – Mark Tolonen Mar 19 '15 at 12:03
  • So how can I convert from byte to string or the other way around? I tried 'data = data.ljust(packet_size, self.pad.decode(encoding)), but 'str' object has no attribute 'decode'. – ben Mar 19 '15 at 23:18

0 Answers0