cpp
#include <string>
#include <json/json.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include "xml/tinyxml.h"
#include "json/json.h"
struct Commoninfo
{
int ChanCode;
int ChanType;
int DataProtocol;
int DataLen;
int FunCode;
int SlaveId;
int AddrBegin;
int AddrInterval;
int AliasBegin;
int AliasInterval;
int modbusitemlen;
int DataTypeRegister;
string Coefficient;
int WayUid;
};
void writeFileJson(Json::Value &root )
{
//根节点
Json::StyledWriter sw;
cout << sw.write(root) << endl;
//输出到文件
ofstream os("./result.json");
//os.open("result.json", std::ios::out | std::ios::app);
if (!os.is_open())
cout << "error:can not find or create the file which named \" demo.json\"." << endl;
os << sw.write(root);
os.close();
}
int main(int argc, char *argv[])
{
Commoninfo settingInfo;
TiXmlDocument doc;
if(!doc.LoadFile("./mosbusconfig.xml"));
{
TiXmlElement* root = doc.RootElement();
TiXmlElement* commonNode = root->FirstChildElement("common");
commonNode->FirstChildElement("ChanCode")->QueryIntText(&settingInfo.ChanCode);
commonNode->FirstChildElement("ChanType")->QueryIntText(&settingInfo.ChanType);
commonNode->FirstChildElement("DataProtocol")->QueryIntText(&settingInfo.DataProtocol);
commonNode->FirstChildElement("DataLen")->QueryIntText(&settingInfo.DataLen);
commonNode->FirstChildElement("FunCode")->QueryIntText(&settingInfo.FunCode);
commonNode->FirstChildElement("SlaveId")->QueryIntText(&settingInfo.SlaveId);
commonNode->FirstChildElement("AddrBegin")->QueryIntText(&settingInfo.AddrBegin);
commonNode->FirstChildElement("AddrInterval")->QueryIntText(&settingInfo.AddrInterval);
commonNode->FirstChildElement("AliasBegin")->QueryIntText(&settingInfo.AliasBegin);
commonNode->FirstChildElement("AliasInterval")->QueryIntText(&settingInfo.AliasInterval);
commonNode->FirstChildElement("modbusitemlen")->QueryIntText(&settingInfo.modbusitemlen);
commonNode->FirstChildElement("WayUid")->QueryIntText(&settingInfo.WayUid);
commonNode->FirstChildElement("DataTypeRegister")->QueryIntText(&settingInfo.DataTypeRegister);
settingInfo.Coefficient = commonNode->FirstChildElement("Coefficient")->GetText();
}
Json::Value root;
root["ChanCode"] = std::to_string(settingInfo.ChanCode);
root["ChanDesc"] = "";
root["ChanType"] = std::to_string(settingInfo.ChanType);
root["DataProtocol"] = std::to_string(settingInfo.DataProtocol);
Json::Value NetModNode;
Json::Value NetModTcpItem;
int jAlias = 0;
NetModTcpItem["DataLen"] = std::to_string(settingInfo.DataLen);
NetModTcpItem["DevName"] = "1301";
NetModTcpItem["FunCode"] = std::to_string(settingInfo.FunCode);
NetModTcpItem["RegisterBegin"] = std::to_string(settingInfo.AddrBegin);
Json::Value resuletitem;
for(int i = 0; i < settingInfo.DataLen;)
{
Json::Value item;
item["Addr"] = std::to_string(settingInfo.AddrBegin + i);
item["Alias"] = std::to_string(settingInfo.AliasBegin + jAlias);
item["Coefficient"] = settingInfo.Coefficient;
item["DataLen"] = std::to_string(settingInfo.modbusitemlen);
item["DataTypeRegister"] = std::to_string(settingInfo.DataTypeRegister);
i += settingInfo.AddrInterval;
jAlias += settingInfo.AliasInterval;
resuletitem.append(item);
}
//resuletitem.append(item);
NetModTcpItem["Results"] = resuletitem;
NetModTcpItem["SlaveId"] = std::to_string(settingInfo.SlaveId);
NetModNode.append(NetModTcpItem);
root["NetModTcp"] = NetModNode;
root["WayUid"] = std::to_string(settingInfo.WayUid);
writeFileJson(root);
return 0;
}
mosbusconfig.xml
<?xml version="1.0"?>
<root>
<common>
<!-- 和LOCALGATEWAY.xml中配置对应 -->
<ChanCode>10</ChanCode>
<!-- 和LOCALGATEWAY.xml中配置对应 -->
<ChanType>7</ChanType>
<!-- 和LOCALGATEWAY.xml中配置对应,modbus一般为6 -->
<DataProtocol>6</DataProtocol>
<!-- 采集的modbus寄存器长度:例采集寄存器地址1-200的 DateLen为200 -->
<DataLen>200</DataLen>
<!-- 对应modbus FunCode 1 开关量 3模拟量-->
<FunCode>01</FunCode>
<!-- 对应modbus SlaveId -->
<SlaveId>1</SlaveId>
<!-- 对应modbus 寄存器起始地址 -->
<AddrBegin>40001</AddrBegin>
<!-- 一般情况下如是连续采集 FunCode为01 02 值为 1 FunCode为03 值为 2,
非连续采集,按照实际间隔配置 -->
<AddrInterval>1</AddrInterval>
<!-- 对应起始地址hldkeymap中的KeyValue -->
<AliasBegin>60001</AliasBegin>
<!-- 对应起始地址hldkeymap中的KeyValue -->
<AliasInterval>1</AliasInterval>
<!-- 对应item中DataLen的长度 一般情况 FunCode为01 02值为 1 FunCode为03 值为 2 -->
<modbusitemlen>1</modbusitemlen>
<!-- 计算公式,默认为 0,1,0-->
<Coefficient>0,1,0</Coefficient>
<!-- enum RegisterType 数值类型 FunCode 01 值为0 FunCode 03值为 3 -->
<DataTypeRegister>1</DataTypeRegister>
<!-- 和LOCALGATEWAY.xml中配置对应 -->
<WayUid>1575876387</WayUid>
</common>
</root>
result.json
{
"ChanCode" : "10",
"ChanDesc" : "",
"ChanType" : "7",
"DataProtocol" : "6",
"NetModTcp" : [
{
"DataLen" : "4",
"DevName" : "1301",
"FunCode" : "1",
"RegisterBegin" : "40001",
"Results" : [
{
"Addr" : "40001",
"Alias" : "40001",
"Coefficient" : "0,1,0",
"DataLen" : "1",
"DataTypeRegister" : "3"
},
{
"Addr" : "40002",
"Alias" : "40002",
"Coefficient" : "0,1,0",
"DataLen" : "1",
"DataTypeRegister" : "3"
}
],
"SlaveId" : "1"
}
],
"WayUid" : "1575876387"
}