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