-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwebsocketcanopen.html
266 lines (261 loc) · 11.4 KB
/
websocketcanopen.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!doctype html>
<html lang="en">
<head>
<title>WebSocketCANopen</title>
<script src="../adapters/websocketcan.js"></script>
<script src="../adapters/websocketcanopen.js"></script>
<style>
body {
font-family: Arial, Verdana, sans-serif;
}
input {
border: 1px solid #aaaa;
border-radius: 0.25rem;
font-size: 1rem;
padding: 0.25rem;
}
label { text-align: right; }
label:after { content: ":"; }
button {
cursor: pointer;
font-size: 1rem;
border-radius: 0.25rem;
padding: 0.25rem;
}
#connect:disabled { cursor: wait; }
</style>
</head>
<body>
<label for="url">URL</label><input id="url" type="text" placeholder="ws://server:port"><button id="connect">Connect</button><br>
<h2>NMT</h2><hr>
<label for="nmt-node-id">Node ID</label><input id="nmt-node-id" type="number" min="0" max="127" step="1" value="0">
<label for="nmt-cmd">Command</label><select id="nmt-cmd">
<option value="1">Start</option>
<option value="2">Stop</option>
<option value="128">Enter Pre-Operational</option>
<option value="129">Reset Node</option>
<option value="130">Reset Communication</option>
</select>
<button id="nmt-send" class="send" disabled>Send</button>
<table id="nodes">
<caption>Heartbeats</caption>
<thead>
<tr>
<th>Node ID</th>
<th>NMT State</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody></tbody>
</table>
<h2>SYNC</h2><hr>
<button id="sync-send" class="send" disabled>Send</button>
<span id="sync-rx">No SYNC messages received.</span>
<h2>TIME</h2><hr>
<button id="time-send" class="send" disabled>Send</button>
<span id="time-rx">No TIME messages received.</span>
<h2>EMCY</h2><hr>
<table id="emcy">
<thead>
<tr>
<th>Node ID</th>
<th>EEC</th>
<th>ER</th>
<th>MSEF</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<h2>PDO</h2><hr>
<table id="pdo">
<thead>
<tr>
<th>Node ID</th>
<th>Type</th>
<th>#</th>
<th>Data</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<h2>SDO</h2><hr>
<label for="sdo-node-id">Node ID</label><input id="sdo-node-id" type="number" min="1" max="127" step="1"><br>
<label for="sdo-index">Index</label><input id="sdo-index"><br>
<label for="sdo-subindex">Sub-Index</label><input id="sdo-subindex"><br>
<label>Type</label><input type="radio" name="sdo-type" value="upload" checked>Upload <input type="radio" name="sdo-type" value="download">Download<br>
<label for="sdo-value">Value</label><input id="sdo-value" type="number" min="0" max="4294967295" step="1" disabled>
<button id="sdo-send" class="send" disabled>Send</button><br>
<label for="sdo-response">Response</label><span id="sdo-response"></span>
<script>
window.addEventListener("DOMContentLoaded", event => {
document.querySelectorAll('[name="sdo-type"]').forEach(element => {
element.addEventListener("change", event => {
if (element.value == "upload") {
document.querySelector("#sdo-value").value = "";
document.querySelector("#sdo-value").disabled = true;
} else {
document.querySelector("#sdo-value").disabled = false;
}
});
});
let ws;
document.querySelector("#connect").addEventListener("click", event => {
let connectButton = document.querySelector("#connect");
if (connectButton.innerHTML == "Connect") {
let url = document.querySelector("#url");
let wsUrl = url.value;
let wsRe = /wss?:\/\/[0-9a-z]+(:[0-9]+)?/i;
if (!wsRe.test(wsUrl)) {
alert("Invalid URL");
return;
}
url.disabled = true;
connectButton.innerHTML = "Connecting...";
ws = new WebSocketCanOpen(wsUrl);
ws.addEventListener("error", event => {
alert("Connection error");
});
ws.addEventListener("open", event => {
document.querySelectorAll(".send").forEach(element => { element.disabled = false; });
connectButton.innerHTML = "Disconnect";
connectButton.disabled = false;
});
ws.addEventListener("close", event => {
document.querySelectorAll(".send").forEach(element => { element.disabled = false; });
url.disabled = false;
connectButton.innerHTML = "Connect";
connectButton.disabled = false;
});
ws.addEventListener("message", event => {
let msg = event.data;
let pdo;
switch (msg.functionCode) {
case CanOpenMessage.FUNCTION_CODE_NMT:
break;
case CanOpenMessage.FUNCTION_CODE_SYNC:
if (msg.nodeId == 0) { // SYNC
let counter = "no counter";
if (msg.data.length) {
counter = "counter value of " + msg.data[0];
}
document.querySelector("#sync-rx").innerHTML = "Received " + event.timeStamp.toFixed(0) + " ms from page load with " + counter;
} else if (msg.data.length == 8) { // EMCY
let nodeRows = document.querySelectorAll("#emcy tbody tr");
let msef = new Uint8Array(8);
msef.set(msg.data.slice(3, 8));
msef = new BigUint64Array(msef.buffer)[0];
for (i = 0; i < nodeRows.length; i++) {
if (parseInt(nodeRows[i].querySelector("td").innerHTML) == msg.nodeId) {
nodeRows[i].querySelector("td:nth-child(2)").innerHTML = "0x" + new Uint16Array(msg.data.slice(0, 2).buffer)[0].toString(16).toUpperCase().padStart(4, "0");
nodeRows[i].querySelector("td:nth-child(3)").innerHTML = "0x" + msg.data[2].toString(16).toUpperCase().padStart(2, "0");
nodeRows[i].querySelector("td:nth-child(4)").innerHTML = "0x" + msef.toString(16).toUpperCase().padStart(10, "0");
nodeRows[i].querySelector("td:last-child").innerHTML = event.timeStamp.toFixed(0);
return;
}
}
document.querySelector("#emcy tbody").innerHTML += `
<tr>
<td>${msg.nodeId}</td>
<td>0x${new Uint16Array(msg.data.slice(0, 2).buffer)[0].toString(16).toUpperCase().padStart(4, "0")}</td>
<td>0x${msg.data[2].toString(16).toUpperCase().padStart(2, "0")}</td>
<td>0x${msef.toString(16).toUpperCase().padStart(10, "0")}</td>
<td>${event.timeStamp.toFixed(0)}</td></tr>`;
}
break;
case CanOpenMessage.FUNCTION_CODE_TIME:
document.querySelector("#time-rx").innerHTML = CanOpenTimeOfDay.from(msg.data).toDate() + " received " + event.timeStamp.toFixed(0) + " ms from page load";
break;
case CanOpenMessage.FUNCTION_CODE_RPDO1:
pdo = pdo || {type: "R", number: 1};
case CanOpenMessage.FUNCTION_CODE_TPDO1:
pdo = pdo || {type: "T", number: 1};
case CanOpenMessage.FUNCTION_CODE_RPDO2:
pdo = pdo || {type: "R", number: 2};
case CanOpenMessage.FUNCTION_CODE_TPDO2:
pdo = pdo || {type: "T", number: 2};
case CanOpenMessage.FUNCTION_CODE_RPDO3:
pdo = pdo || {type: "R", number: 3};
case CanOpenMessage.FUNCTION_CODE_TPDO3:
pdo = pdo || {type: "T", number: 3};
case CanOpenMessage.FUNCTION_CODE_RPDO4:
pdo = pdo || {type: "R", number: 4};
case CanOpenMessage.FUNCTION_CODE_TPDO4:
pdo = pdo || {type: "T", number: 4};
let pdoRows = document.querySelectorAll("#pdo tbody tr");
for (i = 0; i < pdoRows.length; i++) {
if (pdoRows[i].querySelector("td:first-child").innerHTML == msg.nodeId && pdoRows[i].querySelector("td:nth-child(2)").innerHTML == pdo.type + "PDO" && pdoRows[i].querySelector("td:nth-child(3)").innerHTML == pdo.number) {
pdoRows[i].querySelector("td:nth-child(4)").innerHTML = Array.from(msg.data).map(x => "0x" + x.toString(16).padStart(2, "0")).join(", ");
pdoRows[i].querySelector("td:nth-child(5)").innerHTML = event.timeStamp.toFixed(0);
return;
}
}
document.querySelector("#pdo tbody").innerHTML += `<tr><td>${msg.nodeId}</td><td>${pdo.type}PDO</td><td>${pdo.number}</td><td>${Array.from(msg.data).map(x => "0x" + x.toString(16).padStart(2, "0")).join(", ")}</td><td>${event.timeStamp.toFixed(0)}</td></tr>`;
break;
case CanOpenMessage.FUNCTION_CODE_SDO_TX:
break;
case CanOpenMessage.FUNCTION_CODE_SDO_RX:
break;
case CanOpenMessage.FUNCTION_CODE_NMT_ERROR_CONTROL:
let nodeRows = document.querySelectorAll("#nodes tbody tr");
for (i = 0; i < nodeRows.length; i++) {
if (parseInt(nodeRows[i].querySelector("td").innerHTML) == msg.nodeId) {
nodeRows[i].querySelector("td:nth-child(2)").innerHTML = "0x" + msg.data[0].toString(16).toUpperCase().padStart(2, "0");
nodeRows[i].querySelector("td:last-child").innerHTML = event.timeStamp.toFixed(0);
return;
}
}
document.querySelector("#nodes tbody").innerHTML += `<tr><td>${msg.nodeId}</td><td>0x${msg.data[0].toString(16).toUpperCase().padStart(2, "0")}</td><td>${event.timeStamp.toFixed(0)}</td></tr>`;
default:
}
});
} else {
document.querySelectorAll(".send").forEach(element => { element.disabled = false; });
connectButton.disabled = true;
connectButton.innerHTML = "Disconnecting...";
ws.close(1000);
}
});
document.querySelector("#nmt-send").addEventListener("click", event => {
let cs = parseInt(document.querySelector("#nmt-cmd").options[document.querySelector("#nmt-cmd").selectedIndex].value);
let nodeId = parseInt(document.querySelector("#nmt-node-id").value);
ws.send(new CanOpenNmtNodeControlMessage(cs, nodeId));
});
document.querySelector("#sync-send").addEventListener("click", event => {
ws.send(new CanOpenSyncMessage());
});
document.querySelector("#time-send").addEventListener("click", event => {
let cs = parseInt(document.querySelector("#nmt-cmd").options[document.querySelector("#nmt-cmd").selectedIndex].value);
let nodeId = parseInt(document.querySelector("#nmt-node-id").value);
ws.send(new CanOpenTimeMessage(CanOpenTimeOfDay.fromDate(new Date())));
});
document.querySelector("#sdo-send").addEventListener("click", event => {
document.querySelector("#sdo-response").innerHTML = "Please wait...";
sdoNodeId = parseInt(document.querySelector("#sdo-node-id").value);
sdoIndex = parseInt(document.querySelector("#sdo-index").value);
sdoSubIndex = parseInt(document.querySelector("#sdo-subindex").value);
let sdoClient = new CanOpenSdoClient(ws);
if (document.querySelector("#sdo-value").disabled) {
sdoClient.upload(sdoNodeId, sdoIndex, sdoSubIndex).then(response => {
document.querySelector("#sdo-value").value = new Uint32Array(response)[0];
document.querySelector("#sdo-response").innerHTML = "Upload successful.";
}).catch(error => {
document.querySelector("#sdo-value").value = error;
document.querySelector("#sdo-response").innerHTML = "Abort received.";
});
} else {
sdoClient.download(sdoNodeId, sdoIndex, sdoSubIndex, new Uint32Array([parseInt(document.querySelector("#sdo-value").value)])).then(response => {
document.querySelector("#sdo-response").innerHTML = "Download successful.";
}).catch(error => {
document.querySelector("#sdo-response").innerHTML = "Abort received: 0x" + error.toString(16).padStart(8, "0");
});
}
});
});
</script>
</body>
</head>
</html>