【全志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

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4