【STM32MP157应用编程】4.串口接收、发送数据
创始人
2024-05-31 02:41:31
0

目录

串口文件

指令操作串口

程序操作串口

程序说明

程序代码

4_ChuanKou_2.c

启动交叉编译工具

编译

拷贝到开发板

测试

串口文件

在/dev目录下,存放了串口的文件。

文件名对应的串口
ttySTM0CH340
ttySTM1com2(公头)
ttySTM2com1(母头)
ttySTM3被开发板的蓝牙所使用(不可用)

 注意:文件名的命名只跟驱动有关。

指令操作串口

设置com1串口波特率为115200。

stty -F ttySTM2 115200

 向com1串口发送数据。

echo aaa > ttySTM2

 

 

 com1串口接收数据。

cat ttySTM2

 

程序操作串口

程序说明

参数1:串口文件序号。

        0:ttySTM0。

        1:ttySTM1。

        2:ttySTM2。

参数2:波特率。

程序代码

4_ChuanKou_2.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include //./xxx 1 115200int main(int argc, char *argv[])
{//检查参数个数if (argc != 3){printf("%s文件的参数个数错误!\n", argv[0]);return -1;}//检查参数1是否为纯数字char *endptr;strtol(argv[1], &endptr, 10);if ((endptr == argv[1]) || ((!isspace(*endptr)) && (*endptr != '\0'))){perror("参数1错误!\n");return -1;}endptr = NULL;//检查参数2是否为纯数字strtol(argv[2], &endptr, 10);if ((endptr == argv[2]) || ((!isspace(*endptr)) && (*endptr != '\0'))){perror("参数2错误!\n");return -1;}//检查参数1的取值范围:<3if (atol(argv[1]) >= 3){perror("参数1错误!\n");return -1;}char tty_path[100];sprintf(tty_path, "/dev/ttySTM%s", argv[1]);int fd = open(tty_path, O_RDWR | O_NOCTTY);if (fd < 0){printf("打开%s失败!\n", tty_path);return -1;}//获取串口文件属性,以便于修改相应的属性struct termios options;if (tcgetattr(fd, &options) < 0){printf("获取串口属性错误!\n");return -1;}//设置波特率if (strcmp(argv[2], "50") == 0){cfsetispeed(&options, B50); //设置串口输入波特率cfsetospeed(&options, B50); //设置串口输出波特率}else if (strcmp(argv[2], "75") == 0){cfsetispeed(&options, B75); //设置串口输入波特率cfsetospeed(&options, B75); //设置串口输出波特率}else if (strcmp(argv[2], "110") == 0){cfsetispeed(&options, B110); //设置串口输入波特率cfsetospeed(&options, B110); //设置串口输出波特率}else if (strcmp(argv[2], "134") == 0){cfsetispeed(&options, B134); //设置串口输入波特率cfsetospeed(&options, B134); //设置串口输出波特率}else if (strcmp(argv[2], "150") == 0){cfsetispeed(&options, B150); //设置串口输入波特率cfsetospeed(&options, B150); //设置串口输出波特率}else if (strcmp(argv[2], "200") == 0){cfsetispeed(&options, B200); //设置串口输入波特率cfsetospeed(&options, B200); //设置串口输出波特率}else if (strcmp(argv[2], "300") == 0){cfsetispeed(&options, B300); //设置串口输入波特率cfsetospeed(&options, B300); //设置串口输出波特率}else if (strcmp(argv[2], "600") == 0){cfsetispeed(&options, B600); //设置串口输入波特率cfsetospeed(&options, B600); //设置串口输出波特率}else if (strcmp(argv[2], "1200") == 0){cfsetispeed(&options, B1200); //设置串口输入波特率cfsetospeed(&options, B1200); //设置串口输出波特率}else if (strcmp(argv[2], "1800") == 0){cfsetispeed(&options, B1800); //设置串口输入波特率cfsetospeed(&options, B1800); //设置串口输出波特率}else if (strcmp(argv[2], "2400") == 0){cfsetispeed(&options, B2400); //设置串口输入波特率cfsetospeed(&options, B2400); //设置串口输出波特率}else if (strcmp(argv[2], "2400") == 0){cfsetispeed(&options, B2400); //设置串口输入波特率cfsetospeed(&options, B2400); //设置串口输出波特率}else if (strcmp(argv[2], "4800") == 0){cfsetispeed(&options, B4800); //设置串口输入波特率cfsetospeed(&options, B4800); //设置串口输出波特率}else if (strcmp(argv[2], "9600") == 0){cfsetispeed(&options, B9600); //设置串口输入波特率cfsetospeed(&options, B9600); //设置串口输出波特率}else if (strcmp(argv[2], "19200") == 0){cfsetispeed(&options, B19200); //设置串口输入波特率cfsetospeed(&options, B19200); //设置串口输出波特率}else if (strcmp(argv[2], "38400") == 0){cfsetispeed(&options, B38400); //设置串口输入波特率cfsetospeed(&options, B38400); //设置串口输出波特率}else if (strcmp(argv[2], "57600") == 0){cfsetispeed(&options, B57600); //设置串口输入波特率cfsetospeed(&options, B57600); //设置串口输出波特率}else if (strcmp(argv[2], "115200") == 0){cfsetispeed(&options, B115200); //设置串口输入波特率cfsetospeed(&options, B115200); //设置串口输出波特率}else if (strcmp(argv[2], "230400") == 0){cfsetispeed(&options, B230400); //设置串口输入波特率cfsetospeed(&options, B230400); //设置串口输出波特率}else if (strcmp(argv[2], "460800") == 0){cfsetispeed(&options, B460800); //设置串口输入波特率cfsetospeed(&options, B460800); //设置串口输出波特率}else if (strcmp(argv[2], "500000") == 0){cfsetispeed(&options, B500000); //设置串口输入波特率cfsetospeed(&options, B500000); //设置串口输出波特率}else if (strcmp(argv[2], "576000") == 0){cfsetispeed(&options, B576000); //设置串口输入波特率cfsetospeed(&options, B576000); //设置串口输出波特率}else if (strcmp(argv[2], "921600") == 0){cfsetispeed(&options, B921600); //设置串口输入波特率cfsetospeed(&options, B921600); //设置串口输出波特率}else if (strcmp(argv[2], "1000000") == 0){cfsetispeed(&options, B1000000); //设置串口输入波特率cfsetospeed(&options, B1000000); //设置串口输出波特率}else if (strcmp(argv[2], "1152000") == 0){cfsetispeed(&options, B1152000); //设置串口输入波特率cfsetospeed(&options, B1152000); //设置串口输出波特率}else if (strcmp(argv[2], "1500000") == 0){cfsetispeed(&options, B1500000); //设置串口输入波特率cfsetospeed(&options, B1500000); //设置串口输出波特率}else if (strcmp(argv[2], "2000000") == 0){cfsetispeed(&options, B2000000); //设置串口输入波特率cfsetospeed(&options, B2000000); //设置串口输出波特率}else if (strcmp(argv[2], "2500000") == 0){cfsetispeed(&options, B2500000); //设置串口输入波特率cfsetospeed(&options, B2500000); //设置串口输出波特率}else if (strcmp(argv[2], "3000000") == 0){cfsetispeed(&options, B3000000); //设置串口输入波特率cfsetospeed(&options, B3000000); //设置串口输出波特率}else if (strcmp(argv[2], "3500000") == 0){cfsetispeed(&options, B3500000); //设置串口输入波特率cfsetospeed(&options, B3500000); //设置串口输出波特率}else if (strcmp(argv[2], "4000000") == 0){cfsetispeed(&options, B4000000); //设置串口输入波特率cfsetospeed(&options, B4000000); //设置串口输出波特率}else{printf("波特率参数错误!\n");return -1;}//设置串口属性if (tcsetattr(fd, TCSANOW, &options) < 0){printf("设置串口属性错误!\n");return -1;}printf("设置串口完成!\n");printf("串口启动!\n");//创建进程,读写串口功能分开pid_t jin_cheng = fork();if (jin_cheng < 0){printf("创建进程错误!\n");return -1;}else if (jin_cheng == 0) //子进程{char data[1024];int num = 0;while (1){while ((num = read(fd, &data, 1024)) != 0){printf("读取到的数据为:%s\n", data);memset(data, '\0', strlen(data));num = 0;read(fd, &data, 1024); //读掉最后的停止位}}}else if (jin_cheng > 0) //父进程{char data[1024];while (1){scanf("%s", data);getchar();write(fd, data, strlen(data));memset(data, '\0', strlen(data));}}close(fd);return 0;
}

