c语言蓝牙接收6,终于搞定了通过两路蓝牙接收数据

一直想做无线传感器,通过蓝牙来接收数据,无奈因为arduino接收串口数据的一些问题,一直搁到现在。因为学校里给学生开了选修课,所以手边有一些nano和mega可以使用,所以就做了用两个nano加上两个蓝牙模块来发射数据,用mega加两个蓝牙模块来接收数据。因为mega可以同时使用4路串口,所以考虑用mega来作为主控机,用来接收来自传感器的数据,每个传感器加一个nano和蓝牙来无线发射数据。

b5688be71e02e8917c67095fe7bc4280.png

因为是测试,所以在nano中写了一个简单的发射信号的语句。以下四nano中的程序:

————————————————我是分界线——————————————————————

int i=1;

void setup() {

Serial.begin(9600);   }

void loop(){

Serial.print(i);

Serial.print("$");

delay(50);

i++;

}

————————————————我是分界线——————————————————————

以下是mega中的程序:

——————————————————我是分界线————————————————————————

String comdata = "";

String shuju="";

String shuju2="";

int i;

void setup()

{

Serial.begin(9600);

Serial1.begin(9600);

Serial2.begin(9600);

}

void loop()

{

while (Serial1.available() > 0)

{

shuju += char(Serial1.read());

delay(2);

}

while (Serial2.available() > 0)

{

shuju2 +=char(Serial2.read());

delay(2);

}

if (shuju.length() > 0)

{

i=shuju.indexOf("$");

if(i!=-1){

comdata=shuju.substring(0,i);

Serial.print("#1=");

Serial.print(comdata);

Serial.println("$");

shuju=shuju.substring(i+1,shuju.length());

comdata = "";

i=0;

}

}

if (shuju2.length() > 0)

{

i=shuju2.indexOf("$");

if(i!=-1){

comdata=shuju2.substring(0,i);

Serial.print("#2=");

Serial.print(comdata);

Serial.println("$");

shuju2=shuju2.substring(i+1,shuju2.length());

comdata = "";

i=0;

}    } }

——————————————我是分界线————————————————————

上篇忘记介绍蓝牙模块的设置了,如果想要利用两路蓝牙来接收数据,那么这两路蓝牙必须设置不同的密码,以下是蓝牙模块的设置方法,可以将蓝牙模块接到串口模块上,然后里哟你串口软件发指令给蓝牙模块,以下设置是针对DFrobot的蓝牙模块的,它上面有开关可以设置接收命令状态。如果不是DFrobot的蓝牙模块,只要将KEY针脚接高电平就是可以接受命令状态。将两个蓝牙模块设置为主,然后设置不同的密码。再将另外两个设置为从,设置对应主蓝牙的密码。以下是具体的命令设置:

在发送栏中输入AT(不分大小写),然后点击发送,可看见模块返回OK,这表示AT指令通讯正常。当AT指令设置完毕后,将2号开关AT Mode拨到2端退出AT命令模式,重新上电后设置才生效。

1.AT指令集如下:

指令

响应

参数

AT

OK

2.模块重启指令:

指令

响应

参数

AT+RESET

OK

3.设置和查询模块连接模式:

当模块无法被适配器、主机连接时,需配置该参数,配置为AT+CMODE=1。

指令

响应

参数

AT+CMODE=< Param >

OK

Param:

0 —— 指定蓝牙地址连接模式 (指定蓝牙地址由绑定指令设置)

1 —— 任意蓝牙地址连接模式 (不受绑定指令设置地址的约束)

默认连接模式:0

AT+CMODE?

+CMODE: < Param > OK

4.设置和查询模块角色:

当模块无法被适配器、主机搜索到时,需配置该参数,配置为从角色。

577335966931f1931677551381e0234a.png

模块角色说明:

Slave(从角色) ——被动连接,可以和任意蓝牙适配器配对使用;

Master (主角色)——查询周围从设备,并主动发起连接,从而建立主、从蓝牙设备间的透明数据传输通道。

Slave-Loop(回环角色) ———被动连接,接收远程蓝牙主设备数据并将数据原样返回给远程蓝牙主设备;

5.设置和查询配对码:

1a00d8cc04eacc72c3142dc3f0d02657.png

6.设置和查询串口参数:

f0461be1d0bb2fe35dcd72e5bdb6ff9a.png

举例:设置串口波特率:115200,1位停止位,无校验

AT+ UART=115200,0,0\r\n

OK

AT+ UART?

+UART:115200,0,0

OK

注:在应用中,选择1位停止位,无校验。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441272.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【POJ - 1942 】Paths on a Grid (组合数学,求组合数的无数种方法)

题干&#xff1a; Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time hes explaining that (ab) 2a 22abb 2). So you decide to waste your time with d…

编译原理last集c语言,编译原理作业集-第七章.doc

编译原理作业集-第七章第七章 语义分析和中间代码产生本章要点1. 中间语言&#xff0c;各种常见中间语言形式&#xff1b;2. 说明语句赋值语句布尔表达式控制语句的翻译&#xff1b;3. 过程调用的处理&#xff1b;4. 类型检查&#xff1b;本章目标掌握和理解中间语言&#xff0…

【CodeForces - 244A 】Dividing Orange (构造,水题)

