forked from LWDevGit/serialAssist-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
251 lines (212 loc) · 7.86 KB
/
index.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
/**
* Created by dell on 2019-07-31.
*/
/*
* jQuery语法 $(selector).action()
*/
var utils = require("./public/js/utils");
window.$ = window.jQuery = require('./public/js/jquery.min.js');
//参数
var revCount = 0;
var revCountTemp = 0;
var rateArray = [9600, 14400, 19200, 38400, 56000, 115200, 250000, 256000];
//var parityArray = ['none','even','mark','odd','space'];
//var dataBitsArray = [5, 6, 7, 8];
var serialData = '';
var serialHexData = new Array();
var serialHexDataToDis = '';
var sendData = '';
var sendHexData = new Array();
var timerRev;
function wrapEvent() {
if(port.isOpen == true) {
if(revCount > 0) {
//console.log('revCountTemp',revCountTemp)
//console.log('revCount',revCount)
if(revCountTemp == revCount) {
revCountTemp = revCount = 0;
//ascii显示
if($("#optionsRevStrDisplay").is(":checked") == true){
//console.log('optionsRevStrDisplay',$("#optionsRevStrDisplay").is(":checked"))
$('.receive-windows').append(serialData.toString());
serialData = '';
}
//hex显示
if($("#optionsRevHexDisplay").is(":checked") == true){
console.log('serialHexData',serialHexData)
for(i=0; i<serialHexData.length; i++){
//console.log(serialHexData[i]<16?"0x0"+serialHexData[i].toString('hex').toUpperCase():"0x"+serialHexData[i].toString('hex').toUpperCase())
//console.log(serialHexData[i]<16? serialHexData[i].toString('hex').toUpperCase(): serialHexData[i].toString('hex').toUpperCase())
serialHexDataToDis += (serialHexData[i]<16? serialHexData[i].toString('hex').toUpperCase(): serialHexData[i].toString('hex').toUpperCase()) + ' '
}
$('.receive-windows').append(serialHexDataToDis);
serialHexDataToDis = '';
serialHexData = [];
}
//自动换行
if($("#auto-wrap").is(":checked") == true){ $('.receive-windows').append('<br/>'); }
//console.log('auto-wrap',$("#auto-wrap").is(":checked"))
}
else{
revCountTemp = revCount;
}
}
}
}
//初始化波特率
for(let i=0; i<rateArray.length; i++){
$('.rate').append('<option>'+rateArray[i]+'</option>')
};
/*for(let i=0; i<parityArray.length; i++){
$('.parityValue').append('<option>'+parityArray[i]+'</option>')
};
for(let i=0; i<dataBitsArray.length; i++){
$('.dataBitsValue').append('<option>'+dataBitsArray[i]+'</option>')
};*/
let serialport = require('serialport');
let port = null;
serialport.list((err, ports) => {
for (let item of ports) {
$('.com').append(`<option>${item.comName + " #" + item.manufacturer}</option>`)
}
console.log(ports);
});
//input textarea控件监听
$('textarea#inputTextarea').keyup(function () {
//hex发送时只能输入限定字符
if($("#optionsSendHexDisplay").is(":checked") == true){
//console.log('optionsSendHexDisplay',$("#optionsSendHexDisplay").is(":checked"))
var text = $(this).val().replace(/[^\a-f,A-F,0-9], /g, "");
$(this).val(text)
}
})
//BaudRate控件监听
$('select#baudRate').change(function () {
console.log($(this).val());
//port.baudRate = $(this).val();
//如果打开串口则动态更新波特率
if(port.isOpen == true) {
/*port.update('baudRate',() => {
baudRate: parseInt($(this).val());
});*/
}
})
//点击刷新com
$('select#disabledSelect').click(function () {
console.log('select click')
$("select#disabledSelect").empty()
serialport.list((err, ports) => {
for (let item of ports) {
$('.com').append(`<option>${item.comName + " #" + item.manufacturer}</option>`)
}
console.log(ports);
});
})
// 打开串口
$('.btn-submit').click((data) => {
let COM = ($('select option:selected').text()).split(' #')[0];
let baudRate = $('#baudRate').val();
//let parity = $('#parity').val();
//let dataBits = $('#dataBits').val();
console.log(COM);
console.log(baudRate);
//console.log(parity);
//console.log(dataBits);
port = new serialport(COM, {
baudRate: parseInt(baudRate),
dataBits: 8,
parity: 'none',
stopBits: 1
});
//open事件监听
port.on('open', () =>{
console.log('serialport open success')
$('.receive-windows').text(`串口已打开: ${COM}, 波特率: ${baudRate}`);
$('.receive-windows').append('<br/>=======================================<br/>');
//定时器,用于接收数据的判断->自动换行
timerRev = setInterval(wrapEvent,250);
})
//close事件监听
port.on('close', () =>{
console.log('serialport close success')
timerRev = window.clearInterval(timerRev);
$('.receive-windows').text(`串口已关闭: ${COM}`);
$('.receive-windows').append('<br/>=======================================<br/>');
})
//data事件监听
port.on('data', data => {
//console.log(`DATA: ${data}`);
if($("#optionsRevStrDisplay").is(":checked") == true){
serialData += data;
}
if($("#optionsRevHexDisplay").is(":checked") == true){
serialHexData[revCount] = data;
}
revCount += 1;
})
//error事件监听
port.on('error',function(err){
console.log('Error: ',err.message);
})
});
// 关闭串口
$('.btn-cancle').click(() => {
let COM = $('select option:selected').text();
//关闭串口
port.close();
});
//sendRadio控件监听
$('input#optionsSendStrDisplay').change(function () {
//console.log('optionsSendStrDisplay',$(this).val());
sendData = $('.input-send-data').val();
let sendDataDis = utils.hex2strToDis(sendData);
$('.input-send-data').val(sendDataDis);
})
//sendRadio控件监听
$('input#optionsSendHexDisplay').change(function () {
//console.log('optionsSendHexDisplay',$(this).val());
sendData = $('.input-send-data').val();
let sendDataDis = utils.str2hexToDis(sendData).toUpperCase();
$('.input-send-data').val(sendDataDis);
})
// 点击发送数据
$('.btn-send').click(() => {
//ascii发送
if($("#optionsSendStrDisplay").is(":checked") == true){
//console.log('optionsSendStrDisplay',$("#optionsSendStrDisplay").is(":checked"))
sendData = $('.input-send-data').val();
//发送回车
if($("#enter-checked").is(":checked") == true){ sendData += '\r\n'; }
//console.log('enter-checked',$("#enter-checked").is(":checked"))
if (port != {} && port != null) {
console.log(`SendData: ${sendData}`);
port.write(sendData, (err) =>{
if (err) return console.log('write Error: ', err.message);
});
}
}
//hex发送
if($("#optionsSendHexDisplay").is(":checked") == true){
//console.log('optionsSendHexDisplay',$("#optionsSendHexDisplay").is(":checked"))
sendData = $('.input-send-data').val();
//sendData = Buffer.from(sendData,'ascii').toString('hex')
sendHexData = utils.hex2array(sendData)
//发送回车
if($("#enter-checked").is(":checked") == true){
sendHexData.push(0x0D)
sendHexData.push(0x0A)
}
//console.log('enter-checked',$("#enter-checked").is(":checked"))
if (port != {} && port != null) {
console.log(`sendHexData: ${sendHexData}`);
port.write(sendHexData, (err) =>{
if (err) return console.log('write Error: ', err.message);
});
}
}
})
// 清空接收的数据
$('.btn-reset').click(() => {
//$('.input-send-data').val('');
$('.receive-windows').empty();
})