启动交叉编译工具

source /opt/st/stm32mp1/3.1-snapshot/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi

编译

${CC} -o 4_ChuanKou_2 4_ChuanKou_2.c 

拷贝到开发板

scp 4_ChuanKou_2 root@10.3.22.219:/home/root/Linux_C_YingYong_BianCheng/JiaoCheng/4_ChuanKou/

 

测试

使用com1串口进行数据收发,设置波特率为115200。

./4_ChuanKou_2 2 115200

 发送数据。

 

 接收数据。

相关内容

热门资讯

安卓系统可以放机械硬盘,存储扩... 你有没有想过,你的安卓手机或者平板,竟然可以装个机械硬盘?是的,你没听错,就是那种通常只出现在电脑里...
安卓系统好视界打不开,原因排查... 最近是不是你也遇到了这样的烦恼?你的安卓手机上那个好视界APP突然打不开啦!别急,让我来帮你一步步排...
幺妹直播安卓系统,安卓系统下的... 你有没有听说最近幺妹直播在安卓系统上大放异彩?没错,就是那个让无数网友沉迷其中的直播平台。今天,就让...
操作键位设置手机安卓系统 你有没有发现,手机上的那些操作键位设置,简直就像是我们日常生活中的小秘密武器呢?没错,就是那个小小的...
安卓系统怎么安装钱咖,轻松开启... 你有没有想过,手机里多一个赚钱的小帮手是多么美妙的事情呢?今天,就让我来带你一起探索如何在安卓系统上...
安卓系统鼠标键盘映射,安卓系统... 你有没有想过,你的安卓手机竟然也能变成一台电脑?没错,就是那种可以连接鼠标键盘,操作起来像模像样的电...
一加安卓系统耗电严重,深度剖析... 最近是不是发现你的手机电量像坐过山车一样,一会儿满电,一会儿就“嗖”地一下掉到谷底?别急,这可不是你...
汽车导航只有安卓系统吗,安卓系... 你有没有想过,为什么你的手机导航软件总是那么聪明,能带你轻松找到目的地呢?没错,这就是汽车导航的魅力...
未来的游戏推荐安卓系统,安卓系... 你有没有想过,未来的游戏世界会是什么样子呢?想象当你拿起手机,一款全新的游戏就在你的指尖跃动,那感觉...
安卓系统编程源码下载,下载与概... 你有没有想过,安卓系统背后的那些神秘代码是如何一步步编织出我们手中手机的精彩世界的?今天,就让我带你...
安卓系统ins如何设置私人,因... 你有没有发现,在Instagram上,有时候分享一些私密的内容,只想要特定的朋友看到,那可怎么办呢?...
安卓系统自带信息app,功能解... 你知道吗?手机里那些看似普通的信息应用,其实藏着不少秘密呢!今天,就让我带你一起探索一下安卓系统自带...
安卓系统看文档软件,探索高效便... 你有没有发现,手机里的安卓系统越来越强大了,各种应用层出不穷,让人眼花缭乱。今天,我就要给你安利一款...
安卓12系统oppo隐藏功能,... 你知道吗?最近安卓12系统在手机圈里可是掀起了一阵热潮呢!而作为手机界的一股清流,OPPO也悄悄地在...
塞班系统比安卓卡,为何比安卓系... 你有没有发现,手机用久了,有时候就像老牛拉车一样慢吞吞的?这不,最近有个话题在朋友圈里炒得挺热的,就...
安卓外卖系统教程视频,从入门到... 你有没有想过,点外卖竟然也能变得如此简单有趣?没错,就是那个让你在饥肠辘辘时,只需动动手指就能享受美...
mtk是安卓系统吗 你有没有想过,你的手机里那个神秘的MTK,它到底是不是安卓系统的一部分呢?别急,今天就来给你揭秘这个...
安卓系统改语言设置方法,安卓系... 你有没有发现,手机里的安卓系统突然变得陌生了呢?是不是想换一种语言来感受一下不同的文化氛围?别急,今...
电脑怎么换安卓系统恢复 你有没有想过,你的电脑突然间变成了安卓系统?别慌,这可不是什么天方夜谭,有时候,电脑系统的小变动也能...
安卓苹果系统漫画对比,操作系统... 你知道吗?在科技的世界里,两大巨头——安卓和苹果,就像是一对欢喜冤家,总是时不时地来一场“系统大战”...