⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c.txt

📁 linux 串口应用程序
💻 TXT
字号:
  #include   <string.h>   
  #include   <pthread.h>   
  #include   <unistd.h>   
  #include   <stdio.h>   
  #include   <stdlib.h>   
  #include   <errno.h>   
  #include   <termios.h>   
  #include   <sys/types.h>   
  #include   <sys/time.h>   
  #include   <sys/stat.h>   
  #include   <fcntl.h>   
  #include   <sys/ioctl.h>   
  #include   <signal.h>   
    
  int   open_port(int   port_num)   
  {   
      int   port_handle   =   0   ;     
      char   device_name[50]   ;   
      struct   termios   options   ;   
      int       status   ;   
    
      if(port_num   <   0   ||   port_num   >   4)   {   
          perror("bad   serial   port   number   !\n")   ;   
          exit(1)   ;   
      }   
      sprintf(device_name,   "/dev/ttyS%d",   port_num)   ;   
      port_handle   =   open(device_name,   O_RDWR   |   O_NOCTTY     |   O_NDELAY)   ;   
      if(port_handle   ==   -1)   {   
          perror("open_port   func   failed   !\n")   ;   
          exit(1)   ;   
      }   else   {   
  /*           restore   async   mode,   use   block   mode   */   
          fcntl(port_handle,   F_SETFL,   0);   
    
  /*           configure   this   serial   port   9600,   N,   8,   1   */   
          tcgetattr(port_handle,   &options)   ;   
          cfsetispeed(&options,   B9600)   ;   
          cfsetospeed(&options,   B9600)   ;   
    
  /*           start   the   read   and   local   mode     */   
          options.c_cflag   |=   (CLOCAL   |   CREAD);   
    
  /*           set   parity   and   other   options     */   
          options.c_cflag   &=   ~PARENB   ;   
          options.c_iflag   &=   ~INPCK   ;     
          options.c_cflag   &=   ~CSTOPB   ;   
          options.c_cflag   &=   ~CSIZE   ;   
          options.c_cflag   |=   CS8   ;   
    
  /*           close   hardware   flow   control     */   
          options.c_cflag   &=   ~CRTSCTS   ;   
    
  /*           close   software   flow   control     */   
          options.c_iflag   &=   ~(IXON   |   IXOFF   |   IXANY)   ;   
          options.c_iflag   &=   ~ICRNL   ;   
          options.c_cc[VMIN]   =   0   ;     
          options.c_cc[VTIME]   =   10   ;     
    
  /*           select   raw   input   mode     */   
          options.c_lflag   &=   ~(ICANON   |   ECHO   |   ECHOE   |   ISIG)   ;   
    
          /*           select   raw   output   mode   */   
          //options.c_oflag   &=   ~OPOST   ;   
    
          tcsetattr(port_handle,   TCSANOW,   &options)   ;   
      }   
      return   port_handle   ;   
  }   
    
  int   main(int   argc,   char   *   argv[])     
  {   
      int   port_handle   =   0   ;     
      unsigned   char   cmd_pkg[20]   ;   
      int   status   ;     
      int   i   ;     
    
    
  /*   open   the   serial   port   1   ---   COM1   */   
      port_handle   =   open_port(0)   ;   
    
      memset(cmd_pkg,   0x00,   sizeof(cmd_pkg))   ;   
      sprintf(cmd_pkg,   "abcdefg")   ;     
        
      /*   check   sensor   id   */   
      for(i   =   1   ;   i   <   60   ;   i++)   {   
          print_all(cmd_pkg,   strlen(cmd_pkg)   ;     
          write(port_handle,   cmd_pkg,   strlen(cmd_pkg))   ;   
          usleep(50)   ;   
      }   
      close(port_handle)   ;   
      return   0   ;   
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -