-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
446 lines (371 loc) · 11.2 KB
/
test.js
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
'use strict';
const SerialPort = require('serialport')
const ByteLength = require('@serialport/parser-byte-length')
var Promise = require("bluebird");
//var testOptions = {
// baudRate: 115200,
// databits: 8,
// parity: 'none',
// stopbits: 1,
//};
var defaultOptions = {
baudRate: 115200,
databits: 8,
parity: 'none',
stopbits: 1,
//autoOpen: true
};
const port = new SerialPort('/dev/tty.usbserial-AL0517WZ', defaultOptions);
//port.open(function (err) {
// if (err) {
// return console.log('Error opening port: ', err.message)
// }
//
// // Because there's no callback to write, write errors will be emitted on the port:
// port.write('main screen turn on')
//});
// The open event is always emitted
port.on('open', function() {
console.log('port is now opened');
//port.write(Buffer.from('Hi Mom!'));
//port.write('<01><LEN><SEQ><CMD><DATA><05><BCC><03>');
//var buffer = Buffer.alloc(10);
//buffer[0] = 0x01; // Preamble
//buffer[1] = 0x20; // <LEN>
//buffer[2] = 0x20; // <SEQ> HASH
//buffer[3] = 0x50; // <CMD> 0x50 - BEEP
//buffer[4] = 0x00; // <DATA>
//buffer[5] = 0x05; // Postamble
//buffer[6] = 0x05; // <BCC>
//buffer[7] = 0x03; // Postamble
//var BCC = parseInt('25', 16) + parseInt('20', 16) + parseInt('50', 16) + parseInt('00', 16) + parseInt('05', 16);
////console.log(BCC.toString(10));
//var decimalSum = '48495352';
//0+48 = 48
//1+48 = 49
//5+48 = 53
//4+48 = 52
var testData = [];
testData.push(1);
testData.push(36);
testData.push(62);
testData.push(80);
testData.push(5);
testData.push(48);
testData.push(48);
testData.push(66);
testData.push(55);
testData.push(3);
var testBinary = Buffer.from(testData);
//1 36 62 80 5 48 48 66 55 3
var test2 = [];
test2.push(1);
test2.push(36);
test2.push(62);
test2.push(80);
test2.push(5);
test2.push(48);
test2.push(48);
test2.push(66);
test2.push(55);
test2.push(3);
//var d = Buffer.from([0x01, 0x24], 'hex');
//console.log(d);
var decimalData = [];
decimalData.push(1); //premble
decimalData.push(4+32); //len - 32 is offset
decimalData.push(32); //seq
decimalData.push(80); //cmd 80 is beep
//decimalData.push(0); //data
decimalData.push(5); //postamble
//decimalData.forEach(function(v, i) {
// console.log(i);
// console.log(v);
//});
var BCC = (4+32) + 32 + 80 + 0 + 5;
console.log(parseInt(BCC).toString(16));
//153 result
//decimalData.push(0+48); //BCC
//decimalData.push(1+48); //BCC
//decimalData.push(5+48); //BCC
//decimalData.push(3+48); //BCC
decimalData.push(Buffer.from('0x30')); //BCC
decimalData.push(Buffer.from('0x30')); //BCC
decimalData.push(Buffer.from('0x39')); //BCC
decimalData.push(Buffer.from('0x39')); //BCC
decimalData.push(3); //terminaltor
var data = Buffer.from(decimalData)
//console.log(data);
//console.log('write test data');
//port.write(data, function(err, result) {
// if (err) {
// console.log(err);
// }
// console.log(err);
// console.log(result);
//});
playMusic(port);
//port.write(pack, function(err, result) {
// if (err) {
// console.log(err);
// }
// console.log(err);
// console.log(result);
//});
//setTimeout(function() {
// var pack = generateCommandPack(44, Buffer.from([parseInt('34', 16)]));
// port.write(pack);
//}, 500);
//var pack = generateCommandPack(115, Buffer.from([parseInt('30', 16), parseInt('2c', 16), parseInt('31', 16)]));
//port.write(pack, function(err, result) {
// console.log(err);
// console.log(result);
//});
//port.write(Buffer.from('01', 'hex'));
////отваряне на клиентски бон
//var d = [
// parseInt('31', 16),
// parseInt('2c', 16),
// parseInt('31', 16),
// parseInt('2c', 16),
// parseInt('31', 16),
// parseInt('2c', 16),
// parseInt('31', 16),
//];
//var pack = generateCommandPack(48, Buffer.from(d));
//port.write(pack, function(err, res) {
// console.log(err);
// console.log(res);
//});
//var d = [
// parseInt('32', 16),
// parseInt('30', 16),
// parseInt('32', 16),
// parseInt('30', 16),
// parseInt('34', 16),
// parseInt('38', 16),
// parseInt('34', 16),
// parseInt('38', 16),
// parseInt('33', 16),
// parseInt('09', 16),
// parseInt('0', 16),
//];
//var pack = generateCommandPack(57, Buffer.from(d));
//port.write(pack, function(err, res) {
// console.log(err);
// console.log(res);
//});
//setTimeout(function() {
// var d = [
// parseInt('09', 16),
// parseInt('c0', 16), //c0 - А на кирилица
// parseInt('1', 16),
// parseInt('2d', 16),
// parseInt('31', 16),
// ];
// var pack = generateCommandPack(49, Buffer.from(d));
// port.write(pack, function(err, res) {
// console.log(err);
// console.log(res);
// });
//}, 500);
});
var SEQ = 32;
function generateCommandPack(cmd, data=null) {
SEQ++;
if (SEQ >= 127) {
SEQ = 32;
}
var pack = [];
const PREMBLE = 1;
const POSTAMBLE = 5;
const TERMINATOR = 3;
const CHECKSUM_OFFSET_HEX = 30;
const CHECKSUM_OFFSET_DECIMAL = 48;
const MIN_LENGTH = 36;
//var len = Buffer.byteLength(Buffer.from(data)) + MIN_LENGTH;
var dataLength = 0;
if (data) {
dataLength = data.length;
}
console.log(data);
//if (data == 0) {
// dataLength = 1;
//}
console.log('data length: ' + dataLength);
var len = dataLength + MIN_LENGTH;
console.log(len);
//console.log(Buffer.from(data.toString(), 'hex'));
var BCC = len + SEQ + cmd + POSTAMBLE;
if (data) {
data.forEach(function(v) {
BCC = BCC + v;
});
}
var hexChecksum = parseInt(BCC).toString(16);
var charactersLength = hexChecksum.length;
var bccArr = [];
if (charactersLength == 2) {
bccArr.push(CHECKSUM_OFFSET_DECIMAL);
bccArr.push(CHECKSUM_OFFSET_DECIMAL);
} else if (charactersLength == 3) {
bccArr.push(CHECKSUM_OFFSET_DECIMAL);
}
for (var i=0;i<hexChecksum.length;i++) {
var v = hexChecksum[i];
var n = parseInt(v, 16) + parseInt(CHECKSUM_OFFSET_HEX, 16);
bccArr.push(n);
}
console.log(bccArr);
console.log(hexChecksum);
pack.push(PREMBLE); //premble
pack.push(len); //len - 32 is offset
pack.push(SEQ); //seq
pack.push(cmd); //cmd 80 is beep
if (data) {
data.forEach(function(v) {
pack.push(v);
});
//pack.push(data); //data
}
pack.push(POSTAMBLE); //postamble
bccArr.forEach(function(v) {
pack.push(v);
});
pack.push(TERMINATOR);
return Buffer.from(pack);
}
function registriraneNaProdajba() {
var d = [
parseInt('09', 16), //tab
//parseInt('c0', 16), //c0 - А на кирилица
parseInt('1', 16), //departament
parseInt('09', 16), //tab
parseInt('31', 16),
];
var pack = generateCommandPack(49, Buffer.from(d));
}
//var commandSeq = null;
var commandPromise = null;
function runCommand(connection, cmd, data) {
commandPromise = defer();
var pack = generateCommandPack(cmd, data);
connection.write(pack, function(err, result) {
if (err) {
return commandPromise.reject(err);
}
});
return commandPromise.promise;
}
var messageStarted = false;
var messageBytes = [];
port.on('data', function (data) {
console.log('Received Data:', data)
data.forEach(function(v) {
if (v == 1) {
messageStarted = true;
}
if (messageStarted) {
messageBytes.push(v);
}
if (v == 3) {
messageStarted = false;
if (commandPromise) {
commandPromise.resolve(messageBytes);
}
}
});
});
function playMusic(port) {
var notesFrequencies = {
'c': 261,
'd': 294,
'e': 329,
'f': 349,
'g': 391,
'gS': 415,
'a': 440,
'aS': 455,
'b': 466,
'cH': 523,
'cSH': 554,
'dH': 587,
'dSH': 622,
'eH': 659,
'fH': 698,
'fSH': 740,
'gH': 784,
'gSH': 830,
'aH': 880,
};
var musicSteps = [
[notesFrequencies['a'], '500', '30'],
[notesFrequencies['a'], '500', '30'],
[notesFrequencies['a'], '500', '30'],
[notesFrequencies['f'], '350', '30'],
[notesFrequencies['cH'], '150', '30'],
[notesFrequencies['a'], '500', '30'],
[notesFrequencies['f'], '350', '30'],
[notesFrequencies['cH'], '150', '30'],
[notesFrequencies['a'], '150', '30'],
[0, 0, '150'], //end part one
[notesFrequencies['eH'], '500', '30'],
[notesFrequencies['eH'], '500', '30'],
[notesFrequencies['eH'], '500', '30'],
[notesFrequencies['fH'], '350', '30'],
[notesFrequencies['cH'], '150', '30'],
[notesFrequencies['gS'], '500', '30'],
[notesFrequencies['f'], '350', '30'],
[notesFrequencies['cH'], '150', '30'],
[notesFrequencies['a'], '650', '30'],
[0, 0, '150'], //end part two
[notesFrequencies['aH'], '500', '30'],
[notesFrequencies['a'], '300', '30'],
[notesFrequencies['a'], '150', '30'],
[notesFrequencies['aH'], '400', '30'],
[notesFrequencies['gSH'], '200', '30'],
[notesFrequencies['gH'], '200', '30'],
[notesFrequencies['fSH'], '125', '30'],
[notesFrequencies['fH'], '125', '30'],
[notesFrequencies['fSH'], '250', '30'],
[0, 0, '250'], //end part three
[notesFrequencies['aS'], '250', '30'],
[notesFrequencies['dSH'], '400', '30'],
[notesFrequencies['dH'], '200', '30'],
[notesFrequencies['cSH'], '200', '30'],
[notesFrequencies['cH'], '125', '30'],
[notesFrequencies['b'], '125', '30'],
[notesFrequencies['cH'], '250', '30'],
[0, 0, '250'], //end part four
];
return Promise.each(musicSteps, function(musicData) {
if (musicData[0] == 0 && musicData[1] == 0) {
return Promise.delay(musicData[2]);
} else {
return runCommand(port, 80, asciiToHex(musicData[0] + ',' + musicData[1]) ).delay(parseInt(musicData[1]) + parseInt(musicData[2]));
}
});
}
function defer() {
var resolve, reject;
var promise = new Promise(function() {
resolve = arguments[0];
reject = arguments[1];
});
return {
resolve: resolve,
reject: reject,
promise: promise
};
}
function asciiToHex(str) {
var arr1 = [];
for (var n = 0, l = str.length; n < l; n ++) {
var hex = Number(str.charCodeAt(n)).toString(16);
arr1.push(hex);
}
return Buffer.from(arr1.join(''), 'hex');
}
//port.on('readable', function () {
// console.log('Data:', port.read())
//})