题干&#xff1a; One day Ms Swan bought an orange in a shop. The orange consisted of nk segments, numbered with integers from 1 to nk. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided…

兔子吃萝卜的c语言编程,狼追兔子的c语言实现

满意答案16guoyuming2013.03.05采纳率&#xff1a;49% 等级&#xff1a;13已帮助&#xff1a;8005人用单链表实现#include #includetypedef struct node{int cave;struct node * next;}node,*LinkList;void main(){int i0,j,count1; // 初始值为1&#xff1b;LinkList L,p,h…

【CodeForces - 244B】Undoubtedly Lucky Numbers (dfs打表 + 二分)

题干&#xff1a; Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x  4, and y  7, then numbers 47, 74…

c语言二叉树构造与输出,C语言数据结构树状输出二叉树,谁能给详细的解释一下...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼void TranslevelPrint(Bitree bt){struct node{Bitree vec[MAXLEN]; //存放树结点int layer[MAXLEN]; //结点所在的层int locate[MAXLEN]; //打印结点的位置int front,rear;}q;int i,j,k;int nLocate;j 1;k 0;q.front 0;q.rear …

【CodeForces - 245C 】Game with Coins (思维,贪心)

题干&#xff1a; Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has aicoins. Polycarpus and Vasily move in turns. Polycarpus moves first. Du…

【CodeForces - 246D】Colorful Graph (暴力,图,存边,STL)

题干&#xff1a; Youve got an undirected graph, consisting of n vertices and m edges. We will consider the graphs vertices numbered with integers from 1 to n. Each vertex of the graph has a color. The color of the i-th vertex is an integer ci. Lets consi…

android 动态换肤框架,GitHub - ss520k/Android-Skin-Loader: 一个通过动态加载本地皮肤包进行换肤的皮肤框架...

Android-Skin-Loader更新日志导入到Android Studio&#xff0c;使用gradle构建皮肤包(见7. 皮肤包是什么&#xff1f;如何生成&#xff1f;)(2015-12-02)解决Fragment换肤在某些版本的support-v4包下失效的问题(感谢javake同学)(2015-12-02)对textColor加入selector类型的资源的…

【CodeForces - 349A】Cinema Line (贪心(其实不是贪心),乱搞)

题干&#xff1a; The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the boo…

android 获取默认程序图标,android – PackageManager.getApplicationIcon()返回默认图标?...

我刚想通了.有一个PackageManager.getDefaultActivityIcon()方法返回一个Drawable.如果Drawable的Bitmap与应用程序图标Drawable的Bitmap匹配,则它是默认图标.PackageManager pm context.getPackageManager();Drawable icon pm.getApplicationIcon(apk.package_name);Drawabl…

【CodeForces - 255A】Greg's Workout (水题)

题干&#xff1a; Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in orde…

android吸附菜单,Android仿微博、人人Feed详情页吸附导航栏

仿微博、人人的feed详情页面&#xff1a;Listview上下滑动&#xff0c;导航栏view可吸附在顶部的效果。一、实现效果上图&#xff1a;效果图.gif欢迎拍砖&#xff0c;拍拍更进步。没有对比&#xff0c;怎么会有伤害&#xff0c;下面是 微博、人人的Feed详情页&#xff1a;微博、…

android 居右属性,使用layoutDirection属性设置布局靠左或靠右

通过设置layoutDirection属性值为mx.core.LayoutDirection.RTL(右到左)或mx.core.LayoutDirection.LTR(左到右)&#xff0c;使布局为靠左或靠右(如下图)。该属性可设置3种值&#xff0c;LayoutDirection.RTL、LayoutDirection.LTR和null(ILayoutDirectionElement时)/undefined(…

【CodeForces - 255B】Code Parsing(思维,字符串)

题干&#xff1a; Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitalys algorithm works with string s, consisting of characters "x" and "y", and uses two following operations at runtime: …

【CodeForces - 255C】Almost Arithmetical Progression (dp,离散化)

题干&#xff1a; Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: a1  p, wh…

Android手机mm开头的大文件,[2018年最新整理]2Android源代码编译命令m和mm和mmm以及make分析.doc...

[2018年最新整理]2Android源代码编译命令m和mm和mmm以及make分析老罗的新浪微博&#xff1a;/shengyangluo&#xff0c;欢迎关注&#xff01;在前文中&#xff0c;我们分析了Android编译环境的初始化过程。Android编译环境初始化完成后&#xff0c;我们就可以用m/mm/mmm/make命…

【CodeForces - 349C】Mafia(思维模拟,优秀的二分)

题干&#xff1a; One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a pl…

android新材料设计,android - 如何实现新材料BottomAppBar为BottomNavigationView - SO中文参考 - www.soinside.com...

解决了基本上&#xff0c;而不是试图迫使菜单的资源&#xff0c;我需要的布局&#xff0c;我用这个方法&#xff0c;而不是&#xff0c;我只是把使用“空”元素作为dglozano建议BottomAppBar内的LinearLayout。使用?attr/selectableItemBackgroundBorderless我也能做到这一点实…

【CodeForces - 1A】Theatre Square(水题,几何)(CODEFORCES,梦的开始)

题干&#xff1a; Theatre Square in the capital city of Berland has a rectangular shape with the size n  m meters. On the occasion of the citys anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the …