Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1307930
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: 嵌入式

2012-05-11 16:19:07


点击(此处)折叠或打开

  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include <unistd.h>

  4. #include <sys/types.h>

  5. #include <sys/stat.h>

  6. #include <fcntl.h>

  7. #include <termios.h>

  8. #include <errno.h>



  9. int main(int argc, char **argv)

  10. {

  11.     int fd;

  12.     int i,k;

  13.     int nwrite,nread;

  14.     struct termios opt;

  15.     unsigned char read_buff[256];

  16.     unsigned char write_buff[]={0x02,0x00,0x00,0x04,0x15,0x10,0x03,0x1C,0x03};

  17.     int len=9;



  18.     fd = open("/dev/ttyS5", O_RDWR | O_NOCTTY); //×è·

  19.     if(fd == -1)

  20.         perror("open serial 0\n");

  21.     

  22.     tcgetattr(fd, &opt);

  23.     cfsetispeed(&opt, B19200);

  24.     cfsetospeed(&opt, B19200);

  25.     

  26.     if(tcsetattr(fd, TCSANOW, &opt) != 0 )

  27.     {

  28.         perror("tcsetattr error");

  29.      return -1;

  30.     }

  31.     

  32.     opt.c_cflag &=~CSIZE;

  33.     opt.c_cflag |= CS8;

  34.     opt.c_cflag &=~CSTOPB;

  35.     opt.c_cflag &=~PARENB;

  36.     opt.c_cflag &=~INPCK;

  37.     opt.c_cflag |= (CLOCAL | CREAD);

  38.     

  39.     opt.c_lflag &=~(ICANON | ECHO | ECHOE | ISIG);

  40.     

  41.     opt.c_oflag &=~OPOST;

  42.     

  43.     opt.c_iflag &=~ICRNL;

  44.     opt.c_iflag &=~INLCR;

  45.     

  46.     opt.c_cc[VTIME] = 0;

  47.     opt.c_cc[VMIN] = 0;

  48.     

  49.     tcflush(fd, TCIOFLUSH);

  50.     

  51.     printf("configure complete\n");

  52.     

  53.     if(tcsetattr(fd, TCSANOW, &opt) != 0)

  54.     {

  55.         perror("serial error");

  56.         return -1;

  57.     }

  58.     printf("start send and receive data\n");

  59.     

  60.     

  61.     //for(i=0;i<len;i++)

  62.     //{

  63.         nwrite = write(fd, write_buff, 9);

  64.         

  65.     //    if(nwrite!=1)

  66.     //    {

  67.     //    printf("Send error!\n");

  68.     //    break;

  69.     //    }

  70.     //    else

  71.     

  72.     printf("SEND: ");

  73.     for(i=0;i<len;i++)

  74.     {

  75.         {

  76.             printf("%02X ",write_buff[i]);

  77.         }

  78.         //for(k=0;k<0xfffff;k++);

  79.     }

  80.     printf("\n");



  81.     while(1)

  82.     {

  83.         while((nread = read(fd,read_buff,256))>0)

  84.         {

  85.             printf("\nREAD Len %d\n", nread);

  86.         //    read_buff[nread+1]='\0';

  87.         //    printf("\nread: %s",read_buff);

  88.             printf("READ: %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",

  89.                     read_buff[0],

  90.                     read_buff[1],

  91.                     read_buff[2],

  92.                     read_buff[3],

  93.                     read_buff[4],

  94.                     read_buff[5],

  95.                     read_buff[6],

  96.                     read_buff[7],

  97.                     read_buff[8]);



  98.         }

  99.     }



  100.     close(fd);

  101. }

阅读(1330) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~