由於項目需要藍牙功能,前些日子,我負責開發藍牙模塊,這個子項目主要涉及到獲取藍牙模塊參數、設置藍牙參數、多線程收發數據等功能,下面已經可以在Linux下正常使用的藍牙參數設置獲取主要代碼。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/signal.h>
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned int U32;
typedef unsigned int UINT;
struct termios newtio, oldtio;
int fd;
UINT downloadAddress, downloadFileSize;
static void Jump_Loop(U32 addr, U32 len);
static void Set_UARTMODE(U32 addr, U32 len);
static void Set_Bind(U32 addr, U32 len);
static void Set_Role(U32 addr, U32 len);
static void Set_Class(U32 addr, U32 len);
static void Set_Name(U32 addr, U32 len);
static void Set_PassWord(U32 addr, U32 len);
static void Set_Auth(U32 addr, U32 len);
static void Set_Baud(U32 addr, U32 len);
static void Serial_Tran(int fd, char ver[]);
Get_Parameter();
Set_Parameter();
int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
if(tcgetattr(fd, &oldtio) != 0)
{
perror("SetupSerial 1");
return -1;
}
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch(nBits)
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch(nEvent)
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
switch(nSpeed)
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if(nStop == 1)
newtio.c_cflag &= ~CSTOPB;
else if(nStop == 2)
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
newtio.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG);
newtio.c_oflag &=~OPOST;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd, TCSANOW,&newtio)) != 0)
{
perror("\rcom set error");
return -1;
}
printf("\rset done!\n");
return 0;
}
int open_port(int fd, int comport)
{
if(comport == 1) //串口1
{
fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NONBLOCK);
if(fd < 0)
{
perror("\rCan't Open Serial Port");
return(-1);
}
}
else if(comport == 2) //串口2
{
fd = open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if(fd < 0)
{
perror("\rCan't Open Serial Port");
return(-1);
}
}
else if(comport == 3)//串口3
{
fd = open("/dev/ttyS2", O_RDWR|O_NOCTTY|O_NONBLOCK);
if(fd < 0)
{
perror("\rCan't Open Serial Port");
return(-1);
}
}
if(fcntl(fd, F_SETFL, 0) < 0)
{
printf("\rfcntl failed!\n");
}
else
{
printf("\rfcntl = %d\n", fcntl(fd, F_SETFL, 0));
}
if(isatty(STDIN_FILENO) == 0)
printf("\rstandard input is not a terminal device\n");
else
printf("isatty success!\n");
printf("\rfd-open=%d\n",fd);
return fd;
}
static int Get_Parameter(int fd)
{
printf("Get Bluetooth Parameter\n");
//printf("Get parameter : \n");
// printf("Device Name : \n");
Serial_Tran(fd, "AT+NAME?\r\n"); //詢查設備名稱
//printf("BAUD : \n");
Serial_Tran(fd, "AT+BAUD?\r\n"); //查詢波特率
//printf("Device Bind : \n");
Serial_Tran(fd, "AT+BIND?\r\n"); // 查詢設備綁定
//printf("Device Class : \n");
Serial_Tran(fd, "AT+CLASS?\r\n") ; //查詢類別
//printf("Device Authority : \n");
Serial_Tran(fd, "AT+SNIFF?\r\n"); //查詢是否需要鑒權
//printf("Device PassWord : \n");
Serial_Tran(fd, "AT+PASSWORD?\r\n"); //查詢鑒權密碼
//printf("Device Role : \n");
Serial_Tran(fd, "AT+ROLE?\r\n"); // 查詢設備角色
//printf("Device Sniff State: \n");
Serial_Tran(fd, "AT+SNIFF?\r\n"); //查詢節能狀態
//printf("Device ScanTime : \n");
Serial_Tran(fd, "AT+SCANTIME?\r\n"); //查尋掃描與連接參數
//printf("Program Version : \n");
Serial_Tran(fd, "AT+VERSION?\r\n"); //查詢程序版本號
//printf("Device LED : ");
Serial_Tran(fd, "AT+LED?\r\n"); // 查詢指示燈
//printf("Remote Device Address : ");
Serial_Tran(fd, "AT+RADDR?\r\n"); //查詢遠端設備地址
//printf("Local Device Address : ");
Serial_Tran(fd, "AT+LADDR?\r\n"); //查詢本地設備地址
//printf("Device UartMode : \n");
Serial_Tran(fd, "AT+UARTMODE?\r\n"); //查詢串口通信模式
//printf("Device LowPower : \n");
Serial_Tran(fd, "AT+LOWPOWER?\r\n"); //查詢設備低功?
return 0;
}
static int Set_Parameter(int fd)
{
int i, sel, num;
printf("\rIf you want to set vironment?please input 1\n");
printf("\rENTER : ");
//if(!GetIntNumber(&label))
scanf("%d",&num);
if (num== 1)
{
while(1)
{
printf("\rSelect :\n");
for(i=0; Functions[i].FuncAddr; i++)
{
printf("\r%2d : %s\n", i, Functions[i].str);
}
printf("\rEnter your selection: ");
scanf("%d",&sel);
if(sel==8)
{
break;
}
if(sel>=0&&sel<i)
{
(*Functions[sel].FuncAddr)(downloadAddress, downloadFileSize);
}
}
}
else
{
return 0;
}
}