-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_protocolA.js
61 lines (48 loc) · 1.35 KB
/
test_protocolA.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
var logger = require('winston');
var SerialPort = require('serialport');
// Internal Dependencies
var config = require('./config');
var token = require('./constants');
var codec = require('./codec');
var app = require('./app');
var db = require('./db');
// Init logging
logger.level = config.logLevel;
// Global variables for Client and Server mode
var isTransferState = false;
var isClientMode = false;
var port = null; // COM Port Communication
initPort();
function initPort(){
var portNumber = 'COM1';
port = new SerialPort(portNumber);
port.on('open', handlePortOpen);
port.on('close', handlePortClose);
port.on('data', handlePortData);
port.on('error', function(err) {
logger.error(err.message);
throw new Error('Port Error: ' + err.message);
})
}
function handlePortOpen() {
logger.info('Port open. Data rate: ' + port.options.baudRate);
}
function handlePortClose() {
logger.info('Port Closed.');
}
function handlePortWrite(data){
logger.info('REQUEST A');
logger.info(data);
port.write(data);
}
function handlePortData(data){
logger.info('RESPONSE B');
logger.info(data); // Raw Buffer Data
}
function write(){
handlePortWrite('Hola');
}
function runIntervalCheck() {
setInterval(write, 10000);
};
runIntervalCheck();