cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 1 | ==================== |
| 2 | Short introduction |
| 3 | ==================== |
| 4 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 5 | Opening serial ports |
| 6 | ==================== |
| 7 | |
Chris Liechti | d8137cc | 2015-09-03 23:09:16 +0200 | [diff] [blame] | 8 | Open port at "9600,8,N,1", no timeout:: |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 9 | |
| 10 | >>> import serial |
Chris Liechti | d8137cc | 2015-09-03 23:09:16 +0200 | [diff] [blame] | 11 | >>> ser = serial.Serial('/dev/ttyUSB0') # open serial port |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 12 | >>> print(ser.name) # check which port was really used |
| 13 | >>> ser.write(b"hello") # write a string |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 14 | >>> ser.close() # close port |
| 15 | |
| 16 | Open named port at "19200,8,N,1", 1s timeout:: |
| 17 | |
Chris Liechti | d8137cc | 2015-09-03 23:09:16 +0200 | [diff] [blame] | 18 | >>> with serial.Serial('/dev/ttyS1', 19200, timeout=1) as ser: |
| 19 | ... x = ser.read() # read one byte |
| 20 | ... s = ser.read(10) # read up to ten bytes (timeout) |
| 21 | ... line = ser.readline() # read a '\n' terminated line |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 22 | |
Chris Liechti | d8137cc | 2015-09-03 23:09:16 +0200 | [diff] [blame] | 23 | Open port at "38400,8,E,1", non blocking HW handshaking:: |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 24 | |
Chris Liechti | d8137cc | 2015-09-03 23:09:16 +0200 | [diff] [blame] | 25 | >>> ser = serial.Serial('COM3', 38400, timeout=0, |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 26 | ... parity=serial.PARITY_EVEN, rtscts=1) |
| 27 | >>> s = ser.read(100) # read up to one hundred bytes |
| 28 | ... # or as much is in the buffer |
| 29 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 30 | Configuring ports later |
| 31 | ======================= |
| 32 | |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 33 | Get a Serial instance and configure/open it later:: |
| 34 | |
| 35 | >>> ser = serial.Serial() |
| 36 | >>> ser.baudrate = 19200 |
Chris Liechti | d8137cc | 2015-09-03 23:09:16 +0200 | [diff] [blame] | 37 | >>> ser.port = 'COM1' |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 38 | >>> ser |
| 39 | Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0) |
| 40 | >>> ser.open() |
Chris Liechti | d8137cc | 2015-09-03 23:09:16 +0200 | [diff] [blame] | 41 | >>> ser.is_open |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 42 | True |
| 43 | >>> ser.close() |
Chris Liechti | d8137cc | 2015-09-03 23:09:16 +0200 | [diff] [blame] | 44 | >>> ser.is_open |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 45 | False |
| 46 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 47 | Readline |
| 48 | ======== |
cliechti | fbe5ae7 | 2011-08-05 00:50:21 +0000 | [diff] [blame] | 49 | Be carefully when using :meth:`readline`. Do specify a timeout when opening the |
cliechti | 2297814 | 2009-07-21 01:58:11 +0000 | [diff] [blame] | 50 | serial port otherwise it could block forever if no newline character is |
cliechti | fbe5ae7 | 2011-08-05 00:50:21 +0000 | [diff] [blame] | 51 | received. Also note that :meth:`readlines` only works with a timeout. |
| 52 | :meth:`readlines` depends on having a timeout and interprets that as EOF (end |
| 53 | of file). It raises an exception if the port is not opened correctly. |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 54 | |
| 55 | Do also have a look at the example files in the examples directory in the |
| 56 | source distribution or online. |
cliechti | fbe5ae7 | 2011-08-05 00:50:21 +0000 | [diff] [blame] | 57 | |
| 58 | .. note:: |
| 59 | |
| 60 | The ``eol`` parameter for :meth:`readline` is no longer supported when |
| 61 | pySerial is run with newer Python versions (V2.6+) where the module |
| 62 | :mod:`io` is available. |
| 63 | |
| 64 | EOL |
| 65 | --- |
| 66 | To specify the EOL character for :meth:`readline` or to use universal newline |
| 67 | mode, it is advised to use io.TextIOWrapper_:: |
| 68 | |
| 69 | import serial |
| 70 | import io |
| 71 | ser = serial.serial_for_url('loop://', timeout=1) |
| 72 | sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser)) |
| 73 | |
| 74 | sio.write(unicode("hello\n")) |
| 75 | sio.flush() # it is buffering. required to get the data out *now* |
| 76 | hello = sio.readline() |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 77 | print(hello == unicode("hello\n")) |
cliechti | fbe5ae7 | 2011-08-05 00:50:21 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | .. _io.TextIOWrapper: http://docs.python.org/library/io.html#io.TextIOWrapper |
cliechti | 7bb5883 | 2011-08-05 03:55:12 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | Testing ports |
| 84 | ============= |
| 85 | Listing ports |
| 86 | ------------- |
| 87 | ``python -m serial.tools.list_ports`` will print a list of available ports. It |
| 88 | is also possible to add a regexp as first argument and the list will only |
| 89 | include entries that matched. |
| 90 | |
| 91 | .. note:: |
| 92 | |
| 93 | The enumeration may not work on all operating systems. It may be |
| 94 | incomplete, list unavailable ports or may lack detailed descriptions of the |
| 95 | ports. |
| 96 | |
| 97 | .. versionadded: 2.6 |
| 98 | |
| 99 | Accessing ports |
| 100 | --------------- |
cliechti | 4cb9466 | 2013-10-17 03:17:50 +0000 | [diff] [blame] | 101 | pySerial includes a small console based terminal program called |
Chris Liechti | 90570b9 | 2015-08-04 03:32:02 +0200 | [diff] [blame] | 102 | :ref:`miniterm`. It ca be started with ``python -m serial.tools.miniterm <port_name>`` |
cliechti | 7bb5883 | 2011-08-05 03:55:12 +0000 | [diff] [blame] | 103 | (use option ``-h`` to get a listing of all options). |