Summary:
Pyserial module:https://github.com/tbusf/pyserial
Python uses pyserial for serial communication: http://blog.csdn.net/log1100/article/details/54380325
Python module--pyserial:http://blog.csdn.net/dainiao01/article/details/5885122 for serial communication
Parameters for the Serial class
ser = serial.Serial(port=None, # number of device, numbering starts at# zero. if everything fails, the user# can specify a device string, note# that this isn‘t portable anymore# if no port is specified an unconfigured# an closed serial port object is createdbaudrate=9600, # baud ratebytesize=EIGHTBITS, # number of databitsparity=PARITY_NONE, # enable parity checkingstopbits=STOPBITS_ONE, # number of stopbitstimeout=None, # set a timeout value, None for waiting foreverxonxoff=0, # enable software flow controlrtscts=0, # enable RTS/CTS flow controlinterCharTimeout=None # Inter-character timeout, None to disable)
Methods of Serial instances
open() # open portclose() # close port immediatelysetBaudrate(baudrate) # change baud rate on an open portinWaiting() # return the number of chars in the receive bufferread(size=1) # read "size" characterswrite(s) # write the string s to the portflushInput() # flush input buffer, discarding all it‘s contentsflushOutput() # flush output buffer, abort outputsendBreak() # send break conditionsetRTS(level=1) # set RTS line to specified logic levelsetDTR(level=1) # set DTR line to specified logic levelgetCTS() # return the state of the CTS linegetDSR() # return the state of the DSR linegetRI() # return the state of the RI linegetCD() # return the state of the CD line
Attributes of Serial instances
ReadOnly
portstr # device nameBAUDRATES # list of valid baudratesBYTESIZES # list of valid byte sizesPARITIES # list of valid paritiesSTOPBITS # list of valid stop bit widths
New values can is assigned to the following attributes, the port would be reconfigured, even if it's opened at that time: (ie Enable is also reconfigured if it is turned on??? LIUB)
port # port name/number as set by the userbaudrate # current baud rate settingbytesize # byte size in bitsparity # parity settingstopbits # stop bit with (1,2)timeout # timeout settingxonxoff # if Xon/Xoff flow control is enabledrtscts # if hardware flow control is enabled
There are so many good things to look at below:
Tcp/ip–serial Bridge
This program opens a TCP/IP port. When a connection was made to that port (e.g. with Telnet) It forwards all data to the serial port and vice versa.
This example only exports a raw socket connection. The next example below gives the client much more control over the remote serial port.
- The serial port settings is set on the command line at the starting the program.
- There is no possibility to change settings from remote.
- All data is passed through As-is.
Open a Python shell
Import Serial Module
And then you can use it.
Ser = serial. Serial (0) is to open the first serial port
Print SER.PORTSTR can see the identity of the first serial port, under Windows is COM1
Ser.write ("Hello") is to write data inside the serial port
Ser.close () is the serial port that the SER represents is closed
Ser.open () will open this serial port
Ser = serial. Serial (' COM1 ', 115200) to set the baud rate, of course, there are special functions
data = Ser.read () can read one character
data = Ser.read (20) is read 20 characters
data = Ser.readline () is a read line that ends with/n and, if not/n, reads and blocks.
data = Ser.readlines () and Ser.xreadlines () both need to set the time-out period
Ser.baudrate = 9600 Set baud rate
Ser to view the status of the current serial port
Ser.isopen () see if this serial port has been opened
Import Pyserialt=serial. Serial () = 3 = 115200 t.open () t.write (Chr (0x03)) # Enter CTRL on the serial port +c
Python Serial Communication