[ARM+Linux] 基于wiringPi库的串口通信

article/2025/10/27 11:45:22

wiringOP-master/examples/serialTest.c中,wiringPi库中自带的串口程序:

#include <stdio.h>
#include <string.h>
#include <errno.h>#include <wiringPi.h>
#include <wiringSerial.h>int main ()
{int fd ;int count ;unsigned int nextTime ;if ((fd = serialOpen ("/dev/ttyS2", 115200)) < 0)//打开在/dev.ttyS2路径下的文件,波特率配置成115200,如果返回值为-1说明打开失败{fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;return 1 ;}if (wiringPiSetup () == -1)//初始化wiringPi库{fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;return 1 ;}nextTime = millis () + 300 //这个函数返回 一个 从你的程序执行 wiringPiSetup 初始化函数(或者wiringPiSetupGpio ) 到 当前时间 经过的 毫秒数。返回类型是unsigned int,最大可记录 大约49天的毫秒时长。for (count = 0 ; count < 256 ; ){if (millis () > nextTime){printf ("\nOut: %3d: ", count) ;fflush (stdout) ;//fflush(stdout)刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上。serialPutchar (fd, count) ;//将字符输出到串口nextTime += 300 ;++count ;}delay (3) ;while (serialDataAvail (fd)){printf (" -> %3d", serialGetchar (fd)) ;//获取串口的数据fflush (stdout) ;}}printf ("\n") ;return 0 ;

(90条消息) fflush(stdio)、fflush(stdout)详解_hanxp001的博客-CSDN博客

根据官方的wiringPi库修改接收和发送串口数据

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>
#include <unistd.h>
int fd ;void* SendHandler()
{char *SendBuf;SendBuf = (char *)malloc(32*sizeof(32));while(1){memset(SendBuf,'\0',32);scanf("%s",SendBuf);while(*SendBuf != NULL){serialPutchar(fd, *SendBuf++);}}
}void* RevHandler()
{while(1){while (serialDataAvail(fd)){printf ("%c", serialGetchar(fd)) ;fflush (stdout) ;}}}int main ()
{int count ;unsigned int nextTime ;pthread_t idsend;//定义线程标识符pthread_t idrev;//定义线程标识符if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0){fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;return 1 ;}pthread_create(&idsend,NULL,SendHandler,NULL);//创建线程1发送数据pthread_create(&idrev,NULL,RevHandler,NULL);//创建线程2接收数据if (wiringPiSetup () == -1){fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;return 1 ;}while(1){sleep(10);}printf ("\n") ;return 0 ;
}

 修改过后通过创建线程来接收和发送串口数据

    pthread_create(&idsend,NULL,SendHandler,NULL);//创建线程1发送数据
    pthread_create(&idrev,NULL,RevHandler,NULL);//创建线程2接收数据

(90条消息) Linux 线程_TX564的博客-CSDN博客

接收数据通过调用wiringPi库自带函数 serialGetchar(fd)来获取上位机通过串口发送的数据

通过scanf获取来自键盘输入的数据再通过serialPutchar(fd, *SendBuf++);发送数据

 (90条消息) wiringPI库_LEO-max的博客-CSDN博客

基于C库的原生开发串口主要是靠一个结构体实现所有的启动位,停止位,数据位,奇偶校验,波特率

 

根据wiringPi库自己写一个串口

先在桌面新建一个UartTool.c文件然后拖入Source Insight中编写

UartTool.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>#include "wiringSerial.h"int MyserialOpen (const char *device, const int baud)
{struct termios options ;speed_t myBaud ;int	  status, fd ;switch(baud){case 9600: myBaud = B9600;break;case 115200: myBaud = B115200;break;}if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)//打开设备return -1 ;// Get and modify current options:tcgetattr (fd, &options) ;cfmakeraw   (&options) ;cfsetispeed (&options, myBaud) ;//波特率设置,进波特率cfsetospeed (&options, myBaud) ;//波特率设置,出波特率options.c_cflag |= (CLOCAL | CREAD) ;options.c_cflag &= ~PARENB ;//设置奇偶校验位options.c_cflag &= ~CSTOPB ;//停止位options.c_cflag &= ~CSIZE ;options.c_cflag |= CS8 ;//数据位,8位options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;options.c_oflag &= ~OPOST ;options.c_cc [VMIN]  =   0 ;options.c_cc [VTIME] = 100 ;	// Ten seconds (100 deciseconds)tcsetattr (fd, TCSANOW, &options) ;ioctl (fd, TIOCMGET, &status);status |= TIOCM_DTR ;status |= TIOCM_RTS ;ioctl (fd, TIOCMSET, &status);usleep (10000) ;	// 10mSreturn fd ;
}void serialSendstring (const int fd, const char *s)
{int ret;ret = write (fd, s, strlen (s));if (ret < 0)printf("Serial Puts Error\n");}int serialGetstring(const int fd,char*buffer)
{int n_read;n_read = read(fd,buffer,32);return n_read;
}int serialDataAvail (const int fd)
{int result ;if (ioctl (fd, FIONREAD, &result) == -1)return -1 ;return result ;
}

 UartTool.h

   int MyserialOpen (const char *device, const int baud);void serialSendstring (const int fd, const char *s);int serialGetstring(const int fd,char*buffer);int serialDataAvail (const int fd);

