程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Controlling serial communication with pyserial component in Python

編輯:Python

1. install pyserial package
python use pyserial Component control serial port communication .
pip  install pyserial

2 . Hardware connection , Check the serial port number on the device manager .

 3. Configuration and corresponding methods

portx="COM4"
  # Baud rate , One of the standard values :50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200
  bps=115200
  # timeout ,None: Always wait for the operation ,0 To return the requested result immediately , Other values are wait timeout ( The unit is in seconds )
  timex=5
  # Open the serial port , And get the serial object
  ser=serial.Serial(portx,bps,timeout=timex)

ser.write(“hello") Is to write data to the serial port

ser.close() It's closing ser The serial port

ser.open() Will open the 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) Yes. 20 Characters


4. effect

 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved