-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadPort.js
147 lines (107 loc) · 3.54 KB
/
readPort.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
//server
function portReader(){
var tStep = 0;
var j = 0
var fs = require('fs');
var SerialPort = require('serialport');
var sensorInit = {};
// read local json file
var temps = fs.readFileSync('./gui/data.json');
var tempData = JSON.parse(temps);
console.log(tempData);
//read epxeriment setup file
var rawExp = fs.readFileSync('./gui/expSetup.json');
var expSetup = JSON.parse(rawExp);
console.log(expSetup);
// create experiment data folder
var dirName = expSetup["expName"];
console.log(dirName);
var dir = './gui/expData/'+ dirName;
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
};
// reset data.csv file when start
var csvkeys = "time,id,temp\r\n"
fs.writeFileSync('./gui/expData/' + dirName +'/data.csv', csvkeys);
// time data
var now = Date.now();
fs.writeFileSync('./time.txt', now);
var rawTime = fs.readFileSync('./time.txt');
var timeData = rawTime.toString()
// console.log(timeData);
var portName;
SerialPort.list(function (err, ports) {
if (err) {
throw err;
}
ports.forEach(function(portNames) {
// console.log(port.comName);
portName = portNames.comName.toString();
var port = new SerialPort(portName, {
baudRate: 9600,
});
//open port
port.on("open", readPort );
function readPort(){
// check open port errors
port.write('main screen turn on', function(err) {
if (err) {
return console.log('Error on write: ', err.message);
}
console.log('reading port...');
});
// read port
port.on('data', function (bufferData) {
// start - stop port reading form gui
var statust = fs.readFileSync('./gui/status.json');
var status = JSON.parse(statust);
//console.log(status);
//read config file - sensor ids and names
var rawConf = fs.readFileSync('./gui/config.json');
var conf = JSON.parse(rawConf);
// console.log(conf);
if (status["status"] == "STOP"){
port.close();
};
// conver buffer data to string
var data = bufferData.toString().replace('\r\n', ' ');
console.log(data);
// split id and temp
var splitData = data.split(':')
var id = splitData[0];
var temp = splitData[1];
// config
var tempId = conf[id];
//console.log(tempId);
// Sensor Init
sensorInit[id] = temp;
let writeSensorInit = JSON.stringify(sensorInit, null, 2);
fs.writeFileSync('./gui/sensorInit.json', writeSensorInit);
// write data to json file
tempData[tempId] = temp;
var writeData = JSON.stringify(tempData, null, 2);
fs.writeFileSync('./gui/data.json', writeData);
// write data to csv
var time = Date.now();
var rawTime = fs.readFileSync('./time.txt');
var timeData = rawTime.toString()
//console.log(timeData);
var diff = time - timeData;
console.log(diff);
console.log(j);
if (diff > 2000 && j > 1 ){
tStep = tStep + 5;
}
fs.writeFileSync('./time.txt', time);
var csvData = time + "," + tempId +","+ temp + "\r\n";
//var csvData ="time:"+ time + ",id:" + id +","+"temp:"+ temp + "\r\n";
//fs.mkdirSync('./gui/de1');
fs.appendFileSync('./gui/expData/' + dirName +'/data.csv', csvData);
j++
});
};
});
});
};
module.exports = portReader;
portReader()