Python extension interface [1]-Serial communication

Source: Internet
Author: User

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:

    1. Read one character at a time and add a string to return
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.