【全志H616 使用标准库 完成自制串口库(分文件实现) orangepi zero2(开源)】.md updata: 23/11/07

本文主要是介绍【全志H616 使用标准库 完成自制串口库(分文件实现) orangepi zero2(开源)】.md updata: 23/11/07,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

      • H616 把玩注意:
        • Linux内核版本5.16 及以上,需手动配置i2c-3 uart5驱动
          • 配置示例
        • 分文件编译时需将每个文件一同编译 (空格隔开)
          • 例: ggc a.c b.c b.h -lpthread -lxxx..;
      • 常用命令
        • 查看驱动文件
        • 查看内核检测信息/硬件
      • 使用wiringPi库 完成串口通讯程序:
        • serialTest.c
      • 使用标准库 完成自制串口库(分文件实现):
        • uartTool.h
        • uartTool.c
        • my_uart_ttyS5_test.c

H616 把玩注意:

Linux内核版本5.16 及以上,需手动配置i2c-3 uart5驱动

uart5 即ttyS5串口通讯口

配置示例
orangepi@orangepizero2:~/y$ uname -r //查看内核版本
5.16.17-sun50iw9 //打开修改boot下的配置文件
orangepi@orangepizero2:~/y$ sudo vi  /boot/orangepiEnv.txt1 verbosity=72 bootlogo=serial3 console=both4 disp_mode=1920x1080p605 overlay_prefix=sun50i-h6166 rootdev=UUID=5e468073-a25c-4048-be42-7f5    cfc36ce257 rootfstype=ext48 overlays=i2c3 uart5  //在这里添加适配i2c-3和uart59 usbstoragequirks=0x2537:0x1066:u,0x2537:    0x1068:u
分文件编译时需将每个文件一同编译 (空格隔开)
例: ggc a.c b.c b.h -lpthread -lxxx…;
常用可封装成一个build.sh 脚本,方便编译;
例:
orangepi@orangepizero2:~/y$ cat build.sh 
gcc $1 uartTool.h uartTool.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt 
给sh脚本执行权限后,使用即可完成编译:
orangepi@orangepizero2:~/y$ ./build.sh my_uartxxx.c 

常用命令

查看驱动文件

例_串口:ls /dev/ttyS*
在这里插入图片描述

查看内核检测信息/硬件

例_usb设备:ls /dev/bus/usb

使用wiringPi库 完成串口通讯程序:

serialTest.c
/* serialTest.c */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>int fd;void* func1()
{char buf[32] = {0};char *data = buf;while(1){memset(data,'\0',32);printf("input send data to serial: \n");scanf("%s",data);//发送数据_串口int i = 0;while(*data){serialPutchar (fd, *data++) ;      }}
}void* func2()
{while(1){while(serialDataAvail(fd)){ //接收数据_串口printf("%c",serialGetchar (fd));//清空/刷新写入缓存区(标准输出流 stdout);//确保打印输出立即显示在控制台上,而不是在缓冲区中等待。fflush (stdout) ;}}
}int main ()
{unsigned int nextTime ;pthread_t t1,t2;if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0){fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;return 1 ;}pthread_create(&t1,NULL,func1,NULL); pthread_create(&t2,NULL,func2,NULL); if (wiringPiSetup () == -1){fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;return 1 ;} while(1){sleep(10);};return 0 ;
}

使用标准库 完成自制串口库(分文件实现):

uartTool.h
/* uartTool.h */
int mySerialOpen (const char *device, const int baud);
void mySerialsend (const int fd,const char* s);
int mySerialread (const int fd,char* buf);
uartTool.c
/* 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 ;default:return -2 ;}if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)return -1 ;fcntl (fd, F_SETFL, O_RDWR) ;// 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 ;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 mySerialsend (const int fd,const char* s)
{int ret;ret = write (fd, s, strlen (s));if (ret < 0){printf("Serial Puts Error\n");}
}int mySerialread (const int fd,char* buf)
{int n_read = read (fd, buf, 1);if(n_read < 0){perror("read error");}return n_read ;
}
my_uart_ttyS5_test.c
/* my_uart_ttyS5_test.c */
#include <stdio.h>
#include <string.h>
#include <errno.h>
// #include <wiringPi.h>
// #include <wiringSerial.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>#include "uartTool.h"int fd;void* func1()
{char data[32] = {0};while(1){memset(data,'\0',32);scanf("%s",data);//发送数据_串口mySerialsend (fd, data) ;     }
}void* func2()
{char buf[32] = {0};while(1){while(mySerialread(fd,buf)){ //接收数据_串口printf("%s",buf);        }}
}int main ()
{pthread_t t1,t2;if ((fd = mySerialOpen ("/dev/ttyS5", 115200)) < 0){fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;return 1 ;}pthread_create(&t1,NULL,func1,NULL); pthread_create(&t2,NULL,func2,NULL); while(1){sleep(10);};return 0 ;
}

这篇关于【全志H616 使用标准库 完成自制串口库(分文件实现) orangepi zero2(开源)】.md updata: 23/11/07的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/375766

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.