打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
串口通讯的python模块

windows installer

There is also a Windows installer for end users. It is located in the Download Page
Developers may be interested to get the source archive, because it contains examples and the readme.

Short introduction

Open port 0 at "9600,8,N,1", no timeout

.text .imp { font-weight: bold; color: red; }
  1. >>> import serial  
  2. >>> ser = serial.Serial(0)  # open first serial port  
  3. >>> print ser.portstr       # check which port was really used  
  4. >>> ser.write("hello")      # write a string  
  5. >>> ser.close()             # close port  
Open named port at "19200,8,N,1", 1s timeout
.text .imp { font-weight: bold; color: red; }
  1. >>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)  
  2. >>> x = ser.read()          # read one byte  
  3. >>> s = ser.read(10)        # read up to ten bytes (timeout)  
  4. >>> line = ser.readline()   # read a '/n' terminated line  
  5. >>> ser.close()  
Open second port at "38400,8,E,1", non blocking HW handshaking
.text .imp { font-weight: bold; color: red; }
  1. >>> ser = serial.Serial(1, 38400, timeout=0,  
  2. ...                     parity=serial.PARITY_EVEN, rtscts=1)  
  3. >>> s = ser.read(100)       # read up to one hundred bytes  
  4. ...                         # or as much is in the buffer  
Get a Serial instance and configure/open it later
.text .imp { font-weight: bold; color: red; }
  1. >>> ser = serial.Serial()  
  2. >>> ser.baudrate = 19200  
  3. >>> ser.port = 0  
  4. >>> ser  
  5. Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)  
  6. >>> ser.open()  
  7. >>> ser.isOpen()  
  8. True  
  9. >>> ser.close()  
  10. >>> ser.isOpen()  
  11. False  
Be carefully when using "readline". Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that "readlines" only works with a timeout. "readlines" depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.
Do also have a look at the example files in the examples directory in the source distribution or online.

Examples

Please look in the SVN Repository. There is an example directory where you can find a simple terminal and more.
http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/examples/

Parameters for the Serial class

.text .imp { font-weight: bold; color: red; }
  1. ser = serial.Serial(  
  2. port=None,              # number of device, numbering starts at  
  3. # zero. if everything fails, the user  
  4. # can specify a device string, note  
  5. # that this isn't portable anymore  
  6. # if no port is specified an unconfigured  
  7. # an closed serial port object is created  
  8. baudrate=9600,          # baud rate  
  9. bytesize=EIGHTBITS,     # number of databits  
  10. parity=PARITY_NONE,     # enable parity checking  
  11. stopbits=STOPBITS_ONE,  # number of stopbits  
  12. timeout=None,           # set a timeout value, None for waiting forever  
  13. xonxoff=0,              # enable software flow control  
  14. rtscts=0,               # enable RTS/CTS flow control  
  15. interCharTimeout=None   # Inter-character timeout, None to disable  
  16. )  
The port is immediately opened on object creation, if a port is given. It is not opened if port is None.
Options for read timeout:
.text .imp { font-weight: bold; color: red; }
  1. timeout=None            # wait forever  
  2. timeout=0               # non-blocking mode (return immediately on read)  
  3. timeout=x               # set timeout to x seconds (float allowed)  

Methods of Serial instances

.text .imp { font-weight: bold; color: red; }
  1. open()                  # open port  
  2. close()                 # close port immediately  
  3. setBaudrate(baudrate)   # change baud rate on an open port  
  4. inWaiting()             # return the number of chars in the receive buffer  
  5. read(size=1)            # read "size" characters  
  6. write(s)                # write the string s to the port  
  7. flushInput()            # flush input buffer, discarding all it's contents  
  8. flushOutput()           # flush output buffer, abort output  
  9. sendBreak()             # send break condition  
  10. setRTS(level=1)         # set RTS line to specified logic level  
  11. setDTR(level=1)         # set DTR line to specified logic level  
  12. getCTS()                # return the state of the CTS line  
  13. getDSR()                # return the state of the DSR line  
  14. getRI()                 # return the state of the RI line  
  15. getCD()                 # return the state of the CD line  

Attributes of Serial instances

Read Only:
.text .imp { font-weight: bold; color: red; }
  1. portstr                 # device name  
  2. BAUDRATES               # list of valid baudrates  
  3. BYTESIZES               # list of valid byte sizes  
  4. PARITIES                # list of valid parities  
  5. STOPBITS                # list of valid stop bit widths  
New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time:

.text .imp { font-weight: bold; color: red; }
  1. port                    # port name/number as set by the user  
  2. baudrate                # current baud rate setting  
  3. bytesize                # byte size in bits  
  4. parity                  # parity setting  
  5. stopbits                # stop bit with (1,2)  
  6. timeout                 # timeout setting  
  7. xonxoff                 # if Xon/Xoff flow control is enabled  
  8. rtscts                  # if hardware flow control is enabled  

Exceptions

.text .imp { font-weight: bold; color: red; }
  1. serial.SerialException  

Constants

parity:
.text .imp { font-weight: bold; color: red; }
  1.     serial.PARITY_NONE  
  2. serial.PARITY_EVEN  
  3. serial.PARITY_ODD  
stopbits:
.text .imp { font-weight: bold; color: red; }
  1. serial.STOPBITS_ONE  
  2. al.STOPBITS_TWO  
bytesize:
.text .imp { font-weight: bold; color: red; }
  1.     serial.FIVEBITS  
  2. serial.SIXBITS  
  3. serial.SEVENBITS  
  4. serial.EIGHTBITS  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用python来调试串口
用C#一步步写串口通信
Serial Communication using C# and Whidbey
Python的串口通信(pyserial)
VC++多串口控制解决方案API版
C#源码多线程多串口
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服