|
133 | 133 |
|
134 | 134 | class XMODEM(object): |
135 | 135 | ''' |
136 | | - XMODEM Protocol handler, expects an object to read from and an object to |
137 | | - write to. |
| 136 | + XMODEM Protocol handler, expects two callables which encapsulate the read |
| 137 | + and write operations on the underlying stream. |
138 | 138 |
|
| 139 | + Example functions for reading and writing to a serial line: |
| 140 | +
|
| 141 | + >>> import serial |
| 142 | + >>> from xmodem import XMODEM |
| 143 | + >>> ser = serial.Serial('/dev/ttyUSB0', timeout=0) # or whatever you need |
139 | 144 | >>> def getc(size, timeout=1): |
140 | | - ... return data or None |
| 145 | + ... return ser.read(size) or None |
141 | 146 | ... |
142 | 147 | >>> def putc(data, timeout=1): |
143 | | - ... return size or None |
| 148 | + ... return ser.write(data) or None |
144 | 149 | ... |
145 | 150 | >>> modem = XMODEM(getc, putc) |
146 | 151 |
|
147 | 152 |
|
148 | | - :param getc: Function to retrieve bytes from a stream |
| 153 | + :param getc: Function to retrieve bytes from a stream. The function takes |
| 154 | + the number of bytes to read from the stream and a timeout in seconds as |
| 155 | + parameters. It must return the bytes which were read, or ``None`` if a |
| 156 | + timeout occured. |
149 | 157 | :type getc: callable |
150 | | - :param putc: Function to transmit bytes to a stream |
| 158 | + :param putc: Function to transmit bytes to a stream. The function takes the |
| 159 | + bytes to be written and a timeout in seconds as parameters. It must |
| 160 | + return the number of bytes written to the stream, or ``None`` in case of |
| 161 | + a timeout. |
151 | 162 | :type putc: callable |
152 | 163 | :param mode: XMODEM protocol mode |
153 | 164 | :type mode: string |
@@ -202,6 +213,11 @@ def __init__(self, getc, putc, mode='xmodem', pad=b'\x1a'): |
202 | 213 | def abort(self, count=2, timeout=60): |
203 | 214 | ''' |
204 | 215 | Send an abort sequence using CAN bytes. |
| 216 | +
|
| 217 | + :param count: how many abort characters to send |
| 218 | + :type count: int |
| 219 | + :param timeout: timeout in seconds |
| 220 | + :type timeout: int |
205 | 221 | ''' |
206 | 222 | for _ in range(count): |
207 | 223 | self.putc(CAN, timeout) |
@@ -377,6 +393,22 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0): |
377 | 393 |
|
378 | 394 | Returns the number of bytes received on success or ``None`` in case of |
379 | 395 | failure. |
| 396 | +
|
| 397 | + :param stream: The stream object to write data to. |
| 398 | + :type stream: stream (file, etc.) |
| 399 | + :param crc_mode: XMODEM CRC mode |
| 400 | + :type crc_mode: int |
| 401 | + :param retry: The maximum number of times to try to resend a failed |
| 402 | + packet before failing. |
| 403 | + :type retry: int |
| 404 | + :param timeout: The number of seconds to wait for a response before |
| 405 | + timing out. |
| 406 | + :type timeout: int |
| 407 | + :param delay: The number of seconds to wait between resend attempts |
| 408 | + :type delay: int |
| 409 | + :param quiet: If ``True``, write transfer information to stderr. |
| 410 | + :type quiet: bool |
| 411 | +
|
380 | 412 | ''' |
381 | 413 |
|
382 | 414 | # initiate protocol |
|
0 commit comments