-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathserial_node_serial.js
116 lines (109 loc) · 3.81 KB
/
serial_node_serial.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
/*
Gordon Williams (gw@pur3.co.uk)
*/
(function() {
// Are we on nw.js with chrome.serial? No need for serialport then!
if (typeof chrome !== 'undefined' && chrome.serial) {
console.log("We have chrome.serial - not using 'serialport' module");
return;
}
if (typeof require === 'undefined') return;
var serialport;
try {
require.resolve('serialport');
} catch (e) {
console.log("No 'serialport' module found");
return;
}
try {
serialport = require('serialport').SerialPort;
} catch (e) {
console.log("Error initialising 'serialport':" + e.toString());
return;
}
var connection = undefined;
var getPorts=function(callback) {
serialport.list().then(function(ports) {
if (ports===undefined) return callback([], true/*instantPorts*/);
// On Linux we get spammed with ttySx ports - remove these
if (process.platform == 'linux')
ports = ports.filter(function(port) { return !port.path.match(/^\/dev\/ttyS[0-9]*$/); });
/**
* Filter the available serial ports to find the one(s)
* most likely to be an espruino (genuine, or at least STM32Fxxx based
*
* According to http://www.linux-usb.org/usb.ids
* vendorId === '0x0483' -> STMicroelectronics
* productId === '0x5740' -> STM32F407
*
* What is below works great for both Espruino boards, but fails for:
* * USB-TTL adaptors
* * Bluetooth Serial
* * BBC micro:bit, Nordic devkits, etc
*
* So can't be left in. TODO: return an object for each USB serial device
* and have a `preferred : bool` field in it. Then if many devices are found
* but only one is preferred, use that.
*/
// .filter(function(e) {
// return (e.vendorId === '0x0483' && e.productId === '0x5740');
// })
callback(ports.map(function(port) {
// port.pnpId could be handy
var vid = parseInt(port.vendorId);
var pid = parseInt(port.productId);
var d = { path : port.path };
if (vid||pid) d.usb = [vid,pid];
if (port.manufacturer) d.description = port.manufacturer;
if (!port.vendorId || !port.productId) d.unimportant = true;
return d;
}), true/*instantPorts*/
);
}).catch(function(err) {
console.log("serialport error: "+err.toString());
return callback([], true/*instantPorts*/);
});
};
var openSerial=function(serialPort, openCallback, receiveCallback, disconnectCallback) {
// https://github.com/voodootikigod/node-serialport#reference-guide
connection = new serialport({
path : serialPort,
baudRate: parseInt(Espruino.Config.BAUD_RATE)
});
connection.on('open', function() {
openCallback("Ok");
});
connection.on('data', function(data) {
if (receiveCallback !== undefined) {
var a = new Uint8Array(data.length);
for (var i=0;i<data.length;i++)
a[i] = data[i];
receiveCallback(a.buffer);
}
});
connection.on('close', function() {
disconnectCallback();
connection = undefined;
});
};
var closeSerial=function(callback) {
connection.close(callback);
};
var writeSerial = function(data, callback) {
// convert to an array - if we put a string into
// a Buffer (expected by nodeserial) then I think
// it gets interpreted as UTF8
var a = Buffer.alloc(data.length);
for (var i=0;i<data.length;i++)
a[i] = data.charCodeAt(i);
connection.write(a, callback);
};
// ----------------------------------------------------------
Espruino.Core.Serial.devices.push({
"name" : "Node Serial",
"getPorts": getPorts,
"open": openSerial,
"write": writeSerial,
"close": closeSerial,
});
})();