Raspberry Pi hardware version: second generation B+
Raspberry Pi system version
root@raspberrypiroot@raspberrypi:~# lsb_release -a
No LSB modules are available.
Distributor ID:Raspbian
Description:Raspbian GNU/Linux 8.0 (jessie)
Release:8.0
Codename:jessie
Modify the file: vim /boot/cmdline.txt
改成下面这样
root@raspberrypi:~# cat /boot/cmdline.txt
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Reboot the Raspberry Pi:
root@raspberrypi:~# reboot
Connect the serial port of Raspberry Pi with other serial devices
Raspberry Pi GND --> GND serial device
Raspberry Pi TXD --> RXD serial device
Raspberry Pi TXD --> RXD serial device
使用pyserial
root@raspberrypi:~# apt-get install python-serial
一个字符内容"return "回显程序,加上"return "
波特率:9600
root@raspberrypi:~# vim python_serial.py
# -*- coding: utf-8 -*
import serial
import time
# 打开串口
ser = serial.Serial("/dev/ttyAMA0", 9600)
def main():
while True:
# 获得接收缓冲区字符
count = ser.inWaiting()
if count != 0:
# 读取内容并回显
recv = ser.read(count)+"....return\n\n"
ser.write(recv)
# 清空接收缓冲区
ser.flushInput()
# 必要的软件延时
time.sleep(0.1)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
if ser != None:
ser.close()
运行程序:
root@raspberrypi:~# python python_serial.py
test chart