树莓派3B+没有内置的USB转TTL功能,但您可以通过使用USB转TTL适配器来实现该功能。USB转TTL适配器是一种将USB接口转换为TTL串行通信接口的设备,可以与树莓派的GPIO引脚进行通信。
一、组件
树莓派3B+ USB转TTL模块
通过USB转TTL模块连接串口设备
二、开发步骤
1、lsusb 显示本机的USB设备列表
出现CH340就是检测到硬件设备了。
2、ls -l /dev/tty* 查看识别的串口号
出现 /dev/ttyUSB0
程序:
/*
* 树莓派串口测试程序
*/
#include <wiringSerial.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int fd;
int n_read;
char* readbuf = (char* )malloc(128);
fd = serialOpen("/dev/ttyUSB0",9600);
while(1)
{
n_read = read(fd,readbuf,128);
if(n_read > 0)
{
printf("%d\n",(int)*readbuf);
}
}
return 0;
}