树莓派+Python+pyserial 3.0实现串口通信

betball贝博app 树莓派 522 次浏览 没有评论

wget https://pypi.python.org/packages/source/p/pyserial/pyserial-3.0.tar.gz

tar zxvf pyserial-3.0.tar.gz

pi@raspberrypi:~ cd pyserial-3.0/ pi@raspberrypi:~/pyserial-3.0 python setup.py install
pi@raspberrypi:~/pyserial-3.0 lsusb      查看USB连接情况 Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter Bus 001 Device 005: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 串口连接情况 pi@raspberrypi:~/pyserial-3.0 python -m serial.tools.list_ports
/dev/ttyAMA0
/dev/ttyUSB0
2 ports found

连接串口(方法一)

python

>> import serial
>>> ser=ser=serial.Serial('/dev/ttyUSB0',115200,timeout=0.5)
>>> ser
>>> ser.isOpen()
True
>>> ser.write('\xA5\x20')

方法二

>> import serial
>>> comm=serial.Serial()
>>> comm.port="/dev/ttyUSB0"
>>> comm.baudrate=115200
>>> comm.stopbits=1
>>> comm.parity="E"
>>> comm.open()

 

发表评论

邮箱地址不会被公开。

Go