JDY-16蓝牙模块调试

版权声明:转载请注明出处 https://blog.csdn.net/u010659887/article/details/84391504

手册下载http://www.jdy-rf.com/down/html/?10.html

详细的调试方法手册里说的很清楚了,这里记录一些遇到问题和解决方法

1.AT模式

 JDY-16在与设备蓝牙连接时无法使用AT模式,所有的AT指令都会当做数据传输给已连接的蓝牙设备;

当断开蓝牙连接时会自动进入AT模式。

2.AT指令

比如查询RTC时间指令:AT + RTCD \ r \ n

串口指令:0x41 0x54 0x2B 0x52 0x54 0x43 0x44 0x0d 0x0a

\ r \ n对应的十六进制是0x0d 0x0a

3.主从模式

主模式下的模块不能被扫描到,从模式可以

待链接状态下红色灯闪,链接成功红色灯长亮

4.BLE

BLE即低功耗蓝牙协议,与传统蓝牙协议最显著的区别就是一个蓝牙设备可以有多个服务,每个服务又有多个特征值,每个特征值又有不同的读写权限,仅仅配对设备或者连接服务是无法与BLE设备互动的,初次接触BLE的小伙伴要注意。

目前市面上大多数蓝牙模块只支持自己厂家的模块互联,而且不支持作为主设备去连接指定的BLE服务及特征值,不出意外JDY-16也不支持。

5.串口调试代码

参数1串口设备路径;参数2传输数据

#include     <stdio.h>      /*标准输入输出定义*/
#include     <stdlib.h>     /*标准函数库定义*/
#include     <unistd.h>     /*Unix标准函数定义*/
#include     <sys/types.h>  /**/
#include     <sys/stat.h>   /**/
#include     <fcntl.h>      /*文件控制定义*/
#include     <termios.h>    /*PPSIX终端控制定义*/
#include     <errno.h>      /*错误号定义*/
#include	 <sys/time.h>


/***@brief  设置串口通信速率
*@param  fd     类型 int  打开串口的文件句柄
*@param  speed  类型 int  串口速度
*@return  void*/
#define TRUE 1
#define FALSE -1

int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200, 38400,  19200,  9600,  4800,  2400,  1200,  300, 38400,  19200,  9600, 4800, 2400, 1200,  300, };
void set_speed(int fd, int speed)
{
  int   i;
  int   status;
  struct termios   Opt;
  tcgetattr(fd, &Opt);
  for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
   {
   	if  (speed == name_arr[i])
   	{
   	    tcflush(fd, TCIOFLUSH);
		/*  设置串口的波特率 */
    	cfsetispeed(&Opt, speed_arr[i]);
    	cfsetospeed(&Opt, speed_arr[i]);
    	status = tcsetattr(fd, TCSANOW, &Opt);
    	if  (status != 0)
		{
			perror("tcsetattr fd1");
		}
     	return;
     	}
   tcflush(fd,TCIOFLUSH);

   }
}

/**
*@brief   设置串口数据位,停止位和效验位
*@param  fd     类型  int  打开的串口文件句柄*
*@param  databits 类型  int 数据位   取值 为 7 或者8*
*@param  stopbits 类型  int 停止位   取值为 1 或者2*
*@param  parity  类型  int  效验类型 取值为N,E,O,,S
*/

int set_Parity(int fd,int databits,int stopbits,int parity)
{
	struct termios options;
	if( tcgetattr( fd,&options)  !=  0)
	{
		perror("SetupSerial 1");
		return(FALSE);
  }

  options.c_cflag &= ~CSIZE;
  switch (databits) /*设置数据位数*/
  {
  	case 7:
  		options.c_cflag |= CS7;
  		break;
  	case 8:
		options.c_cflag |= CS8;
		break;
	default:
		fprintf(stderr,"Unsupported data size\n");
		return (FALSE);
	}

  switch (parity)
  	{
  	case 'n':
	case 'N':
		options.c_cflag &= ~PARENB;   /* Clear parity enable */
		options.c_iflag &= ~INPCK;     /* Enable parity checking */
		options.c_iflag &= ~(ICRNL|IGNCR);
		options.c_lflag &= ~(ICANON );
		break;
	case 'o':
	case 'O':
		options.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/ 
		options.c_iflag |= INPCK;             /* Disnable parity checking */
		break;
	case 'e':
	case 'E':
		options.c_cflag |= PARENB;     /* Enable parity */
		options.c_cflag &= ~PARODD;   /* 转换为偶效验*/  
		options.c_iflag |= INPCK;       /* Disnable parity checking */
		break;
	case 'S':
	case 's':  /*as no parity*/
		options.c_cflag &= ~PARENB;
		options.c_cflag &= ~CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported parity\n");
		return (FALSE);
		}

  /* 设置停止位*/   
  switch (stopbits)
  	{
  	case 1:
  		options.c_cflag &= ~CSTOPB;
		break;
	case 2:
		options.c_cflag |= CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported stop bits\n");
		return (FALSE);
	}

  /* Set input parity option */

  if (parity != 'n')
  {
	  options.c_iflag |= INPCK;
  }	
    options.c_cc[VTIME] = 150; // 15 seconds
    options.c_cc[VMIN] = 0;
	tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  if (tcsetattr(fd,TCSANOW,&options) != 0)
  	{
  		perror("SetupSerial 3");
		return (FALSE);
	}
  return (TRUE);

 }

/**
*@breif 打开串口
*/
int OpenDev(const char *Dev)
{
	int	fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
	if (-1 == fd)
		{ /*设置数据位数*/
			perror("Can't Open Serial Port");
			return -1;
		}
	else
	return fd;
}

/**
*@breif 	main()
*/
int main(int argc, char **argv)
{
	int fd;
	int nread;
	char buffer[512];
	//rtc
	//char send[512]={0x41,0x54,0x2B,0x52,0x54,0x43,0x44,0x0D,0x0a};
	//微信步数
	//char send[512]={0x41, 0x54, 0x2B, 0x57, 0x58, 0x50, 0xA0, 0x86, 0x01, 0xFA, 0x00, 0x00, 0x88, 0x13, 0x00, 0x0D, 0x0A};
	//char send[512] = {0x41,0x54,0x2B,0x4E,0x41,0x4D,0x45,0x0d,0x0a};
	char send[512];
	//char *dev ="/dev/ttySAC1"; 
	int n=0,i=0;
    const char* dev   = NULL;
    const char* string   = NULL;
    dev   = argv[1];
    string = argv[2];
    if(dev==NULL)
    {
      printf("Please input seria device name ,for exmaple /dev/ttyO1.\nNote:This is loop test application. Make sure that your serial is loop\n");
      return 0;
    }

	/*  打开串口 */
	fd = OpenDev(dev);
	
	if (fd>0)
	{
		set_speed(fd,9600); //设置波特率
	}
       
	else
	{
		printf("Can't Open Serial Port!\n");
		exit(0);
	}

  	if (set_Parity(fd,8,1,'N')== FALSE)  //设置传递参数
  	{
    	printf("Set Parity Error\n");
    	exit(1);
  	}
		printf("\nWelcome to TTYtest\n\n");
        memset(buffer,0,512);

		n = strlen(string);
		memcpy(send,string,n);
		send[n] = 0x0D;
		send[n+1] = 0x0A;
		write(fd, send, n+2);

        printf("Send test data------%s\n",send);
		
#if 1
  	while(1)
  	{
		nread = read(fd,buffer,512);	
        printf("read char is %s \n",buffer);
         }
#endif

}

猜你喜欢

转载自blog.csdn.net/u010659887/article/details/84391504