Matlab蓝牙串口设置与连接通讯
利誉册用外部设备(单片机等)给蓝牙数据庆毕宏模块上电,执行配对工作。
1 点击蓝牙标志,点击添加设备,
2 下一步
3 第二项“输入设备配对码”
4 密码:1234
5 至此蓝牙连接完成,电脑自动安装驱动。
3
开始进行软件配置
1 To find available Bluetooth devices
搜索蓝牙设备
2 To construct a Bluetooth Channel object
连接蓝牙
3 To connect the Bluetooth Channel object to the remote device
打开数桥蓝牙
#include "stdio.h"void main()
{
int code_in
int i,j,a
int code_out[15]
int mid[15]
/* code */
int g[15]={0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x365,0x1AF,0x35E,0x1D9,0x3B2}
printf("输入原始信息数据(16进制):")
scanf("%x",&code_in)
for(i=0i<15i++)
{
a=code_in&g[i]
for(j=0j<10j++)
{
mid[j]=a&1
a=a>>1
}
code_out[i]=mid[0]
for(j=1j<10j++)
code_out[i]=code_out[i]^mid[j]
}
printf("纠错编码后的输出比卜耐特流: ")
for(i=15i>0i--)
printf("%d",code_out[i-1])
/* decode and correct */
for (i=0i<15i++)
{
code_out[i]=0
mid[i]=0
}
code_in=0
i=0
j=0
a=0
int check_out[5]
int errorflag,matchbit,matchflag,comparebit
int check[5]={0x765,0x9AF,0x135E,0x21D9,0x43B2} /*the check array*/
printf("\n输入接受端收到的原始数据(16进制):")
scanf("%x",&code_in) /*input the data received at the receiver*/
a=code_in
for (i=0i<型租春15i++) /*change the data received into bits*/
{
code_out[i]=a&1
a=a>>1
}
for(i=0i<5i++)/型迅*caculate the cheksum*/
{
a=code_in&check[i]
for(j=0j<15j++)/*get every bit of a */
{
mid[j]=a&1
a=a>>1
}
check_out[i]=mid[0]
for(j=1j<15j++)
check_out[i]=check_out[i]^mid[j]/*add the bits in mode 2*/
}
i=0
errorflag=0
do
{
if (check_out[i]==1)
{
errorflag=1
i=5
}
else
i++
}
while (i<5)/* check if the check is all 0 */
if (errorflag==1)
{
i=0
matchbit=100 /* look for the same line*/
do
{
j=0
matchflag=1
do
{
comparebit=(check[j]>>i)&1/* get the [j][i] bit of the array*/
if (check_out[j]!=comparebit)
{
j=5
matchflag=0
}/*not coincident,jump out at once*/
else
j++
}
while (j<5)
if (matchflag==1)
{
matchbit=i
i=15
} /*if found,jump out at once*/
else
i++
}
while (i<15)
if (matchbit<=14)
{
code_out[matchbit]=code_out[matchbit]^1 /*correct the error bit*/
printf("第%d位数据出错!\n",matchbit+1)
}
else
printf("错误但不可纠!\n")
}
else printf("传输正确或错误不可检!\n")
printf("还原的信息比特为:")
for (i=10i>0i--) /*only print the information bits*/
printf("%d",code_out[i-1])
}
How Bluetooth LE works? — Link layer | by Jacob su | MediumBluetooth Protocol Stack - MATLAB &Simulink (mathworks.com)
蓝牙协议分析(1)_基本概念 (wowotech.net) 系列蓝牙技术介绍
(41条消息) 蓝牙BLE LINK LAYER剖析(二) -- PDU_朝气蓬勃-CSDN博客
(41条消息) BLE_StephenZhou-CSDN博客
蓝牙协议分为四个层次:物理层(Physical Layer)、逻辑层(Logical Layer)、L2CAP Layer和应用层(APP Layer)。
负责提供数据传输的物理通道(通常称为信道)。通常情况下,一个通信系统中存在几种不同类型的信道,如控制信道、数据信道、语音信道等等。
在物理层的基础上,提供两个或多个设备之间、和物理无关的逻辑传输通道(也称作 逻辑链路 )。
在BR/EDR系统中,与对端的Link Manager通过LMP协议(Link Manager Protocol)进行通信;在BLE系统中则历返绝使用的是LL协议(Link Layer Protocol)。
包含Standby、Advertising、Scanning、Initiating、Connection、Synchronization、Isochronous Broadcasting state,转换图如下所示
HCI数据包分为command、event和data三种类型。command表示Host发送给Controller的命令,event为Controller发送给Host的事件,data通常是实际的蓝牙传输数据。
通常Host发送的command都会收到Controller的返回event,提示命令的执行结果。例如,HCI命令0x200c表示 LE Set Scan Enable ,并通过参数控制开启和关闭BLE的扫描,Controller执行完毕后返回event code 0x0e,即 Command Complete ,并附带status作为参数表示结果是否成功。
HCI的ACL协议主要用于在Host和Controller之间,传输世岁数据。
L2CAP是逻辑链路控制和适配协议(Logical Link Control and Adaptation Protocol)的缩写,负责管理肢姿逻辑层提供的逻辑链路。基于该协议,不同Application可共享同一个逻辑链路。类似TCP/IP中端口(port)的概念。
ACL只提供了一个数据传输协议,类比于网络协议栈中的IP协议,在其之上使用的L2CAP协议可以类比于TCP/UDP协议,实现了更为完善的数据传输功能,包括:
L2CAP channel表示两个设备之间的一条逻辑链路,使用Channel ID( CID )进行区分,并以此为基本单元在Controller逻辑链路上进行多路复用。在基于连接的信道(connection-oriented channels)中,L2CAP PDU也称为B-Frame,其格式如下:
在L2CAP之上,有着各种各样的应用层协议,比如服务发现协议SDP,蓝牙传输协议RFCOMM/OBEX,BLE的属性协议ATT,甚至是通用以太网协议BNEP以及其上的TCP/IP网络栈等。通过分层和抽象使得上层应用无需关心底层的细节,从而实现了整个蓝牙协议栈的普适性和拓展性。
理解蓝牙协议中的应用层,基于L2CAP提供的channel,实现各种各样的应用功能。Profile是蓝牙协议的特有概念,为了实现不同平台下的不同设备的互联互通,蓝牙协议不止规定了核心规范(称作Bluetooth core),也为各种不同的应用场景,定义了各种Application规范,这些应用层规范称作蓝牙profile。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)