Hacking EV3系列之五:iOS通过BTstack发送message给EV3
2014-03-01 17:22
393 查看
在上一篇文章中,我们已经分析了iOS通过BTstack这个第三方库连接外部蓝牙设备的方法,也就是可以连接EV3了。 那么,在连接成功之后,我们要做的关键就是给EV3发它可以识别的信息。这就需要EV3的通讯协议了。 最基本的协议就在c_com.h这个文件中。 这里我们以向EV3发message这种最简单的形式来看一下协议是如何使用了。 先摘录一段c_com.h中关于发message协议的内容。 1.基本的发送协议 General Protocol Overview\verbatim ,------,------,------,------,------,------,------,------, |Byte 0|Byte 1|Byte 2|Byte 3| | | |Byte n| '------'------'------'------'------'------'------'------' Byte 0 – 1: Command size, Little Endian\n Byte 2 – 3: Message counter, Little Endian\n Byte 4: Command type. The 7 lowest bit of this byte is used for identifying the command type. Bit 7 (MSB) is used for identifying whether the command should give a reply message or not. Byte 5 - n: Dependent on command type 2. 发message的协议 WRITEMAILBOX: ------------- Bytes sent to another brick: Mailbox name has to be zero terminated but name length has to be number of chars excluding the zero termination. xxxxxxxx819Exxxxxxxxxxxxxxxxxxxx bbbbmmmmttssllaaaaa...LLLLppp... bbbb = bytes in the message, mmmm = message counter, tt = type of message, ss = system command, ll = Name Length, aaa... = Name, LLLL = Payload length, ppp... = Payload 从上面我们已经可以很清楚地看出这个数据的协议格式了。 有了这个格式,下面我们需要的就是编写一个方法来实现这个message的内容。 // 组织message的方法 - (int)messageWithMailboxName:(const char*)mailboxName message:(const char*)msg outBuffer:(unsigned char *)outBuffer { outBuffer[2] = 0x00; // message count outBuffer[3] = 0x00; outBuffer[4] = 0x81; // command not reply outBuffer[5] = 0x9e; // write mailbox command int cur_p = 6; // cursor, to count bit int size = strlen(mailboxName) + 1; outBuffer[cur_p++] = size; memcpy(&outBuffer[cur_p], mailboxName, size); cur_p+=size; size = strlen(msg) +1; outBuffer[cur_p++] = size & 0xff; outBuffer[cur_p++] = (size & 0xff00) >> 8; memcpy(&outBuffer[cur_p], msg, size); cur_p+=size; outBuffer[0]=(cur_p - 2)&0xff; outBuffer[1]=((cur_p - 2)&0xff00) >> 8; return cur_p; } // 发送message的方法 - (void)sendMessage:(NSString *)message title:(NSString *)title { BTstackManager *bt = [BTstackManager sharedInstance]; uint8_t data[bt.mtu]; uint16_t length = [self messageWithMailboxName:[title UTF8String] message:[message UTF8String] outBuffer:data]; [bt sendRFCOMMPacketForChannelID:bt.connectedChannelId data:datadataLength:length]; } // 发送的BTstack方法 -(BTstackError) sendRFCOMMPacketForChannelID:(uint16_t)connectionID data:(uint8_t*)data dataLength:(uint16_t)length{ if (state < kActivated) return BTSTACK_NOT_ACTIVATED; bt_send_rfcomm(connectionID, data, length); return 0; } ok,这样使用上面的Methods,我们就可以将messsage发送出去。 关于数据的接收,以及command,direct command(我们可以直接发送命令到EV3直接控制EV3)的内容,请看下一篇文章。 敬请期待! 未完待续! 欢迎交流! |
相关文章推荐
- Hacking EV3系列之四:BTstack与iOS通信
- 【iOS与EV3混合机器人编程系列之7】通过蓝牙控制EV3
- Hacking EV3系列之六:iOS使用Direct Command 控制EV3
- Hacking EV3系列之一:自编iOS app控制EV3之资料收集与分析
- 【iOS与EV3混合机器人编程系列之六】iOS_WiFi_EV3_Library 剖析之发送命令给EV3
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- IOS怎样使用MessageUI Framework 发送短信息
- iOS 怎样通过UDP发送广播寻找设备,拿到ip地址,然后用TCP 连接设备
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- 【iOS测试系列】instruments工具的使用(二)- 通过Time Profiler
- [置顶] iOS-通过Message框架开发一个Sticker Pack(无需代码)(第一篇)
- 【iOS与EV3混合机器人编程系列之二】工欲善其事,必先利其器(准备篇)
- ios 后台发送邮件之SKPSMTPMessage的使用
- iPhone消息推送机制(Push)实现及通过.net应用程序发送消息给ios应用程序
- iOS通过ASIHttpRequest接收php端发送的Json数据
- iOS 怎样通过UDP发送广播寻找设备,拿到局域网的设备ip 和 端口
- iOS打包机器人-通过Python轻松实现一键打包上传Fir.im并发送邮件通知测试人员
- IOS通过post方式发送图片
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)