What modules should be used when using Python serial port?

Source: Internet
Author: User

If you still do not know how to use the Python serial port during the Python application process, you can refer to the following articles to have a correct understanding of the use of the Python serial port, the following is an introduction to the relevant content of the article. I hope you will have some gains.

To use the Python serial port, you must first download the relevant modules:

 
 
  1. 1. pyserial   
  2. 2. pywin32 (http://sourceforge.net/projects/pywin32/)  
  3.  

Google "Python serial port operation" keyword, find the relevant python code, send data can be assembled and processed by chr and pack, such:

 
 
  1. snd = '' 
  2. snd += chr(97)  
  3. data = 0x12345678 
  4. snd += pack.('i', data)  
  5. snd += chr(0x64)  
  6. self.l_serial.write(snd);  
  7.  

# The sent data is in hexadecimal format: 61 78 56 34 12 64 the received data uses the ord function to convert the byte content into an integer for processing. For example:

 
 
  1. if ord(recv[2])== 0x01:  

Determine whether recv [2] is 0x01. Note: This comparison cannot be performed.

 
 
  1. if recv[2] == 'a':  
  2. pass  
  3.  

Cannot be compared like this.

 
 
  1. if recv[2] == 0x97:  
  2. pass  
  3.  

The above article describes how to use the Python serial port.

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.