创意安天

 找回密码
 注册创意安天

read tty in linux system

[复制链接]
发表于 2009-7-28 15:22 | 显示全部楼层 |阅读模式
不累的王

普通会员

发贴: 339
积分: 0
来自:
注册日期: 2006-05-26
  发表时间: 2007-01-31 16:26:22  

--------------------------------------------------------------------------------
andy 是否依然需要它?



     1  #include <stdio.h> /*标准输入输出定义*/
     2  #include <stdlib.h> /*标准函数库定义*/
     3  #include <unistd.h> /*Unix 标准函数定义*/
     4  #include <sys/types.h>
     5  #include <sys/stat.h>
     6  #include <fcntl.h> /*文件控制定义*/
     7  #include <termios.h> /*PPSIX 终端控制定义*/
     8  #include <errno.h> /*错误号定义*/
     
    10  /*********************************************************************/
    11  #define FALSE   -1
    12  #define TRUE    0
    13  /*********************************************************************/
    14  int OpenDev(char *Dev)
    15  {
    16          int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
    17          if (-1 == fd)
    18          {
    19                  perror("Can't Open Serial Port";
    20                  return -1;
    21          }
    22          else
    23                  return fd;
    24  }
   
    26  /*
    27  *@brief 设置串口通信速率
    28  *@param fd 类型 int 打开串口的文件句柄
    29  *@param speed 类型 int 串口速度
    30  *@return void
    31  */
    32  speed_t speed_arr[] = { B230400, B115200, B57600, B38400, B19200,
    33                          B9600, B4800, B2400, B1200, B300, };
    34  int name_arr[] = { 230400, 115200, 57600, 38400, 19200,
    35                          9600, 4800, 2400, 1200, 300, };
    36
    37  void set_speed(int fd, int speed)
    38  {
    39          int i;
    40          int status;
    41          struct termios Opt;
    42          tcgetattr(fd, &Opt);
    43          for ( i= 0; i < sizeof(speed_arr)/sizeof(int); i++ )
    44          {
    45                  if (speed == name_arr[i])
    46                  {
    47                          tcflush(fd, TCIOFLUSH);
    48                          cfsetispeed(&Opt, speed_arr[i]);
    49                          cfsetospeed(&Opt, speed_arr[i]);
    50                          status = tcsetattr(fd, TCSANOW, &Opt);
    51                          if (status != 0)
    52                          {
    53                                  perror("tcsetattr fd1";
    54                                  return;
    55                          }
    56                          tcflush(fd,TCIOFLUSH);
    57                  }
    58          }
    59  }
   
    61  /*
    62  *@brief 设置串口数据位,停止位和效验位
    63  *@param fd 类型 int 打开的串口文件句柄
    64  *@param databits 类型 int 数据位 取值 为 7 或者8
    65  *@param stopbits 类型 int 停止位 取值为 1 或者2
    66  *@param parity 类型 int 效验类型 取值为N,E,O,,S
    67  */
    68  int set_Parity(int fd,int databits,int stopbits,int parity)
    69  {
    70          struct termios options;
    71          if ( tcgetattr( fd,&options) != 0)
    72          {
    73                  perror("SetupSerial 1";
    74                  return(FALSE);
    75          }
    76          options.c_cflag &= ~CSIZE;
    77          switch (databits) /*设置数据位数*/
    78          {
    79                  case 7:
    80                          options.c_cflag |= CS7;
    81                          break;
    82                  case 8:
    83                          options.c_cflag |= CS8;
    84                          break;
    85                  default:
    86                          fprintf(stderr,"Unsupported data size\n";
    87                  return (FALSE);
    88          }
    89          switch (parity)
    90          {
    91                  case 'n':
    92                  case 'N':
    93                          options.c_cflag &= ~PARENB; /* Clear parity enable */
    94                          options.c_iflag &= ~INPCK; /* Enable parity checking */
    95                          break;
    96                  case 'o':
    97                  case 'O':
    98                          options.c_cflag |= (PARODD | PAREN; /* 设置为奇效验*/
    99                          options.c_iflag |= INPCK; /* Disnable parity checking */
   100                          break;
   101                  case 'e':
   102                  case 'E':
   103                          options.c_cflag |= PARENB; /* Enable parity */
   104                          options.c_cflag &= ~PARODD; /* 转换为偶效验*/
   105                          options.c_iflag |= INPCK; /* Disnable parity checking */
   106                          break;
   107                  case 'S':
   108                  case 's': /*as no parity*/
   109                          options.c_cflag &= ~PARENB;
   110                          options.c_cflag &= ~CSTOPB;
   111                          break;
   112                  default:
   113                          fprintf(stderr,"Unsupported parity\n";
   114                          return (FALSE);
   115          }
   116          /* 设置停止位*/
   117          switch (stopbits)
   118          {
   119                  case 1:
   120                          options.c_cflag &= ~CSTOPB;
   121                          break;
   122                  case 2:
   123                          options.c_cflag |= CSTOPB;
   124                          break;
   125                  default:
   126                          fprintf(stderr,"Unsupported stop bits\n";
   127                  return (FALSE);
   128          }
   129          /* Set input parity option */
   130          if (parity != 'n')
   131                  options.c_iflag |= INPCK;
   132          tcflush(fd,TCIFLUSH);
   133          options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
   134          options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
   135          if (tcsetattr(fd,TCSANOW,&options) != 0)
   136          {
   137                  perror("SetupSerial 3";
   138                  return (FALSE);
   139          }
   140          return (TRUE);
   141  }
   
   
   144  int main(int argc, char **argv)
   145  {
   146          int fd;
   147          int nread;
   148          char buff[512];
   149          char *dev = "/dev/ttyS0";
   150          fd = OpenDev(dev);
   151          if( -1 == fd )
   152          {
   153                  return 1;
   154          }
   155          printf("open %s ok\n", dev);
   156
   157          set_speed(fd,115200);
   158          if (set_Parity(fd,8,1,'N') == FALSE)
   159          {
   160                  printf("Set Parity Error\n";
   161                  exit (0);
   162          }
   163          printf("Set Parity ok\n";
   164
   165          while (1) //循环读取数据
   166          {
   167                  while((nread = read(fd, buff, 512))>0)
   168                  {
   169                          printf("\nLen %d\n",nread);
   170                          buff[nread - 1] = '\0';
   171                          printf( "%s\n", buff);
   172                  }
   173          }
   174          close(fd);
   175          exit (0);
   176  }


<---- 以上言论仅代表本人立场 ---->



__________________


  “……机器人罗诺,现在我要对你下达最新指令了。”
  “是,主人。我都等了三万年了呢。”
  “你还记得回地球的航路吧?”



   

esoul
普通会员

发贴: 64
积分: 0
来自:
注册日期: 2006-05-24
  发表时间: 2007-02-13 10:55:14  

--------------------------------------------------------------------------------
好,这东西我也需要!
<---- 以上言论仅代表本人立场 ---->
您需要登录后才可以回帖 登录 | 注册创意安天

本版积分规则

小黑屋|手机版|Archiver|创意安天 ( 京ICP备09068574,ICP证100468号。 )

GMT+8, 2024-5-18 14:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表