UartTest.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include "UartTool.h"
#include <unistd.h>
#include <string.h>int fd;void* readSerial()
{char buffer[32] = {'\0'};while(1){while(serialDataAvail(fd))//从串口读取一个字节(没有可用数据会等待10s)如果有的话返回数据,没有的话返回-1{memset(buffer,'\0',sizeof(buffer));serialGetstring(fd,buffer);printf("GET->%s\n",buffer);}}
}void* sendSerial()
{char buffer[32] = {'\0'};while(1){memset(buffer,'\0',sizeof(buffer));scanf("%s",buffer);serialSendstring(fd,buffer);}
}int main(int argc,char** argv)
{char deviceName[32] = {'\0'};pthread_t readt;pthread_t sendt;if(argc < 2){printf("uage:%s /dev/ttyS?\n",argv[0]);return -1;}strcpy(deviceName,argv[1]);if((fd =MyserialOpen(deviceName,115200)) == -1){printf("open %s error!\n",deviceName);return -1;}pthread_create(&readt,NULL,readSerial,NULL);pthread_create(&sendt,NULL,sendSerial,NULL);while(1){sleep(10);}
}

通过以上操作就可以摆脱wiringPi库了 

 核心就是打开某个设备(文件),将参数配置好传给内核,内核驱动根据给的各个参数去配置硬件的寄存器,向串口写数据其实就是写文件操作,读数据其实就是读文件操作

基于串口开发一个小项目:用语音控制抖音上滑下滑点赞锁屏 

 

接线按照以上图示接线

语音模块配置,语音模块用的是SU-03T

1. 进入语音模块官网 http://www.smartpi.cn/#/ ,配置词条和识别后的串口输出指令,具体
根据视频教程

 

 

将手机用usb接口插入开发板

a . 把手机接入开发板
b . 安装 adb 工具,在终端输入 adb 安装指令: sudo apt - get install adb
c . dmeg 能查看到手机接入的信息,但是输入 adb devices 会出现提醒
dinsufficient permissions for device : user in plugdev group ; are your udev
rules wrong ?
d . 配置文件,以支持 USB 设备的热拔插,支持 UDEV 的机制
/ etc / udev / rules . d 文件夹下创建规则文件
cd / etc / udev / rules . d /
sudo vim 51 - android . rules
在文件中添加内容 SUBSYSTEM == "usb" , ENV { DEVTYPE } == "usb_device" , MODE = "0666"
e . 在手机开发者选项中,打开 USB 调试,重新拔插手机
f . 手机弹出调试提醒,点确认手机调试模式
(92条消息) adb devices后显示List of devices attached/unauthorized问题解决_踩坑记的博客-CSDN博客

 模拟手上下滑动,双击和锁屏的shell脚本

adb shell input swipe 540 1300 540 500 100 向下滑动 540 是水平的, 1300 是竖直方向,下 是
500
adb shell input swipe 540 500 540 1300 100 向上滑动
adb shell "seq 3 | while read i;do input tap 350 1050 & input tap 350 1050 &
sleep 0.01;done;" 点赞
adb shell input keyevent 26 锁屏

代码实现 

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include "uartTool.h"
int fd;
void* readSerial()
{char cmd;while(1){cmd = myserialGetchar(fd);switch(cmd){case 'N':printf("next\n");system("adb shell input swipe 540 1300 540 500 100");break;case 'P':printf("pre\n");system("adb shell input swipe 540 500 540 1300 100");break;case 'Z':printf("zan\n");system("adb shell \"seq 3 | while read i;do input tap 350 1050 &input tap 350 1050 & sleep 0.01;done;\"");break;case 'Q':printf("qu\n");system("adb shell input keyevent 26");break;}}
}
int main(int argc, char **argv)
{char deviceName[32] = {'\0'};pthread_t readt;if(argc < 2){printf("uage:%s /dev/ttyS?\n",argv[0]);return -1;}strcpy(deviceName, argv[1]);if( (fd = myserialOpen(deviceName, 115200)) == -1){printf("open %s error\n",deviceName);return -1;}pthread_create(&readt, NULL, readSerial,NULL);while(1){sleep(10);}
}

UartTool.c UartTool.h参考上面的代码


http://chatgpt.dhexx.cn/article/q9Uh5IZY.shtml

相关文章

树莓派 Raspberry Pi —— wiringPi库安装

树莓派的GPIO可以像单片机&#xff08;51单片机&#xff0c;Arduino&#xff0c;STM32等&#xff09;一样进行IO控制&#xff08;输出高、低电平&#xff0c;IIC&#xff0c;SPI&#xff0c;串口通信&#xff0c;PWM输出等&#xff09;&#xff0c;在此使用常用的WiringPi库来进…

树莓派 wiringPi 库

wiringPi是一个很棒的树莓派IO控制库&#xff0c;使用C语言开发&#xff0c;提供了丰富的接口&#xff1a;GPIO控制&#xff0c;中断&#xff0c;多线程&#xff0c;等等 检查树莓派是否已安装 wiringPi&#xff0c;在树莓派终端输入&#xff1a; gpio -v // 会在终端中输出…

树莓派安装wiringPi

在学习微雪的2-CH CAN FD HAT时&#xff0c;根据官网步骤在树莓派安装wiringPi sudo apt-get install wiringpi #对于树莓派4B可能需要进行升级&#xff1a; wget https://project-downloads.drogon.net/wiringpi-latest.deb&#xff08;此链接安装可能出错&#xff0c;如果出…

解决wiringPi库与64位树莓派之间不兼容的问题

目录 一.问题现象&#xff1a; 二.解决方案&#xff08;网站&#xff09;可以直接点这下载 一.问题现象&#xff1a; 今天在练习wiringPi库的使用时候&#xff0c;在最后编译的时候出现了这个问题 主要问题是这个skipping incompatible&#xff01; skipping incompatible. …

树莓派安装WiringPi以及找不到wiringPi.h文件解决方法(图文教程)

目录 安装WiringPi 失败的过程&#xff1a; 选择的方法&#xff1a; 安装步骤&#xff1a; 找不到wiringPi.h文件解决方法 失败过程&#xff1a; 解决方法&#xff1a; 安装WiringPi 失败的过程&#xff1a; 通过分别使用sudo apt-get install wiringPi 和 wget https…

树莓派的wiringPi库

一、wiringPi库 树莓派的外设开发都是基于wiringPi库&#xff0c;wiringPi是树莓派IO控制库&#xff0c;使用C语言开发&#xff0c;提供了丰富的接口&#xff1a;GPIO控制&#xff0c;中断&#xff0c;多线程&#xff0c;等等。 wiringPi库的安装 进入 wiringPi的github (htt…

玩转树莓派三、树莓派安装GPIO库接口wiringpi

WiringPi简介 WiringPi 是由 Gordon Henderson 使用 C 语言编写的一个基于 PIN接口的 GPIO 控制函数库&#xff0c;适用于所有Raspberry Pi 中使用的 BCM2835、BCM2836 和 BCM2837 SoC 设备&#xff0c;使用过 Arduino 控制板的开发人员应该会对此非常熟悉。 wiringPi 包括一…

树莓派学习笔记——wiringPi I2C设备使用详解

1.前言 最近认真学习了树莓派&#xff0c;从浅到深认真分析了wiringPi实现代码&#xff0c;借助树莓派学习linux收获颇丰。深入学习linux一段时间后发现它非常有魅力&#xff0c;一个简单的IO口输出操作尽有那么多的“玩法”。wiringPi是一个简单易用的函数库&#xff0c;通过w…

wiringPi简介、安装和管脚说明,官方document

https://www.cnblogs.com/lulipro/p/5992172.html WiringPi API 教程 https://www.rubydoc.info/gems/wiringpi2/2.0.1/WiringPi/GPIO 硬件初始化函数 使用wiringPi时&#xff0c;你必须在执行任何操作前初始化树莓派&#xff0c;否则程序不能正常工作。 可以调用下表函数…

树莓派4B-WiringPi库的安装和使用 (C和Python版)

本文适合&#xff1a;掌握一定的linux常用基本命令&#xff0c;有C或Python编程基础的读者。 有很多入坑树莓派的新手会Python或者C语言&#xff0c;想玩转树莓派上的GPIO硬件资源&#xff0c;但是又不知道从何下手&#xff0c;本文就是一篇让新手入门树莓派GPIO硬件编程的文章…

(转)树莓派wiringPi库详解

https://www.cnblogs.com/lulipro/p/5992172.html <div id"post_detail">树莓派wiringPi库详解 wiringPi是一个很棒的树莓派IO控制库&#xff0c;使用C语言开发&#xff0c;提供了丰富的接口&#xff1a;GPIO控制&#xff0c;中断&#xff0c;多线程&#xf…

树莓派学习笔记——wiringPi GPIO使用详解

1.前言 最近认真学习了树莓派&#xff0c;从浅到深认真分析了wiringPi实现代码&#xff0c;借助树莓派学习linux收获颇丰。深入学习linux一段时间后发现它非常有魅力&#xff0c;一个简单的IO口输出操作尽有那么多的“玩法”。wiringPi是一个简单易用的函数库&#xff0c;通过w…

树莓派在下载Wiringpi时遇到的问题1.软件包 wiringpi 没有可安装候选2.失败:域名解析暂时失败。 wget: 无法解析主机地址 “project-downloads.drogon.ne

项目场景&#xff1a; 最近无聊,在玩实验室的树莓派,在装Wringpi时遇到了一些问题,记录一下 错误1 我用的是树莓派4B,在sudo apt-get install wiringpi时报了如下错误: 正在读取软件包列表… 完成 正在分析软件包的依赖关系树… 完成 正在读取状态信息… 完成 没有可用的软件…

树莓派学习笔记——wiringPi简介、安装和管脚说明

1.WiringPi简介 WiringPi是应用于树莓派平台的GPIO控制库函数&#xff0c;WiringPi遵守GUN Lv3。wiringPi使用C或者C开发并且可以被其他语言包转&#xff0c;例如python、ruby或者PHP等。WiringPi中的函数类似于Arduino的wiring系统&#xff0c;这使得熟悉arduino的用户使用wri…

WiringPi介绍及安装方法

WiringPi介绍及安装方法 1.WiringPi简介 WiringPi是应用于树莓派平台的GPIO控制库函数&#xff0c;WiringPi遵守GUN Lv3。wiringPi使用C或者C开发并且可以被其他语言包转&#xff0c;例如python、ruby或者PHP等。WiringPi中的函数类似于Arduino的wiring系统&#xff0c;这使得…

树莓派wiringPi库详解

达者为先 师者之意 树莓派wiringPi库详解 1 WiringPi安装2 wiringPi库编译和运行3 wiringPi库API大全3.1 硬件初始化函数3.2 通用GPIO控制函数3.3 时间控制函数3.4 中断3.5 多线程3.6 softPwm软件实现的PWM3.7 串口通信3.8 shift移位寄存器芯片API3.9 树莓派硬件平台特有的API…

第七课:树莓派WiringPi库

目录 一、WiringPi库介绍 二、WiringPi安装 方法一&#xff1a; 安装git工具&#xff08;已安装跳过&#xff09;&#xff08;前提已更换国内下载源&#xff09; ①&#xff1a;输入一下指令 ​②&#xff1a; 输入指令--在线获得 WiringPi 的源代码&#xff08;2019后不…

树莓派——wiringPi库详解

文章目录 查看树莓派引脚编号wiringPi库API大全硬件初始化函数通用GPIO控制函数时间控制函数串口通信串口通信配置测试代码串口通信实例 中断多线程其他 wiringPi&#xff08;特定平台&#xff0c;特定功能接口&#xff09;库是基于c语言开发的&#xff0c;提供了丰富的接口&am…

【树莓派】了解wiringPi库、控制继电器

目录 一、wiringPi库二、继电器1、继电器介绍及接线说明2、树莓派控制继电器 一、wiringPi库 wiringPi是一个很棒的树莓派IO控制库&#xff0c;使用C语言开发&#xff0c;提供了丰富的接口&#xff1a;GPIO控制&#xff0c;中断&#xff0c;多线程等。 在树莓派命令行输入gpio…

树莓派开发—— wiringPi 库的使用

一、wiringPi 的安装 参考文献&#xff1a; https://www.cnblogs.com/lulipro/p/5992172.html 进入 wiringPi的github (https://git.drogon.net/?pwiringPi;asummary) 下载安装包。点击页面的第一个链接的右边的snapshot,下载安装压缩包。 然后进入安装包所在的目录执行以下…