Serial Communication /Serial Communication
1 introduction of serial port /Serial Introduction
Serial interface (Serial Interface) is referred to as the serial port, usually COM interface, the data is sent in a single order, the communication line is simple, a pair of transmission lines can realize two-way communication, suitable for long-distance and slow transmission speed of communication. One of the most common is the standard interface for RS-232, in addition to rs-422,rs-485 and so on.
2 pyserial Environment Construction
Pyserial installation can be done through PIP, directly at the command line to enter the following command to install,
Pip Install Pyserial
3 pyserial Basic Usage
The use of pyserial can achieve Python control of the serial port, the basic use of the method is very simple, mainly for a few steps,
1. Using the serial class incoming configuration parameter, generate serial port instance;
2. Use the Write ()/read () method of the serial port instance to send and receive messages.
Note: for the serial class, the main parameters are port and baud rate, and more parameters can be viewed from the source code.
1 ImportSerial2 3Ser = serial. Serial (port='COM1', baudrate=9600)4 Print(SER.PORTSTR)5 6 defSerial_communi (Ser, msg):7 #N is the length of MSG sent8n =Ser.write (Msg.encode ())9 Print(n)Tens =ser.read (n) One Print(s)
Implementation and extension of the 4 Read_until method
In Pyserial, the Read_until method can handle only one terminator, and when multiple termination flags need to be processed, a new method needs to be redefined, referencing the Read_until function in the pyserial source code, which is implemented as follows:
- Read one character at a time and add a string to return
- Gets the n-bit character after the character, based on the terminator length n, to detect whether it is a terminator
The following code is for reference,
1 def_read_until (self):2line ="'3 whileTrue:4 Try:5c = Self.read (1)6 except (7Attributeerror, TypeError,#COM Exception8ValueError, Eoferror,#TELNET Exception9 self._connection. opencloseexception) as E:Ten #Above Exception happens when communication manually closed, One #If still should run, condition sleep here, A #else return None to exit thread - ifSelf.should_run: - self._cond_wait (Self._recv_cond) the Continue - Else: - returnNone - ifC: +Line + =C - Self._result.put (c) +Tokens =[Self._term_token, Self._abort_token, Self._cr_token, Self._lf_token] A forTokeninchTokens: at if notLine[-len (token):] = =token: - Continue - iftoken = = Self._cr_tokenortoken = =Self._lf_token: - return Line - iftoken = =Self._term_token: - self.waiter_wake_up () in self.termi_signal (True) -Self._result.save (strip=Self._term_token) toSelf.logger.debug ("TERMINATE%s"%Self._term_token) + return Line - iftoken = =Self._abort_token: the #Todo:handle This abort token encountered *Self.logger.debug ("ABORT%s"%Self._abort_token) $ PassPanax Notoginseng #When timeout would enter this else - #Else: the #return Line
Python extension interface [1]-Serial communication