简述
有很多的小伙伴说用Linux树莓派来读取JY61的串口数据不知道怎么操作。今天我和大家分享下我是从三个方面分享的。1、JY61和树莓派的连接方式及VNC的使用;2、下载到树莓派的程序是如何编写的;3、结果展示
1、JY61和树莓派的连接方式及VNC的使用
1.1给树莓派 连接电源和网线
1.2连接上树莓派和JY61的模块
注意六合一串口模块拨码1为ON、拨码2为OFF,开关S1置other ;
JY61模块跟六合一串口模块引脚需要一一对应
VCC对3V3
RX对TX
TX对RX
GND对GND
1.3 下载VNC
前往官网下载VNC,官网地址:https://www.realvnc.com/en/
软件安装步骤参考官网安装步骤,在下载的文件夹VNC Viewer中打开vncviewer.exe
在这里输入树莓派的ip地址(可通过ip搜索器搜索,选择ip名为raspberrypi的ip地址)
(## IP搜索器可自行百度下载)
1.4 登陆VNC
输入账号密码, 账号:**** 密码:**** 账号密码参考自己的树莓派资料
1.5 把代码拷贝到VNC
1.6 拷贝文件位置
1.7 终端运行程序
注:右键可打开终端
终端中的Desktop文件夹即为桌面
编辑:gcc Uart61Demo.cpp -o Uart61Demo
运行:./ Uart61Demo
2 程序编写
程序一共包括串口打开部分、串口设置、JY61数据接收与处理、主程序的运行。
2.1 串口打开
int uart_open(int fd,const char *pathname)
{
fd = open(pathname, O_RDWR|O_NOCTTY);
if (-1 == fd)
{
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open %s success!\n",pathname);
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
return fd;
}
2.2 串口设置
int uart_set(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
printf("tcgetattr( fd,&oldtio) -> %d\n",tcgetattr( fd,&oldtio));
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch