Futaba S-BUS协议解析
2016-12-22 00:11
671 查看
一、协议说明
S-BUS协议实际上通过串口进行数据发送。但是需要注意的是,S-BUS的逻辑电平是反的,需用如下电路对电平反相,再接到串口接收的管脚。
串口配置为波特率100kbps( 100000 ),8位数据,偶校验(even),2位停止位,无流控。
每帧25个字节,帧格式如下【参考https://mbed.org/users/Digixx/notebook/futaba-s-bus-controlled-by-mbed/】:
[Start Byte] [Data0] [Data1] …. [Data21] [Flags][End Byte]
两帧之间的时间间隔4ms(高速模式),约7ms一帧。
其中:
Start Byte = 0x0F。中间22个字节为16个通道的数据,每个通道用 11 bit表示,范围是0-2047。
End Byte根据S-BUS协议版本不同而不同。
Flags的定义:
bit7 = ch17 = digital channel (0x80)
bit6 = ch18 = digital channel (0x40)
bit5 = Frame lost, equivalent red LED on receiver (0x20)
bit4 = failsafe activated (0x10)
bit3 = n/a
bit2 = n/a
bit1 = n/a
bit0 = n/a
数据部分:
如果把22个字节看作一个数的话,则数据部分采用小端模式。即若把数据按照[Data21][Data20]…[Data1][Data0]的顺序排列,则[Data21]的 bit 7 为MSB,[Data0]的 bit 0 为LSB,每 11 bit 为一个通道的数据。Data[21]和[Data20]的高3位为通道16的值,以此类推。
借用一张图,改了改:
下面是一组数据的例子(来自 R7008SB):
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 04
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 14
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 24
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 34
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 04
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 14
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 24
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 34
二、S-BUS解析程序
感谢来自
taogashi【http://www.eefocus.com/taogashi/blog/14-05/303577_f140d.html】
以及
Uwe Gartmann【https://developer.mbed.org/users/Digixx/notebook/futaba-s-bus-controlled-by-mbed/】
的有关S-BUS协议的介绍。
S-BUS协议实际上通过串口进行数据发送。但是需要注意的是,S-BUS的逻辑电平是反的,需用如下电路对电平反相,再接到串口接收的管脚。
串口配置为波特率100kbps( 100000 ),8位数据,偶校验(even),2位停止位,无流控。
每帧25个字节,帧格式如下【参考https://mbed.org/users/Digixx/notebook/futaba-s-bus-controlled-by-mbed/】:
[Start Byte] [Data0] [Data1] …. [Data21] [Flags][End Byte]
两帧之间的时间间隔4ms(高速模式),约7ms一帧。
其中:
Start Byte = 0x0F。中间22个字节为16个通道的数据,每个通道用 11 bit表示,范围是0-2047。
End Byte根据S-BUS协议版本不同而不同。
Flags的定义:
bit7 = ch17 = digital channel (0x80)
bit6 = ch18 = digital channel (0x40)
bit5 = Frame lost, equivalent red LED on receiver (0x20)
bit4 = failsafe activated (0x10)
bit3 = n/a
bit2 = n/a
bit1 = n/a
bit0 = n/a
数据部分:
如果把22个字节看作一个数的话,则数据部分采用小端模式。即若把数据按照[Data21][Data20]…[Data1][Data0]的顺序排列,则[Data21]的 bit 7 为MSB,[Data0]的 bit 0 为LSB,每 11 bit 为一个通道的数据。Data[21]和[Data20]的高3位为通道16的值,以此类推。
借用一张图,改了改:
下面是一组数据的例子(来自 R7008SB):
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 04
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 14
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 24
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 34
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 04
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 14
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 24
0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 34
二、S-BUS解析程序
void Process(uint8_t* raw,uint16_t* result) { uint8_t bitsToRead=3; // bitsToRead表示需要从下一个字节中读取多少bit。规律:bitsToRead 每次总是增加 3 uint8_t bitsToShift; uint8_t startByte=21; uint8_t channelId=15; do { result[channelId]=raw[startByte]; if(bitsToRead<=8) { result[channelId]<<=bitsToRead; bitsToShift=8-bitsToRead; result[channelId]+=(raw[startByte-1]>>bitsToShift); } else { result[channelId]<<=8; result[channelId]+=raw[startByte-1]; startByte--; bitsToRead-=8; bitsToShift=8-bitsToRead; result[channelId]+=(raw[startByte-1]>>bitsToShift); } result[channelId]&=0x7FF; channelId--; startByte--; bitsToRead+=3; }while(startByte>0); }
感谢来自
taogashi【http://www.eefocus.com/taogashi/blog/14-05/303577_f140d.html】
以及
Uwe Gartmann【https://developer.mbed.org/users/Digixx/notebook/futaba-s-bus-controlled-by-mbed/】
的有关S-BUS协议的介绍。
相关文章推荐
- Futaba S-BUS controlled by mbed(使用mbed控制Futaba S-BUS)
- JqueryDataTable的使用
- MySQL的一个安装坑
- maven pom.xml详解
- android提供的算法工具类
- 使用PreperedStatement预编译对象防止sql注入简单代码
- 为什么用static来修饰方法或者变量?用处?
- php中使用fsockopen实现异步请求
- Spring+SpringMVC+MyBatis整合实例
- PHP的strcmp函数引发的安全问题
- GO语言学习笔记三
- java中的形参为引用类型的问题
- 数据库时间(datetime类型)的存储和读取
- 2016滴滴出行研发工程师笔试题(亮灯问题)
- 46. Permutations
- Josephus ring
- Main() 方法的参数
- Atitit ABI FFI 的区别与联系 attilax总结
- Linux上MySQL上root密码修改
- 微信小程序开发中的疑问解答汇总