Pyserial使用

来源:互联网 发布:linux nginx 支持php 编辑:程序博客网 时间:2024/05/16 08:57
Environment : WIN-7 64Bit
Python Ver : V3.3.2
Pyserial Ver : V2.7
1. import the lib
import serial
2. List the available ports
import serialfor i in range(50):try:ser = serial.Serial(i)print("COM"+str(i+1))ser.close()except serial.SerialException:pass

3. Send a byte

ser.write(0xAA)
4. Send a string
Greet = bytearray("Hello World", 'ascii')ser.write(Greet)
5. Send a byte array
command = [0xAA, 0x04, 0x12, 0x44]ser.write(command)
6. Get the received data count
dataLen = ser.inWaiting()
7. Read the received data
ser.read(dataLen)