Skip to content

Commit 8897636

Browse files
authored
Merge pull request #28 from rohieb/patch/documentation
improve documentation
2 parents 444b8f4 + a1c1271 commit 8897636

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

README.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ Usage
1818
Create a function to get and put character data (to a serial line for
1919
example)::
2020

21+
>>> import serial
2122
>>> from xmodem import XMODEM
23+
>>> ser = serial.Serial('/dev/ttyUSB0', timeout=0) # or whatever port you need
2224
>>> def getc(size, timeout=1):
23-
... return data or None
25+
... return ser.read(size) or None
2426
...
2527
>>> def putc(data, timeout=1):
26-
... return size or None
28+
... return ser.write(data) # note that this ignores the timeout
2729
...
2830
>>> modem = XMODEM(getc, putc)
2931

xmodem/__init__.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,32 @@
133133

134134
class XMODEM(object):
135135
'''
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.
138138
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
139144
>>> def getc(size, timeout=1):
140-
... return data or None
145+
... return ser.read(size) or None
141146
...
142147
>>> def putc(data, timeout=1):
143-
... return size or None
148+
... return ser.write(data) or None
144149
...
145150
>>> modem = XMODEM(getc, putc)
146151
147152
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.
149157
: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.
151162
:type putc: callable
152163
:param mode: XMODEM protocol mode
153164
:type mode: string
@@ -202,6 +213,11 @@ def __init__(self, getc, putc, mode='xmodem', pad=b'\x1a'):
202213
def abort(self, count=2, timeout=60):
203214
'''
204215
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
205221
'''
206222
for _ in range(count):
207223
self.putc(CAN, timeout)
@@ -377,6 +393,22 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0):
377393
378394
Returns the number of bytes received on success or ``None`` in case of
379395
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+
380412
'''
381413

382414
# initiate protocol

0 commit comments

Comments
 (0)