物联网模组物理层数据混合在一起怎么搞提取
使用 C 语言提取你提供的所有类型数据字段的示例代码,整体思路是通过字符串处理函数对不同模块的字符串分别进行分割和解析,提取出各个参数及其对应的值
举例CAT1模组的物理层数据:
c
+ECSTATUS: PHY, DlEarfcn:1850, UlEarfcn:19850, PCI:1, Band:3, RSRP:-95, RSRQ:-10, SNR:9, DlBler:0/100, UlBler:0/100, DataInactTimerS:60, RetxBSRTimerP:320, TAvalue:20, TxPower:14, MaxTxPower:23, rxTime:1410, txTime:70
+ECSTATUS: L2, SrbNum:2, DrbNum:1
+ECSTATUS: RRC, State:"CONNECTED", TAC:30559, CellId:125637176, DRX:1280
+ECSTATUS: EMM, EmmState:"REG", EmmMode:"CONNECTED", PTWMs:0, EDRXPeriodMs:0, PsmExT3412TimerS:4294967295, T3324TimerS:65535, T3346RemainTimeS:0
+ECSTATUS: PLMN, PlmnState:"SELECTED", PlmnType:"EHPLMN", SelectPlmn:"0x460,0xf001"
+ECSTATUS: ESM, ActBearerNum:1, APN:"sxzcat1.MNC006.MCC460.GPRS", IPv4:"10.198.148.55"
+ECSTATUS: CCM, Cfun:1, IMSI:"460065244629186"
实际函数实现:
c
//字符串分割提取
void extractRRCData(char *rrcData)
{
char *token; //引入开头
char *delim = ", "; //通过此delim 分割的字符
token = strtok(rrcData, delim);//获取开头
// 跳过开头的 "+ECSTATUS: RRC"
token = strtok(NULL, delim);//跳过
while (token!= NULL)
{
char paramName[100];
char paramValue[100];
sscanf(token, "%[^:]:%s", paramName, paramValue);//提取参数名称 参数
printf("%s: %s\n", paramName, paramValue);//打印参数名称 打印参数
token = strtok(NULL, delim);//返回下一段索引
}
}
各个提取函数的逻辑:
对于每个ext
ractXXXData函数(如extractPhyData、extractL2Data等),首先使用strtok函数按照指定的分隔符(, )对传入的对应模块字符串进行分割。
跳过开头表示模块名称的那部分字符串(如+ECSTATUS: PHY),然后通过sscanf函数按照%[^:]:%s的格式来解析出参数名和参数值,并进行打印输出,输出时会加上对应模块的前缀(如物理层 -、层2 -等)方便区分查看。
main函数中的操作:
在main函数里定义了各个模块对应的原始字符串数据,然后依次调用各个提取函数来提取并输出相应模块的所有数据字段内容。
实际效果
c
+ECSTATUS: PHY, DlEarfcn:1506, UlEarfcn:19506, PCI:83, Band:3, RSRP:-97, RSRQ:-11, SNR:4, DlBler:0/100, UlBler:0/100, DataInactTimerS:0, RetxBSRTimerP:0, TAvalue:-1, TxPower:-128, MaxTxPower:23, rxTime:2079, txTime:189
+ECSTATUS: L2, SrbNum:0, DrbNum:0
+ECSTATUS: RRC, State:"IDLE", TAC:30559, CellId:117016381, DRX:1280
+ECSTATUS: EMM, EmmState:"REG", EmmMode:"IDLE", PTWMs:0, EDRXPeriodMs:0, PsmExT3412TimerS:4294967295, T3324TimerS:65535, T3346RemainTimeS:0
+ECSTATUS: PLMN, PlmnState:"SELECTED", PlmnType:"EHPLMN", SelectPlmn:"0x460,0xf001"
+ECSTATUS: ESM, ActBearerNum:1, APN:"sxzcat1.MNC006.MCC460.GPRS", IPv4:"10.74.206.168"
+ECSTATUS: CCM, Cfun:1, IMSI:"460065244629186"
OK
PHY:
DlEarfcn: 1506
UlEarfcn: 19506
PCI: 83
Band: 3
RSRP: -97
RSRQ: -11
SNR: 4
DlBler: 0/100
UlBler: 0/100
DataInactTimerS: 0
RetxBSRTimerP: 0
TAvalue: -1
TxPower: -128
MaxTxPower: 23
rxTime: 2079
txTime: 189
L2: 189
SrbNum: 0
DrbNum: 0
RRC: 0
State: "IDLE"
TAC: 30559
CellId: 117016381
DRX: 1280
EMM: 1280
EmmState: "REG"
EmmMode: "IDLE"
PTWMs: 0
EDRXPeriodMs: 0
PsmExT3412TimerS: 4294967295
T3324TimerS: 65535
T3346RemainTimeS: 0
PLMN: 0
PlmnState: "SELECTED"
PlmnType: "EHPLMN"
SelectPlmn: "0x460
0xf001"
+ECSTATUS: "0x460
ESM: "0x460
ActBearerNum: 1
APN: "sxzcat1.MNC006.MCC460.GPRS"
IPv4: "10.74.206.168"
CCM: "10.74.206.168"
Cfun: 1
IMSI: "460065244629186"