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),和指令方向以及指令內容等內容