MWC飞控V2.3串口通信协议
在V2.3版的change.txt中有这么一段话:
main changes 2.2 -> 2.3
...
RCSERIAL is now deeply integrated. If a MSP_SET_RAW_RC comes, it will just override legacy RX data. (r1420)
as a consequence, RCSERIAL is no more an option
在这之前,想用串口控制飞控的方法是去选择RCSERIAL这个预编译选项等等,现在 RCSERIAL 已经被深度集成,如果我们给飞控发送一个MSP_SET_RAW_RC 指令同时带上控制数据,那就可以控制飞控了。于是RCSERIAL 不复存在,这就要求我们要去了解串口协议。
关于串口协议的一段话:
updated 04 July
Multiwii serial protocol was redesigned://优点等等
• to be light, as before
• to be generic: it can be used transparenlty by a GUI, OSD, telemetry or home made config tool.
ie no more specific OSD code should be coded in multiwii
• to be bit wire efficient: only requested data are transmitted in a binary format
• to be quite secure: data are sent with a checksum, preventing corrupted configuration to be injected.
• to be header sensitive: as it is designed with a specific header, it can be mixed with other frame, like GPS frame
ie, it will be possible to connect either a GUI or a GPS on the same serial port without changing the conf
• to be less sensitive to evolutions:
ie in case of parameter evolution, the main protocol will remain compatible and the GUI will be much less version dependent.
variable data length allows to consider only the beginning of a message, leaving the other octets at the end to extend transparently the message (for instance to add a new PID)
I thought first about an implementation of Mavlink, but I think it's not what I was looking for.
Even with a partial implementation, the predefined structures are not light enough for what I have in mind.
Some messages are however inspired from mavlink structure.
The main rule remains: Multiwii never sends something on its own.
A request must be done in each case to retrieve or set data.
Each messages received are acknowledged even if there is no data inside.
There are 2 main messages to consider:
•request message to multiwii
•multiwii output message
//一句话:一问一答,不问不答
/*重点来了:指令格式*/
request message to multiwii //從飞控中讀取信息
To request simple data without parameters / send a specific command / inject new parameters in multiwii
messages are formated like this:
$M>[data length][code][data][checksum]
1 octet '$'
1 octet 'M'
1 octet '<'
1 octet [data length]
1 octet [code]
several octets [data]
1 octet [checksum]
[data length] can be 0 in case of no param command //也就是说如果只发一个指令不带数据的话数据长度可以为0
multiwii output message //向飞控输出信息
messages are formated like this:
$M>[data length][code][data][checksum]
1 octet '$'
1 octet 'M'
1 octet '>'
1 octet [data length]
1 octet [code]
several octets [data]
1 octet [checksum]
if the message is unknown://未知信息
$M|[0][code][checksum]
1 octet '$'
1 octet 'M'
1 octet '|'
1 octet 0
1 octet [unknown code]
1 octet [checksum]
总结下:
- 读写信息格式:前綴 ’$M‘ 加 '<'或'>' (分別代表讀寫) 加 数据长度 加 指令代码 加 实际数据 加 校验码
以上均已字节为单位,且不含空格
- 前綴换算成16进制的指令格式长这样:
读:24 4D 3C //$M<
写:24 4D 3E //$M>
- 关于校验码
参照官方的MultiWiiConf程序,我写了一个用于计算校验码的C++函数(要求数据长度大于0,如果等于0校验码与指令代码相同)
byte getChecksum(byte length,byte cmd,byte mydata[]) //三个参数分别为: 数据长度 , 指令代码 , 实际数据数组
typedef unsigned char byte; byte getChecksum(byte length,byte cmd,byte mydata[]){ byte checksum=0; checksum ^= (length&0xFF); checksum ^= (cmd&0xFF); for(int i=0;i<length;i++) { checksum ^= (mydata[i]&0xFF); } return checksum; }
- 注意波特率要选115200
- 示例:串口以16进制发送:24 4D 3C 00 64 64 ,飞控将返回版本信息等等数据。
00表示数据长度为0;
第一个64为指令代码,即下面网址上的第一个代码
第一个64为校验码
其它:
- 飞控上的程序在Protocol.cpp中进行串口通信处理,具体函数为serialCom()
- 官方上的协议资料http://www.multiwii.com/wiki/index.php?title=Multiwii_Serial_Protocol可以了解到指令代码(message_id),和指令方向以及指令内容等内容