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