1.看最下面这张引脚图,只给我们留了一个串口,这个串口默认是关闭的,如果是要打开,则在/boot/config.txt 里面添加

#Enable Uart
enable_uart=1

 默认串口是作为console的, 在/boot/cmdline.txt里面看到:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=0cca252e-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

 我目前的需求是想将这个串口作为普通串口用,所以我需要修改cmdline.txt文件:

dwc_otg.lpm_enable=0 console=tty1 root=PARTUUID=0cca252e-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

2.用pyhton测试:

写了一个简单的测试代码serial.py,

import serial
import time

ser = serial.Serial("/dev/serial0",115200)

def main():
    while True:
        count = ser.inWaiting()
        if count != 0:
            recv = ser.read(count)
            ser.write(recv)
        ser.flushInput()
        time.sleep(0.1)


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        if ser != None:
            ser.close()

  运行./serial.py,发现不识别import,于是在文件头加了:

#!/usr/bin/python

接下来又报serial模块没有,看来还需要安装serial模块:

sudo apt-get update
sudo apt-get install python-pip
pip install pyserial

python终于运行起来了。