-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.cpp
133 lines (119 loc) · 4.07 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "config.h"
extern char gdir[256] ;
Config::Config()
{
}
bool Config::load()
{
using boost::property_tree::ptree;
ptree pt ;
char dir[256];
strcpy(dir,gdir);
#if defined(_WIN32)
strcat(dir,"\\modbus.xml");
#else
strcat(dir,"/modbus.xml");
#endif
//read_xml("modbus.xml",pt);
read_xml(dir,pt);
bus_number = pt.get<int>("Config.BusTotal");
BOOST_FOREACH(ptree::value_type &v,pt.get_child("Config.Lines"))
{
Bus bus ;
bus.port = v.second.get<int>("Comm.Port");
#if defined(_WIN32)
sprintf(bus.sPort,"COM%d",bus.port);
#else
sprintf(bus.sPort,"/dev/ttyS%d",bus.port-1);
#endif
bus.baud = v.second.get<int>("Comm.Baud");
bus.databits = v.second.get<int>("Comm.DataBits");
bus.stopbits = v.second.get<int>("Comm.StopBits");
bus.parity = v.second.get<char>("Comm.Parity");
BOOST_FOREACH(ptree::value_type &v1,v.second.get_child("Modules"))
{
Module m ;
m.addr = v1.second.get<int>("Addr");
BOOST_FOREACH(ptree::value_type &v2,v1.second.get_child("Reqs"))
{
Request r ;
r.reqType = v2.second.get<int>("ReqType");
r.reg = v2.second.get<int>("ReqRegister");
r.num = v2.second.get<int>("ReqNum");
BOOST_FOREACH(ptree::value_type &v3,v2.second.get_child("DataParses"))
{
Parse p ;
p.dataOffset = 0 ;
p.dataNums = v3.second.get<int>("DataNums");
p.baseVar = v3.second.get<float>("BaseVar");
p.dataOrder = v3.second.get<int>("DataOrder");
p.dataSize = v3.second.get<int>("DataSize");
p.dataType = v3.second.get<int>("DataType");
p.deadBand = v3.second.get<float>("DeadBand");
p.mulVar = v3.second.get<float>("MulVar");
p.startIndex =v3.second.get<int>("StartIndex");
p.powerType = v3.second.get<int>("PowerType");
if(p.powerType>0&&p.powerType<END)
p.dataOffset += r.dataNums[p.powerType-1];
else
p.dataOffset = 0 ;
r.parses.push_back(p);
if(p.powerType>0&&p.powerType<END)
r.dataNums[p.powerType-1] += p.dataNums ;
}
for(int i = 0; i< END ;i++)
r.dataOffset[i] += m.dataNums[i] ;
m.reqs.push_back(r);
for(int i = 0 ; i< END ;i++)
m.dataNums[i] += r.dataNums[i];
}
for(int i = 0;i<END ;i++)
{
m.dataOffset[i] += bus.dataNums[i] ;
}
bus.modules.push_back(m);
for(int i =0 ;i < END ;i++)
bus.dataNums[i] += m.dataNums[i] ;
}
for(int i= 0 ;i<END ;i++)
dataOffset[i] += bus.dataNums[i] ;
busLines.push_back(bus);
for(int i = 0; i< END ;i++)
dataNums[i] += bus.dataNums[i];
}
check();
return true ;
}
void Config::check()
{
if(bus_number>MAX_CACHE_COMMDATA_NUM)
{
ConfObjectException e(ConfObjectException::other,1) ;
BOOST_THROW_EXCEPTION(e) ;
}
if(dataNums[0]>MAX_YX_NUM||dataNums[1]>MAX_YC_NUM||dataNums[2]>MAX_DD_NUM)
{
ConfObjectException e(ConfObjectException::other,2) ;
BOOST_THROW_EXCEPTION(e) ;
}
for(size_t n = 0 ; n< busLines.size();n++)
busLines[n].check();
}
bool Config::save()
{
using boost::property_tree::ptree;
ptree pt;
pt.put("Config.BusTotal", bus_number);
BOOST_FOREACH(const Bus& bus,busLines)
{
ptree child;
child.put("Port",bus.port);
child.put("Baud",bus.baud);
child.put("DataBits",bus.databits);
child.put("StopBits",bus.stopbits);
child.put("parity",bus.parity);
pt.add_child("Config.Lines.Bus",child);
}
write_xml("configuration.xml", pt);
return true ;
}