Chinaunix首页 | 论坛 | 博客
  • 博客访问: 93146
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-11 09:25
个人简介

浮萍漂泊本无根,天涯游子君莫问。

文章分类
文章存档

2015年(80)

我的朋友

分类: LINUX

2015-06-21 09:04:03

操作系统:ubuntu10.04


前言:
    在串口编程中,当通信的设备不需求并发,可以封装成函数形式。

一,源码
    (1) 头文件

点击(此处)折叠或打开

  1. /*
  2.  * lower,下位机
  3.  * {
  4.  */
  5. /* 串口的初始化 */
  6. extern     int32_t        com_init_lower(void *param);

  7. /* 串口的发送 */
  8. extern    int32_t        com_send_lower(uint8_t *send_buffer,int32_t buffer_len);

  9. /* 串口接收 */
  10. extern    int32_t        com_recv_lower(uint8_t *buffer,const int32_t need_to_recv);

  11. /* 串口关闭*/
  12. extern    int32_t        com_close_lower(void *param);

  13. /*
  14.  * lower,下位机
  15.  * }
  16.  */
    (2) *.c 文件

点击(此处)折叠或打开

  1. #define    LOWER_DEVICE_COM            "/dev/ttySAC0"



  2. static     int32_t     __com_set(int32_t com_fd,speed_t baud_rate)
  3. {
  4.     int32_t fd = com_fd;
  5.     struct termios opt;
  6. //    struct timeval start;
  7.     
  8.     tcgetattr(fd, &opt);
  9. //    cfsetispeed(&opt, B115200);
  10. //    cfsetospeed(&opt, B115200);
  11.     cfsetispeed(&opt, baud_rate);
  12.     cfsetospeed(&opt, baud_rate);

  13.     // 8n1
  14.     opt.c_cflag &= ~CSIZE;
  15.     opt.c_cflag &= ~CSTOPB;
  16.     opt.c_cflag &= ~PARENB;         // 无奇偶校验
  17.     opt.c_cflag |= CS8;

  18.     opt.c_cflag &= ~CRTSCTS;        // 没有使用硬件流控制
  19.     
  20. //    opt.c_cflag &= ~INPCK;
  21.     opt.c_cflag |= (CLOCAL | CREAD);                // Enable the receiver and set local mode

  22.     opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);    // 原始数据

  23.     opt.c_oflag &= ~OPOST;
  24.     opt.c_oflag &= ~(ONLCR | OCRNL);             // 添加的

  25.     opt.c_iflag &= ~(ICRNL | INLCR);
  26.     opt.c_iflag &= ~(IXON | IXOFF | IXANY);         // 没有软件流控制

  27.     tcflush(fd, TCIOFLUSH);

  28.     opt.c_cc[VTIME] = 1;    // 以 十分之一秒(1/10 s) 为单位
  29.     opt.c_cc[VMIN] = 0;

  30.     

  31.     if(tcsetattr(fd, TCSANOW, &opt) != 0 )
  32.     {
  33.            perror("tcsetattr error");
  34.            return -COM_CONFIG_ERROR;
  35.     }

  36.     return OPER_OK;
  37. }

  38. /*
  39.  * lower,下位机
  40.  * {
  41.  */

  42. static    int32_t        lower_device_comfd = 0;        /* 与下位机通信的串口的描述符(R2000模块)*/



  43. static     int32_t     _com_set_lower()
  44. {
  45.     int32_t    ret = 0;

  46.     ret = __com_set(lower_device_comfd,B115200);

  47.     return ret;
  48. }




  49. /* 串口的初始化 */
  50. int32_t        com_init_lower(void *param)
  51. {
  52.     int32_t    ret = 0;

  53.     lower_device_comfd = open(LOWER_DEVICE_COM,O_RDWR | O_NOCTTY);    // 阻塞读
  54.     if(lower_device_comfd< 0)
  55.     {
  56.         ret = -COM_OPEN_ERROR;
  57.     }
  58.     else
  59.     {
  60.         ret = _com_set_lower();
  61.     }

  62.     return ret;
  63. }


  64. /*
  65.  * 串口的发送
  66.  * 返回值:正常返回发送的字节数,异常返回负值
  67.  */
  68. int32_t        com_send_lower(uint8_t *send_buffer,int32_t buffer_len)
  69. {
  70.     int32_t    ret = 0;

  71.     // 疑问1:发送数据的长度是否需要判断有效值,有效值是多少??
  72.     
  73.     ret = write(lower_device_comfd, send_buffer, buffer_len);


  74.     return ret;
  75. }


  76. /* 串口接收 */
  77. int32_t        com_recv_lower(uint8_t *buffer,const int32_t need_to_recv)
  78. {
  79.     int32_t    ret = 0;    
  80.     while( (ret = read(lower_device_comfd, (char*)buffer, need_to_recv)) > 0 )
  81.     {
  82.         break;
  83.     }

  84.     return ret;
  85. }

  86. /* 串口关闭*/
  87. int32_t        com_close_lower(void *param)
  88. {
  89.     int32_t    ret = 0;

  90.     if(lower_device_comfd > 0 )
  91.     {
  92.         close(lower_device_comfd);
  93.         lower_device_comfd = 0;
  94.     }

  95.     return ret;
  96. }

  97. /*
  98.  * lower,下位机
  99.  * }
  100.  */
    (3) 测试

点击(此处)折叠或打开

  1. static    void        com_oper()
  2. {
  3.     unsigned char read_buf[256]     = {0};
  4.     unsigned char write_buf[256]     = {0};
  5.     int     index             = 0;
  6.     int     data_len         = 0;

  7.     
  8.     for(index = 0; index < 3; index++)
  9.     {
  10.         bzero(read_buf, sizeof(read_buf)); //类似于memset
  11.         bzero(write_buf, sizeof(write_buf));
  12.         write_buf[0] = 0xBB;

  13.         data_len = com_send_lower(write_buf, strlen((char*)write_buf));
  14.         printf("write : %d\n",data_len);

  15.         while( (data_len = com_recv_lower(read_buf, sizeof(read_buf))) > 0 )
  16.         {
  17.             break;
  18.         }
  19.         
  20.         printf("read : %d ,", data_len);
  21.         printf("%02X %02X %02X %02X %02X %02X %02X %02X\n", read_buf[0]
  22.         ,read_buf[1],read_buf[2],read_buf[3]
  23.         ,read_buf[4],read_buf[5],read_buf[6],read_buf[7]);

  24.         sleep(2);    
  25.     }
  26. }


  27. void     test_com()
  28. {
  29.     printf("hello\n");


  30.     com_init_lower(NULL);
  31.     com_oper();
  32.     com_close_lower(NULL);

  33. }

二,测试结果
    



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