1. 程式人生 > >JDY-16藍芽模組除錯

JDY-16藍芽模組除錯

手冊下載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

}