|
135 | 135 |
|
136 | 136 | class XMODEM(object): |
137 | 137 | ''' |
138 | | - XMODEM Protocol handler, expects an object to read from and an object to |
139 | | - write to. |
| 138 | + XMODEM Protocol handler, expects two callables which encapsulate the read |
| 139 | + and write operations on the underlying stream. |
140 | 140 |
|
| 141 | + Example functions for reading and writing to a serial line: |
| 142 | +
|
| 143 | + >>> import serial |
| 144 | + >>> from xmodem import XMODEM |
| 145 | + >>> ser = serial.Serial('/dev/ttyUSB0', timeout=0) # or whatever you need |
141 | 146 | >>> def getc(size, timeout=1): |
142 | | - ... return data or None |
| 147 | + ... return ser.read(size) or None |
143 | 148 | ... |
144 | 149 | >>> def putc(data, timeout=1): |
145 | | - ... return size or None |
| 150 | + ... return ser.write(data) or None |
146 | 151 | ... |
147 | 152 | >>> modem = XMODEM(getc, putc) |
148 | 153 |
|
149 | 154 |
|
150 | | - :param getc: Function to retrieve bytes from a stream |
| 155 | + :param getc: Function to retrieve bytes from a stream. The function takes |
| 156 | + the number of bytes to read from the stream and a timeout in seconds as |
| 157 | + parameters. It must return the bytes which were read, or ``None`` if a |
| 158 | + timeout occured. |
151 | 159 | :type getc: callable |
152 | | - :param putc: Function to transmit bytes to a stream |
| 160 | + :param putc: Function to transmit bytes to a stream. The function takes the |
| 161 | + bytes to be written and a timeout in seconds as parameters. It must |
| 162 | + return the number of bytes written to the stream, or ``None`` in case of |
| 163 | + a timeout. |
153 | 164 | :type putc: callable |
154 | 165 | :param mode: XMODEM protocol mode |
155 | 166 | :type mode: string |
@@ -204,6 +215,11 @@ def __init__(self, getc, putc, mode='xmodem', pad=b'\x1a'): |
204 | 215 | def abort(self, count=2, timeout=60): |
205 | 216 | ''' |
206 | 217 | Send an abort sequence using CAN bytes. |
| 218 | +
|
| 219 | + :param count: how many abort characters to send |
| 220 | + :type count: int |
| 221 | + :param timeout: timeout in seconds |
| 222 | + :type timeout: int |
207 | 223 | ''' |
208 | 224 | for _ in range(count): |
209 | 225 | self.putc(CAN, timeout) |
@@ -430,6 +446,22 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0): |
430 | 446 |
|
431 | 447 | Returns the number of bytes received on success or ``None`` in case of |
432 | 448 | failure. |
| 449 | +
|
| 450 | + :param stream: The stream object to write data to. |
| 451 | + :type stream: stream (file, etc.) |
| 452 | + :param crc_mode: XMODEM CRC mode |
| 453 | + :type crc_mode: int |
| 454 | + :param retry: The maximum number of times to try to resend a failed |
| 455 | + packet before failing. |
| 456 | + :type retry: int |
| 457 | + :param timeout: The number of seconds to wait for a response before |
| 458 | + timing out. |
| 459 | + :type timeout: int |
| 460 | + :param delay: The number of seconds to wait between resend attempts |
| 461 | + :type delay: int |
| 462 | + :param quiet: If ``True``, write transfer information to stderr. |
| 463 | + :type quiet: bool |
| 464 | +
|
433 | 465 | ''' |
434 | 466 |
|
435 | 467 | # initiate protocol |
@@ -581,7 +613,6 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0): |
581 | 613 | data = self.getc(1, timeout=1) |
582 | 614 | if data is None: |
583 | 615 | break |
584 | | - assert False, data |
585 | 616 | self.putc(NAK) |
586 | 617 | # get next start-of-header byte |
587 | 618 | char = self.getc(1, timeout) |
|
0 commit comments