一 前记
程序员就是不断地砌砖头,然后用砖头,把砖头模块化,用的时候直接调用,能够节省很多精力。
二 实例:
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 |
![]() |