由于项目需要蓝牙功能,前些日子,我负责开发蓝牙模块,这个子项目主要涉及到获取蓝牙模块参数、设置蓝牙参数、多线程收发数据等功能,下面已经可以在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("
com set error");
return -1;
}
printf("
set done!
");
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("
Can"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("
Can"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("
Can"t Open Serial Port");
return(-1);
} } if(fcntl(fd, F_SETFL, 0) < 0)
{
printf("
fcntl failed!
");
}
else
{
printf("
fcntl = %d
", fcntl(fd, F_SETFL, 0));
} if(isatty(STDIN_FILENO) == 0)
printf("
standard input is not a terminal device
");
else
printf("isatty success!
");
printf("
fd-open=%d
",fd);
return fd;
}
static int Get_Parameter(int fd)
{
printf("Get Bluetooth Parameter
");
//printf("Get parameter :
");
// printf("Device Name :
");
Serial_Tran(fd, "AT+NAME?
"); //询查设备名称
//printf("BAUD :
");
Serial_Tran(fd, "AT+BAUD?
"); //查询波特率
//printf("Device Bind :
");
Serial_Tran(fd, "AT+BIND?
"); // 查询设备绑定
//printf("Device Class :
");
Serial_Tran(fd, "AT+CLASS?
") ; //查询类别
//printf("Device Authority :
");
Serial_Tran(fd, "AT+SNIFF?
"); //查询是否需要鉴权
//printf("Device PassWord :
");
Serial_Tran(fd, "AT+PASSWORD?
"); //查询鉴权密码
//printf("Device Role :
");
Serial_Tran(fd, "AT+ROLE?
"); // 查询设备角色
//printf("Device Sniff State:
");
Serial_Tran(fd, "AT+SNIFF?
"); //查询节能状态
//printf("Device ScanTime :
");
Serial_Tran(fd, "AT+SCANTIME?
"); //查寻扫描与连接参数
//printf("Program Version :
");
Serial_Tran(fd, "AT+VERSION?
"); //查询程序版本号
//printf("Device LED : ");
Serial_Tran(fd, "AT+LED?
"); // 查询指示灯
//printf("Remote Device Address : ");
Serial_Tran(fd, "AT+RADDR?
"); //查询远端设备地址
//printf("Local Device Address : ");
Serial_Tran(fd, "AT+LADDR?
"); //查询本地设备地址
//printf("Device UartMode :
");
Serial_Tran(fd, "AT+UARTMODE?
"); //查询串口通信模式
//printf("Device LowPower :
");
Serial_Tran(fd, "AT+LOWPOWER?
"); //查询设备低功?
return 0;
}
static int Set_Parameter(int fd)
{
int i, sel, num;
printf("
If you want to set vironment?please input 1
");
printf("
ENTER : ");
//if(!GetIntNumber(&label))
scanf("%d",&num);
if (num== 1)
{
while(1)
{
printf("
Select :
");
for(i=0; Functions[i].FuncAddr; i++)
{
printf("
%2d : %s
", i, Functions[i].str);
}
printf("
Enter your selection: ");
scanf("%d",&sel);
if(sel==8)
{
break;
}
if(sel>=0&&sel<i)
{
(*Functions[sel].FuncAddr)(downloadAddress, downloadFileSize);
}
}
}
else
{
return 0;
}
}为嵌入式Linux开发配置Linux主机Fedora 8中启用炫炫的3D桌面特效(组图)相关资讯 蓝牙 Linux技巧 Linux 蓝牙
- Linux技巧分享:如何检查PDF中使用 (08/27/2014 19:11:58)
- [技巧分享]如何在Linux中阻止其它 (06/29/2014 19:56:43)
- 8个有趣的Linux提示与技巧! (01/31/2014 08:06:39)
| - Linux 目录导航技巧 (07/18/2014 08:29:50)
- 如何在Ubuntu 14.04中默认关闭蓝牙 (06/04/2014 10:57:40)
- 开源自动导航蓝牙机器人Bero上市 (11/12/2013 06:05:10)
|
本文评论 查看全部评论 (0)