• c 串口读写数据实例解析


    一 前记

       程序员就是不断地砌砖头,然后用砖头,把砖头模块化,用的时候直接调用,能够节省很多精力。

    二 实例:

      

     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("
    	");
    16 }
    17 
    18 
    19 int main(void)
    20 {
    21         int data_num = 0;
    22 
    23         printf("
    UART test
    
    ");
    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
    ");
    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...
    
    ");
    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
  • 相关阅读:
    Run Mac OS X on a PC
    asp:RadioButton javascript onclick event
    The SMTP server requires a secure connection or the client was not authenticated
    Mac OS could not mount diskXX with name after erase
    server does not support secure connections
    PETS 5 五级简介
    VB中的转义字符(回车、换行、Tab等)
    MAC (Mountain Lion)+Eclipse+python+Djgano+PyDve+MySQL 开发环境搭建
    Spring bean 实现生命周期的三种解决方案
    [APUE]第九章 进程关系
  • 原文地址:https://www.cnblogs.com/dylancao/p/14693874.html
Copyright © 2020-2023  润新知