前言
幹活中用到了深度解算板,寫了相應的驅動程序,放出來。
這個模塊本身的協議比較簡單,所以模塊的驅動也很小巧。
驅動被設計爲擁有指令器和接收機兩個部分,完全被動方式運行,與具體的平臺解耦,只支持串口通訊。
深度傳感器解算板V1.0簡介
概述
深度傳感器器解算板。⽤於讀回深度傳感器器的測量信息,轉爲串口輸出。
深度解算板⽀持接入ROVMAKER的深度傳感器線序,適⽤於MS5837型號深度傳感器。確認線序後將傳感器接入解算板的任一1.25mm防反接座,目前只支持同時接入一個MS5837深度傳感器,連接解算板到外部主控制器。
解算板和傳感器需在水面上電。上電後解算板會測量出環境空氣的壓力值標爲深度輸出的零點。上電後⽆傳感器故障,解算板即開始輸出深度溫度信息。
通信協議
輸出格式
解算板輸出字符串格式: T=XX.XXD=XX.XX\r\n
浮點數溫度輸出 T 單位:攝⽒度(°C)
浮點數深度輸出 D 單位:米(m)
例: T=25.27D=1.21 溫度爲25.27°C深度爲1.21m
控制指令
解算板可通過串口發送字符串配置解算板參數。
- !Fxxxx\r\n :
xxxx爲設置的液體密度 設置液體密度:設置所測⽔水的密度。單位:kg/m3。
通常淡水中密度設置爲997(kg/m3),海水密度(1029kg/m3)。 解算板默認設置爲997kg/m3。 - !Lx\r\n:
x爲LED運⾏狀態 運⾏指示燈狀態:可設置爲關閉/常亮/閃爍。 默認爲閃爍
0 LED關閉;1 LED常亮;2 LED閃爍。
例: !L0\r\n 關閉運⾏指示燈
!L1\r\n 常亮運⾏指示燈
!L2\r\n 閃爍運⾏指示燈 - !Bxxxx\r\n :
xxxx爲設置的波特率 串口波特率設置:默認波特率爲115200bps,可通過發送指令修改波特率。通常不需要修改此參數。
例:!B9600\r\n 修改波特率爲9600bps。 當串口1波特率配置發⽣錯誤時,可通過預留串口2發送該指令配置串口1波特率。 - !Dxx.xx\r\n :
xx.xx 爲浮點數offset 深度輸出offset調整預留指令:浮點數,在當前深度輸出的數值上加上offset值。 例:
當前輸出深度-0.55,offset設置0.55,加上offset後深度輸出爲 -0.55+offset=0.00。 當前輸出深度0.1,offset設置-0.1,加上offset後深度輸出爲0.00 - !Txx.xx\r\n :
xx.xx 爲浮點數offset 溫度輸出offset調整預留指令:浮點數,在當前溫度輸出的數值上加上offset值。 例:
當前輸出溫度25.00,offset設置1,加上offset後深度輸出爲 25.00+offset=26.00。 當前輸出深度25.00,offset設置-1,加上offset後深度輸出爲24.00 - !!\r\n :
暫停傳感器器輸出,串口輸出所有參數。再次發送停⽌參數輸出,繼續傳感器輸出。串口輸出參數格式如下:
- !R\r\n :
復位解算板。 - !r\r\n :
恢復所有初始化設置。 - !C\r\n :
清除所有offset設置 參數配置後傳感器器解算板會保存當前設置,重新上電後配置數據仍然有效。 - !!\r\n:
暫停傳感器器輸出,串串⼝口輸出所有參數。再次發送停⽌止參數輸出,繼續傳感器器輸出。 - !R\r\n:
復位解算板。 - !r\r\n:
恢復所有初始化設置。 - !F1029\r\n:
設置液體密度參數。 - !L2\r\n:
設置運⾏狀態指示燈狀態。 - !B9600\r\n:
設置輸出串口波特率。 - !D0.200\r\n:
深度offset調整預留指令。 - !T0.200\r\n:
溫度offset調整預留指令。 - !C\r\n:
清除所有offset設置 。
驅動文件
M10Driver.h
/*
*******************************************************************************************
*
* M10 DRIVER MODULE
* M10驅動模塊
*
* File : M10Driver.h
* By : Lin Shijun(https://blog.csdn.net/lin_strong)
* Date: 2020/04/29
* version: V1.0
* History: 2020/04/29 V1.0 the prototype
* Note : The M10 driver is divided into two part, i.e. cmder and recver. The cmder is for
* sending the M10 command. The recver is for resolving data from the M10.
* lack "!!\r\n" command in this version intentionally, so that the receiver can
* only take care of the common data in form of "T=XX.XXD=XX.XX\r\n".
********************************************************************************************
*/
#ifndef M10DRIVER_H
#define M10DRIVER_H
/*
******************************************************************************************
* INCLUDE
******************************************************************************************
*/
#include <stdint.h>
/*
******************************************************************************************
* DEBUG CONFIGURATION
******************************************************************************************
*/
// to enable debug messages in this module
// #define M10_DEBUG
/*
******************************************************************************************
* TYPE DEFINE
******************************************************************************************
*/
typedef struct M10STRUCT_DATA{
float temp; // °C
float depth; // m
} M10Data;
typedef void (* M10FUNC_DATA)(M10Data data);
typedef enum {
M10LED_OFF = 0,
M10LED_ON = 1,
M10LED_TWINKLE = 2,
}M10LEDScheme;
/*
************************************************************************************
* INTERFACES
************************************************************************************
*/
void M10Cmder_Init(void (* outChannel)(uint8_t *buf, uint16_t len));
void M10Cmder_Destroy(void);
void M10Cmder_setLiquidDensity(uint16_t value);
void M10Cmder_setLEDScheme(M10LEDScheme s);
void M10Cmder_setBaudRate(uint32_t value);
void M10Cmder_setDepthOffset(float value);
void M10Cmder_setTempOffset(float value);
void M10Cmder_ClearOffset(void);
void M10Cmder_ResetBoard(void);
void M10Cmder_FactoryReset(void);
void M10Recver_Init(M10FUNC_DATA onRecvData);
// Call it to modify the call-back function.
void M10Recver_RegisterCallback(M10FUNC_DATA onRecvData);
void M10Recver_CleanupBuffer(void);
// Feed the receiver every byte received so that receiver can notify user
// the resolved data for each frame.
void M10Recver_Feed(uint8_t nextByte);
void M10Recver_Destroy(void);
#endif // of M10DRIVER_H
M10Recver.c
/*
*******************************************************************************************
*
* M10 RECEIVER MODULE
* M10驅動模塊 - 接收機
*
* File : M10Recver.c
* By : Lin Shijun(https://blog.csdn.net/lin_strong)
* Date: 2020/04/27
* version: V1.0
* History: 2020/04/27 V1.0 the prototype
* Note : The receiver has simple format check for data, which only checks for whether they
* are right characters. A more strict format check may be implemented in the further.
********************************************************************************************
*/
/*
*********************************************************************************************
* INCLUDES
*********************************************************************************************
*/
#include <stdlib.h>
#include <ctype.h>
#include "M10Driver.h"
#include "RxMac.h"
#ifndef M10_DEBUG
#undef _DEBUG
#endif
#include "DebugMsg.h"
/*
*********************************************************************************************
* LOCAL FUNCTION
*********************************************************************************************
*/
#define ARRAYSIZE(arr) (sizeof(arr)/ sizeof(arr[0]))
static void _onFlushed(RxMac sender,RxMacPtr buf,uint16_t len,RxState state,
RxFlag HorU,RxFlag Ender);
/*
*********************************************************************************************
* LOCAL VARIABLE
*********************************************************************************************
*/
static const char * _str_M10Recver = "M10Recver";
static M10FUNC_DATA _onRecvData;
static const uint8_t _flag_header[] = {(uint8_t)'T', (uint8_t)'='};
static const uint8_t _flag_ender[] = {(uint8_t)'\r', (uint8_t)'\n'};
static const RXFLAG_STRUCT _flags[] = {
{_flag_header, 2, RXFLAG_OPTION_STRONG_HEADER},
{_flag_ender , 2, RXFLAG_OPTION_STRONG_ENDER }
};
static RxMac _mac;
static uint8_t _macBuf[23];
/*
*********************************************************************************************
* INTERFACE IMPLEMENTATION
*********************************************************************************************
*/
void M10Recver_Init(M10FUNC_DATA onRecvData){
_onRecvData = onRecvData;
_mac = RxMac_Create(_flags, ARRAYSIZE(_flags), _macBuf, sizeof(_macBuf) - 1,
NULL, NULL, _onFlushed);
if(_mac == NULL)
for(;;)
;
}
void M10Recver_Destroy(){
_onRecvData = NULL;
RxMac_Destroy(_mac);
}
void M10Recver_RegisterCallback(M10FUNC_DATA onRecvData){
_onRecvData = onRecvData;
}
void M10Recver_CleanupBuffer(void){
RxMac_ResetState(_mac);
}
void M10Recver_Feed(uint8_t nextByte){
RxMac_FeedData(_mac, nextByte);
}
/*
*********************************************************************************************
* LOCAL FUNCTION IMPLEMENTATION
*********************************************************************************************
*/
static int _FloatFormatRight(const char *beg, const char *end){
const char *p;
if(end - beg <= 0)
return 0;
for(p = beg; end - p > 0; p++)
if(!isdigit(*p) && *p != '-' && *p != '.')
return 0;
return 1;
}
static void _onFlushed(RxMac sender,RxMacPtr buf,uint16_t len,RxState state,
RxFlag HorU,RxFlag Ender){
const char *pD;
M10Data data;
if(_onRecvData == NULL || !state.headerFound || !state.enderFound)
return;
buf[len] = '\0';
pD = (const char *)&buf[2];
while(pD[0] != '\r' && !(pD[0] == 'D' && pD[1] == '='))
++pD;
if(pD[0] != 'D' || !_FloatFormatRight((const char *)&buf[2], pD) ||
!_FloatFormatRight((const char *)&pD[2], (const char *)buf + len - 2)){
_dbg_printf2("%s: got corrupt frame-%s", _str_M10Recver, (const char *)buf);
return;
}
data.temp = (float)atof((const char *)&buf[2]);
data.depth = (float)atof((const char *)&pD[2]);
_dbg_printf3("%s: got data-temp(%.2f),depth(%.2f)\r\n", _str_M10Recver, data.temp, data.depth);
_onRecvData(data);
}
M10Cmder.c
/*
*******************************************************************************************
*
* M10 COMMANDER MODULE
* M10驅動模塊 - 指令器
*
* File : M10Cmder.c
* By : Lin Shijun(https://blog.csdn.net/lin_strong)
* Date: 2020/04/29
* version: V1.0
* History: 2020/04/29 V1.0 the prototype
* Note :
********************************************************************************************
*/
/*
*********************************************************************************************
* INCLUDES
*********************************************************************************************
*/
#include <stdio.h>
#include "M10Driver.h"
/*
*********************************************************************************************
* LOCAL FUNCTION DECLARATION
*********************************************************************************************
*/
static void _sendCmd(char cmdByte, const char *paramStr);
/*
*********************************************************************************************
* LOCAL VARIABLE
*********************************************************************************************
*/
static void (* _out)(uint8_t *buf, uint16_t len) = NULL;
/*
*********************************************************************************************
* INTERFACE IMPLEMENTATION
*********************************************************************************************
*/
void M10Cmder_Init(void (* outChannel)(uint8_t *buf, uint16_t len)){
_out = outChannel;
}
void M10Cmder_Destroy(){
_out = NULL;
}
void M10Cmder_setLiquidDensity(uint16_t value){
char buf[8];
(void)sprintf(buf, "%u", value);
_sendCmd('F', buf);
}
void M10Cmder_setLEDScheme(M10LEDScheme s){
char buf[8];
if(s > M10LED_TWINKLE)
return;
(void)sprintf(buf, "%u", s);
_sendCmd('L', buf);
}
void M10Cmder_setBaudRate(uint32_t value){
char buf[14];
(void)sprintf(buf, "%lu", (unsigned long)value);
_sendCmd('B', buf);
}
void M10Cmder_setDepthOffset(float value){
char buf[14];
// Note: may overflow here, consider snprintf
(void)sprintf(buf, "%.2f", value);
_sendCmd('D', buf);
}
void M10Cmder_setTempOffset(float value){
char buf[14];
// Note: may overflow here
(void)sprintf(buf, "%.2f", value);
_sendCmd('T', buf);
}
void M10Cmder_ClearOffset(void){
_sendCmd('C', NULL);
}
void M10Cmder_ResetBoard(void){
_sendCmd('R', NULL);
}
void M10Cmder_FactoryReset(void){
_sendCmd('r', NULL);
}
/*
*********************************************************************************************
* LOCAL FUNCTION IMPLEMENTATION
*********************************************************************************************
*/
static void _sendCmd(char cmdByte, const char *paramStr){
int len;
static uint8_t buf[20];
if(_out == NULL)
return;
if(paramStr == NULL)
paramStr = "";
len = sprintf((char *)buf, "!%c%s\r\n", cmdByte, paramStr);
_out(buf, len);
}
依賴
接收器的實現依賴於我寫的通用接收機:
通用接收狀態機模塊
至於DebugMsg.h則是我自己使用的調試信息的模塊:
#ifndef _DEBUG_MSG_H
#define _DEBUG_MSG_H
#include <stdio.h>
#ifdef _DEBUG
#define _dbg_printf0(format) ((void)printf(format))
#define _dbg_printf1(format,p1) ((void)printf(format,p1))
#define _dbg_printf2(format,p1,p2) ((void)printf(format,p1,p2))
#define _dbg_printf3(format,p1,p2,p3) ((void)printf(format,p1,p2,p3))
#define _dbg_printf4(format,p1,p2,p3,p4) ((void)printf(format,p1,p2,p3,p4))
#define _dbg_printf5(format,p1,p2,p3,p4,p5) ((void)printf(format,p1,p2,p3,p4,p5))
#define _dbg_printf6(format,p1,p2,p3,p4,p5,p6) ((void)printf(format,p1,p2,p3,p4,p5,p6))
#define _dbg_printf7(format,p1,p2,p3,p4,p5,p6,p7) \
((void)printf(format,p1,p2,p3,p4,p5,p6,p7))
#define _dbg_printf8(format,p1,p2,p3,p4,p5,p6,p7,p8) \
((void)printf(format,p1,p2,p3,p4,p5,p6,p7,p8))
#define _dbg_printf9(format,p1,p2,p3,p4,p5,p6,p7,p8,p9) \
((void)printf(format,p1,p2,p3,p4,p5,p6,p7,p8,p9))
#else
#define _dbg_printf0(format)
#define _dbg_printf1(format,p1)
#define _dbg_printf2(format,p1,p2)
#define _dbg_printf3(format,p1,p2,p3)
#define _dbg_printf4(format,p1,p2,p3,p4)
#define _dbg_printf5(format,p1,p2,p3,p4,p5)
#define _dbg_printf6(format,p1,p2,p3,p4,p5,p6)
#define _dbg_printf7(format,p1,p2,p3,p4,p5,p6,p7)
#define _dbg_printf8(format,p1,p2,p3,p4,p5,p6,p7,p8)
#define _dbg_printf9(format,p1,p2,p3,p4,p5,p6,p7,p8,p9)
#endif
int dummyprintf(const char *format, ...);
#endif
這樣在項目中同時宏定義 _DEBUG 和 M10_DEBUG 時纔會打印調試信息。不需要的話刪除相關語句就好。
想要了解相關技巧的話詳見:
C語言宏配置的各種奇淫技巧
使用示例
使用此模塊基本分幾步:
- 初始化模塊
- 如需要發送指令則實現並註冊信道給指令器;註冊回調函數給接收器,這樣接收器在發現完整的數據幀時就會通過回調函數進行通知。
- 不斷接收數據,並喂(Feed)給接收器;如需要發指令,隨時調用指令器的接口。
以下代碼示例了在上電後不斷讀取數據並進行解析(假設使用標準輸入輸出上的串口和深度解算板通信):
#include "M10Driver.h"
#include <stdio.h>
#include <stdint.h>
……
// 收到數據的回調函數
static void onDataResolved(M10Data data);
// 發送信道
static void sendChannel(uint8_t *buf, uint16_t len);
void main(){
uint8_t b;
……
M10Recver_Init(onDataResolved);
M10Cmder_Init(sendChannel);
// 如果需要的話發送對應指令
// M10Cmder_ResetBoard();
for(;;){
// 不斷得到下一個字節並餵給接收機
// 接收機會通過回調函數實時傳遞解析出來的值
b = (uint8_t)getchar();
M10Recver_Feed(b);
}
}
static void onDataResolved(M10Data data){
// data中就是實時解析到的數據,具體怎麼使用是你的事
}
static void sendChannel(uint8_t *buf, uint16_t len){
// 往標準輸出輸出這個字節
while(len-- > 0)
putchar((char)*buf++);
}
更新歷史
2020/06/06 放出V1.0