说明前面讲到用通讯解析框架一般mcu或者其他的嵌入式系统或多或少涉及到通讯不光有各种各样的字节指令还有字符串指令比如本文要讲的AT指令很多模块都涉及使用at指令作为指令集去做命令比如电话模块wifi模块蓝牙模块等等一般at指令解析比较繁琐如果用到统一的框架做解析呢
注通讯解析框架的介绍可以看之前我写的文章内容这里不再赘述
下面我们着重讲解使用通讯解析框架解析at指令以汉枫wifi模块为例h框架文件typedefstructU8Readbuf读内容U16BufLen读内容长度U8isUp主动上传还是下载intYALWifiCmdPos其他命令位置YALWifiCmdPM读数据地址typedefstructconstcharBufat内容U8HandleYALWifiCmdPMbuf长度U16GetID获取的CMDIdYALWifiCMDHandle读数据框架结构enumYALWIFICMDGETENTERAT1进入ATYALWIFICMDGETANAENTERAT应答进入ATYALWIFICMDSETCLOSESHOWAT关闭回显
C内容文件U8GetYALWIFIEnterAtYALWifiCmdPMCmdPmU8GetYALWIFIAckEnterAtYALWifiCmdPMCmdPmU8GetYALWIFICloseShowAtYALWifiCmdPMCmdPm
U8YALWIFILINKENDr结尾获取指令constU8YALWIFIREQATconstYALYALWifiCMDHandleYALWIFICmdhandleGetYALWIFIEnterAtYALWIFICMDGETENTERAT进入ATaGetYALWIFIAckEnterAtYALWIFICMDGETANAENTERAT应答ATEGetYALWIFICloseShowAtYALWIFICMDSETCLOSESHOWAT关闭回显
intYALWifiCMDfindCmdPosU16CmdintiU8CmdCtsizeofYALWIFICmdhandlesizeofYALWIFICmdhandle0fori0iisGetreturnYALWIFIReadCMDCmdPmYALWifiCmdPoselseifStrcmpCmdPmReadbufU8a1err0returnerr函数名称GetYALWIFIMacID描述获取WiFi版本号和MACID输入形参返回值无U8GetYALWIFIAckEnterAtYALWifiCmdPMCmdPmu8err1ifCmdPmisGetreturnYALWIFIReadCMDCmdPmYALWifiCmdPoselseifYALWifiCmdisOkCmdPmReadbufSetAtMode1err0returnerr函数名称GetYALWIFICloseShowAt描述关闭回显输入形参返回值无U8GetYALWIFICloseShowAtYALWifiCmdPMCmdPmu8err1ifCmdPmisGetU8infostr10转义字符处理memsetinfostr0sizeofinfostrsprintfscharinfostrsizeofinfostroffsYALWIFILINKENDstrcatcharinfostrcharYALWIFILINKENDreturnYALWIFIReadWriteCMDCmdPmYALWifiCmdPosinfostrelseifYALWifiCmdisOkCmdPmReadbuferr0returnerr函数名称YALWIFIReadCMD描述读WiFi指令输入形参YALWifiCmdPos列表位置返回值0成功1失败u8YALWIFIReadCMDintYALWifiCmdPosifCheckCmdPosYALWifiCmdPosreturn0
memcpySENDBUFYALWIFICmdhandleYALWifiCmdPosBufstrlencharYALWIFICmdhandleYALWifiCmdPosBufUSART1SendStringSENDBUFstrlencharSENDBUF
return0intYALWifiCMDfindCmdPosU16CmdintiU8CmdCtsizeofYALWIFICmdhandlesizeofYALWIFICmdhandle0fori0iCmdCtiifCmdYALWIFICmdhandleiGetIDreturnireturn1解析at指令将字符串数据内容解析到该函数中U8YALAnaDatarevcmdU8BufU16BufLenintYALWifiCmdPosYALWifiCmdPMcmdPmYALWifiCmdPosYALWifiCMDfindCmdPosGetLastCmdId上一次请求的内容ifCheckCmdPosYALWifiCmdPosreturn1cmdPmReadbufBufcmdPmBufLenBufLencmdPmYALWifiCmdPosYALWifiCmdPoscmdPmisGet0returnYALWIFICmdhandleYALWifiCmdPosHandlecmdPm回调函数发送cmd消息通过CMDId请求对应的at指令voidYALWifiCMDSetCmdU16CmdintWifiCmdPosYALWifiCmdPMcmdPmcmdPmReadbufSendBufcmdPmBufLen0cmdPmisGet1WifiCmdPosWifiCMDfindCmdPosCmdifWifiCmdPos1returnifCheckCmdPosWifiCmdPosreturncmdPmWifiCmdPosWifiCmdPosYALWIFICmdhandleWifiCmdPosHandlecmdPmSetLastCmdIdYALWIFICmdhandleWifiCmdPosGetID总结通过框架将AT指令的头尾等公共部分作为框架一部分可变的内容部分作为识别的内容实体用于解析和发送比如作为识别码ATE中的E作为识别码识别解析的内容通过各种的handle回调处理当发送一条AT指令对应的回一条AT指令解析和发送对应LastCmdId的关系去解对应的回应的AT指令内容当然AT指令多变本文只是抛砖引玉可相互交流欢迎评论转发加关注