1 | #include "network.h"
|
---|
2 |
|
---|
3 | #include <iostream.h> // cout
|
---|
4 | #include <iomanip.h> // setw, setfill
|
---|
5 |
|
---|
6 | ClassImp(Network);
|
---|
7 |
|
---|
8 | void Network::Start()
|
---|
9 | {
|
---|
10 | lout << "- Starting network." << endl;
|
---|
11 |
|
---|
12 | VmodIcan::Start();
|
---|
13 | InitNodes();
|
---|
14 |
|
---|
15 | lout << "- Network started." << endl;
|
---|
16 | }
|
---|
17 |
|
---|
18 | void Network::Stop()
|
---|
19 | {
|
---|
20 | lout << "- Stopping network." << endl;
|
---|
21 |
|
---|
22 | StopNodes();
|
---|
23 | VmodIcan::Stop();
|
---|
24 |
|
---|
25 | lout << "- Network stopped." << endl;
|
---|
26 | }
|
---|
27 |
|
---|
28 | Network::Network(const char *dev, const int baud, MLog &out) : CanOpen(dev, baud, out)
|
---|
29 | {
|
---|
30 | for (int i=0; i<32; i++)
|
---|
31 | fNodes[i] = NULL;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void Network::HandleSDO(BYTE_t node, BYTE_t cmd, WORD_t idx, BYTE_t subidx, LWORD_t data, struct timeval *tv)
|
---|
35 | {
|
---|
36 | switch (cmd)
|
---|
37 | {
|
---|
38 | case kSDO_TX4: // answer to 0x40 with 4 bytes of data
|
---|
39 | if (fNodes[node])
|
---|
40 | {
|
---|
41 | fNodes[node]->HandleSDO(idx, subidx, data, tv);
|
---|
42 | return;
|
---|
43 | }
|
---|
44 | break;
|
---|
45 |
|
---|
46 | case kSDO_TX3: // answer to 0x40 with 2 bytes of data
|
---|
47 | if (fNodes[node])
|
---|
48 | {
|
---|
49 | fNodes[node]->HandleSDO(idx, subidx, data>>16, tv);
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | break;
|
---|
53 |
|
---|
54 | case kSDO_TX1: // answer to 0x40 with 1 byte of data
|
---|
55 | if (fNodes[node])
|
---|
56 | {
|
---|
57 | fNodes[node]->HandleSDO(idx, subidx, data>>24, tv);
|
---|
58 | return;
|
---|
59 | }
|
---|
60 | break;
|
---|
61 |
|
---|
62 | case kSDO_TX_OK: // answer to a SDO_TX message
|
---|
63 | if (fNodes[node])
|
---|
64 | {
|
---|
65 | fNodes[node]->HandleSDOOK(idx, subidx);
|
---|
66 | return;
|
---|
67 | }
|
---|
68 | break;
|
---|
69 |
|
---|
70 | case kSDO_TX_ERROR: // error message (instead of 0x60)
|
---|
71 | if (fNodes[node])
|
---|
72 | {
|
---|
73 | fNodes[node]->HandleSDOError(data);
|
---|
74 | return;
|
---|
75 | }
|
---|
76 | break;
|
---|
77 | }
|
---|
78 | cout << dec << setfill('0');
|
---|
79 | cout << "Node=" << (int)node << " Cmd=0x" << hex << (int)cmd << ": ";
|
---|
80 | cout << "Sdo=" << idx << "/" << (int)subidx << ": 0x" << setw(8) << data;
|
---|
81 | cout << endl;
|
---|
82 | }
|
---|
83 |
|
---|
84 | void Network::HandlePDO1(BYTE_t node, BYTE_t *data, struct timeval *tv)
|
---|
85 | {
|
---|
86 | if (!fNodes[node])
|
---|
87 | {
|
---|
88 | cout << "Node " << dec << (int)node << ", PDO1: " << hex;
|
---|
89 | for (int i=0; i<8; i++)
|
---|
90 | cout << " 0x" << (int)data[i];
|
---|
91 | cout << endl;
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | fNodes[node]->HandlePDO1(data, tv);
|
---|
96 | }
|
---|
97 |
|
---|
98 | void Network::HandlePDO2(BYTE_t node, BYTE_t *data, struct timeval *tv)
|
---|
99 | {
|
---|
100 | if (!fNodes[node])
|
---|
101 | {
|
---|
102 | cout << "Node " << dec << (int)node << ", PDO2: " << hex;
|
---|
103 | for (int i=0; i<8; i++)
|
---|
104 | cout << " 0x" << (int)data[i];
|
---|
105 | cout << endl;
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | fNodes[node]->HandlePDO2(data, tv);
|
---|
110 | }
|
---|
111 |
|
---|
112 | void Network::HandlePDO3(BYTE_t node, BYTE_t *data, struct timeval *tv)
|
---|
113 | {
|
---|
114 | if (!fNodes[node])
|
---|
115 | {
|
---|
116 | cout << "Node " << dec << (int)node << ", PDO3: " << hex;
|
---|
117 | for (int i=0; i<8; i++)
|
---|
118 | cout << " 0x" << (int)data[i];
|
---|
119 | cout << endl;
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | fNodes[node]->HandlePDO3(data, tv);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void Network::HandlePDO4(BYTE_t node, BYTE_t *data, struct timeval *tv)
|
---|
127 | {
|
---|
128 | if (!fNodes[node])
|
---|
129 | {
|
---|
130 | cout << "Node " << dec << (int)node << ", PDO4: " << hex;
|
---|
131 | for (int i=0; i<8; i++)
|
---|
132 | cout << " 0x" << (int)data[i];
|
---|
133 | cout << endl;
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | fNodes[node]->HandlePDO4(data, tv);
|
---|
138 | }
|
---|
139 |
|
---|
140 | void Network::SetNode(NodeDrv *drv)
|
---|
141 | {
|
---|
142 | const BYTE_t nodeid = drv->GetId();
|
---|
143 |
|
---|
144 | if (nodeid>31)
|
---|
145 | {
|
---|
146 | cout << "SetNode - Error: Only node Numbers < 32 are allowed"<< endl;
|
---|
147 | return;
|
---|
148 | }
|
---|
149 |
|
---|
150 | fNodes[nodeid] = drv;
|
---|
151 | }
|
---|
152 |
|
---|
153 | void Network::InitNodes()
|
---|
154 | {
|
---|
155 | for (int i=0; i<32; i++)
|
---|
156 | if (fNodes[i])
|
---|
157 | {
|
---|
158 | lout << "- Initializing Node #" << dec << i << endl;
|
---|
159 | fNodes[i]->InitDevice(this);
|
---|
160 | fNodeInitialized[i] = TRUE;
|
---|
161 | }
|
---|
162 | lout << "- All Nodes initialized." << endl;
|
---|
163 | }
|
---|
164 |
|
---|
165 | void Network::StopNodes()
|
---|
166 | {
|
---|
167 | for (int i=0; i<32; i++)
|
---|
168 | if (fNodes[i] && fNodeInitialized[i])
|
---|
169 | {
|
---|
170 | lout << "- Stopping Node #" << dec << i << endl;
|
---|
171 | fNodes[i]->StopDevice();
|
---|
172 | }
|
---|
173 | lout << "- All Nodes stopped." << endl;
|
---|
174 | }
|
---|
175 |
|
---|
176 | bool Network::HasError() const
|
---|
177 | {
|
---|
178 | for (int i=0; i<32; i++)
|
---|
179 | if (fNodes[i] && fNodes[i]->HasError())
|
---|
180 | return true;
|
---|
181 |
|
---|
182 | return false;
|
---|
183 | }
|
---|