由于項目需要藍牙功能,前些日子,我負責開發藍牙模塊,這個子項目主要涉及到獲取藍牙模塊參數、設置藍牙參數、多線程收發數據等功能,下面已經可以在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);
新聞熱點
疑難解答