Chinaunix首页 | 论坛 | 博客
  • 博客访问: 308582
  • 博文数量: 27
  • 博客积分: 758
  • 博客等级: 军士长
  • 技术积分: 369
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 23:10
文章分类

全部博文(27)

文章存档

2014年(1)

2012年(26)

我的朋友

分类: 嵌入式

2012-08-08 16:08:24

把蓝牙模块的RXD TXD分别与USB转TTL小板的RXD TXD相连。
编译:gcc bluetooth_set.c -o blue -lpthread
使用举例:./blue /dev/ttyUSB0 38400

如果是从模块,记住AT模式的BAUD率是上次设置好的;
如果是主模块,可以尝试BAUD为38400
无论是主模块还是从模块,进入AT模式都必须是未连接的情况下才行。

  1. /* bluetooth_set.c */
  2. #include <stdio.h> /* Standard input/output definitions */
  3. #include <stdlib.h>
  4. #include <string.h> /* String function definitions */
  5. #include <unistd.h> /* UNIX standard function definitions */
  6. #include <fcntl.h> /* File control definitions */
  7. #include <errno.h> /* Error number definitions */
  8. #include <termios.h> /* POSIX terminal control definitions */
  9. #include <pthread.h>

  10. static int set_fl(int fd, int flags)
  11. {
  12.     int val;

  13.     val = fcntl(fd, F_GETFL, 0);
  14.     if(val < 0){
  15.         perror("fcntl get error");
  16.         exit(1);
  17.     }

  18.     val |= flags;
  19.     if(fcntl(fd, F_SETFL, val) < 0){
  20.         perror("fcntl set error");
  21.         exit(2);
  22.     }

  23.     return 0;
  24. }

  25. /*
  26.  * @brief Open serial port with the given device name
  27.  *
  28.  * @return The file descriptor on success or -1 on error.
  29.  */
  30. int open_port(char *port_device)
  31. {
  32.     int fd; /* File descriptor for the port */

  33.     fd = open(port_device, O_RDWR );//O_NOCTTY );
  34.     if (fd == -1)
  35.     {
  36.         perror("open_port: Unable to open port");
  37.         exit(-1);
  38.     }
  39.     

  40.     return (fd);
  41. }




  42. void get_result( void *ptr )
  43. {
  44.     char buf[1024];
  45.     int fd=*((int*)ptr);
  46.     char* pos;
  47.     int n;
  48.     
  49.     strcpy(buf,"\n> ");
  50.     pos=&buf[strlen(buf)];
  51.     while(1){
  52.         if((n=read(fd, pos, 1))==1 ){
  53.          if(*pos=='\n'){
  54.          *pos=0;
  55.          if(pos!=buf)printf("%s\n",buf);
  56.          fflush(stdout);
  57.                 strcpy(buf,"\n> ");
  58.                 pos=&buf[strlen(buf)];
  59.      continue;
  60.          }
  61.          pos++;
  62.         }
  63.     }

  64. }

  65. int main(int argc, char* argv[])
  66. {
  67.     struct termios options;
  68.     pthread_t get_result_thread;
  69.     void * thread_ret;

  70.     if(argc<3){
  71.      printf("usage:\n\t%s \n",argv[0]);
  72.      return -1;
  73.     }
  74.     printf("Selected baud rate is %ld\n",atol(argv[2]));
  75.     
  76.     int fd=open_port(argv[1]);
  77.     if(fd==-1){
  78.         return -1;
  79.     }

  80.     tcgetattr(fd, &options);


  81.     //Set the baud rate
  82.     switch(atol(argv[2])){
  83.     case 9600:
  84.         cfsetispeed(&options, B9600);
  85.         cfsetospeed(&options, B9600);
  86.         break;
  87.     case 19200:
  88.         cfsetispeed(&options, B19200);
  89.         cfsetospeed(&options, B19200);
  90.         break;
  91.     case 38400:
  92.         cfsetispeed(&options, B38400);
  93.         cfsetospeed(&options, B38400);
  94.         break;
  95.     case 57600:
  96.         cfsetispeed(&options, B57600);
  97.         cfsetospeed(&options, B57600);
  98.         break;
  99.     case 115200:
  100.         cfsetispeed(&options, B115200);
  101.         cfsetospeed(&options, B115200);
  102.         break;
  103.     default:
  104.         printf("Selected baud rate %ld is not support now!\n", atol(argv[2]));
  105.         close(fd);
  106.         return -1;
  107.     }
  108.    

  109.     //Enable the receiver and set local mode...
  110.     options.c_cflag |= (CLOCAL | CREAD);
  111.     options.c_cflag &= ~CSIZE; /* Mask the character size bits */
  112.     options.c_cflag |= CS8; /* Select 8 data bits */

  113.     //No parity
  114.     options.c_cflag &= ~PARENB;
  115.     options.c_cflag &= ~CSTOPB;


  116.     //Set the new options for the port...
  117.     tcsetattr(fd, TCSANOW, &options);
  118.     pthread_create( &get_result_thread, NULL, (void*)&get_result, (void*)&fd);
  119.     
  120.     char buf[1024];


  121.     while(1){
  122.         memset(buf,0,sizeof buf);
  123.         printf("CMD: ");
  124.         fgets(buf, sizeof buf, stdin);
  125.         int len=strlen(buf);
  126.         if(len>2){
  127.          buf[len-1]='\r';
  128.          buf[len]='\n';
  129.          buf[len+1]=0;
  130.             size_t n=write(fd, buf, len+1);
  131.         
  132.             if(n!=len+1){
  133.                 printf("\n\tError!\n");
  134.             }

  135.         }
  136.         usleep(100000);
  137.     }
  138.     
  139.     
  140.     pthread_join(get_result_thread,&thread_ret);

  141.     close(fd);
  142. }

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