一 前记
程序员就是不断地砌砖头,然后用砖头,把砖头模块化,用的时候直接调用,能够节省很多精力。
二 实例:
1 #include <stdio.h> 2 #include <unistd.h> //Used for UART 3 #include <fcntl.h> //Used for UART 4 #include <termios.h> //Used for UART 5 6 typedef unsigned char uint8; 7 8 void dump8(uint8 *buf, int len) 9 { 10 for(int icnt = 0; icnt < len; icnt++) 11 { 12 printf("0x%x,",buf[icnt]); 13 } 14 15 printf("\n\t"); 16 } 17 18 19 int main(void) 20 { 21 int data_num = 0; 22 23 printf("\nUART test\n\n"); 24 25 int uart0_filestream = -1; 26 27 uart0_filestream = open("/dev/ttyUSB3", O_RDONLY | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode 28 if (uart0_filestream == -1) 29 { 30 printf("Error - Unable to open UART. Ensure it is not in use by another application\n"); 31 return -1; 32 } 33 34 35 struct termios options; 36 tcgetattr(uart0_filestream, &options); 37 options.c_cflag = B921600 | CS8 | CLOCAL | CREAD; //<Set baud rate 38 options.c_iflag = IGNPAR; 39 options.c_oflag = 0; 40 options.c_lflag = 0; 41 tcflush(uart0_filestream, TCIFLUSH); 42 tcsetattr(uart0_filestream, TCSANOW, &options); 43 44 printf("init complete, listening...\n\n"); 45 46 47 while(1) 48 { 49 50 if (uart0_filestream != -1) 51 { 52 unsigned char buf1[2]; 53 int n = read(uart0_filestream, buf1, 2); 54 if ((n > 0) && (buf1[0] == 0xff) &&(buf1[1] == 0xff)) 55 { 56 unsigned char buf2[2]; 57 dump8(buf1,2); 58 n = read(uart0_filestream, buf2, 2); 59 dump8(buf2,2); 60 if (n > 0 && (buf2[0] == 0x40) &&(buf2[1] == 0x01)) 61 { 62 63 unsigned char buf3[640]; 64 65 data_num = read(uart0_filestream, buf3, 640); 66 67 if(data_num > 0) 68 { 69 70 dump8(buf3,data_num); 71 } 72 73 } 74 } 75 76 } 77 78 }
三 总结:
运行一下,就知道结果了。
作者:虚生 出处:https://www.cnblogs.com/dylancao/ 以音频和传感器算法为核心的智能可穿戴产品解决方案提供商 ,提供可穿戴智能软硬件解决方案的设计,开发和咨询服务。 勾搭热线:邮箱:1173496664@qq.com weixin:18019245820 市场技术对接群:347609188 |
![]() |
分类:
C/C++ languare
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战