forked from dmalvezz/ChromeSerialPortExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_port.js
192 lines (176 loc) · 5.08 KB
/
serial_port.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
/**
* @author Davide Malvezzi
* @version 0.1.0
* Google Chrome browser app to allow the use of serial ports comunication inside a web page.
* The app acts as an wrapper between the web pages and the serial ports.
* The app use the chrome.serial API to interact with the serial ports and
* the chrome.runtime messaging API to exchange information with the web page.
* It is also provided a simple JavaScript library to use inside the web pages to access the services offered by the app.
*/
/**
* Extension unique id to start the comunication.
*/
var extensionId = "agdnkpfcfmmchhcjhnknbggldfcfmnam";
function SerialPort(){
/**
* Port GUID assigned by the app.
*/
var portGUID;
/**
* Initialize the comunication with the app.
*/
var port = chrome.runtime.connect(extensionId);
/**
* Contain the unique serial port connection id.
*/
var serialConnectionId;
/**
* Bool that indicates if the serial connection is open.
*/
var isSerialPortOpen = false;
/**
* Callback function to call if there is new data incoming from the serial port connection.
*/
var onDataReceivedCallback = undefined;
/**
* Callback function to call if there is the connection encountered some problems.
*/
var onErrorReceivedCallback = undefined;
/**
* Listener to handle incoming message from the app trought the messaging port.
* Handled commands are:
* - guid -> received when the connection with the app is established, represent the GUID assigned to the port
* - serialdata -> received when new binary data is available on the serial port
*/
port.onMessage.addListener(
function(msg) {
console.log(msg);
if(msg.header === "guid"){
portGUID = msg.guid;
}
else if(msg.header === "serialdata"){
if(onDataReceivedCallback !== undefined){
onDataReceivedCallback(new Uint8Array(msg.data).buffer);
}
}
else if(msg.header === "serialerror"){
onErrorReceivedCallback(msg.error);
}
}
);
/**
* Check if the current port is opened.
*/
this.isOpen = function(){
return isSerialPortOpen;
}
/**
* Set the new data callback.
*/
this.setOnDataReceivedCallback = function(callBack){
onDataReceivedCallback = callBack;
}
/**
* Set the error callback.
*/
this.setOnErrorReceivedCallback = function(callBack){
onErrorReceivedCallback = callBack;
}
/**
* Try to open a serial connection.
* portInfo MUST contain:
* portName -> path to the port to open
* bitrate -> port bit rate as number
* dataBits -> data bit ("eight" or "seven")
* parityBit -> parity bit ("no", "odd" or "even")
* stopBits -> stop bit ("one" or "two")
* Callback is a function to call to handle the app result.
*/
this.openPort = function(portInfo, callBack){
chrome.runtime.sendMessage(extensionId,
{
cmd: "open",
portGUID: portGUID,
info: portInfo
},
function(response){
if(response.result === "ok"){
isSerialPortOpen = true;
serialConnectionId = response.connectionInfo.connectionId;
}
callBack(response);
}
);
}
/**
* Try to close the serial connection.
* Callback is a function to call to handle the app result.
*/
this.closePort = function(callBack){
chrome.runtime.sendMessage(extensionId,
{
cmd: "close",
connectionId: serialConnectionId
},
function(response){
if(response.result === "ok"){
isSerialPortOpen = false;
}
callBack(response);
}
);
}
/**
* Write data on the serial port.
* The request MUST contain:
* connectionId -> connection unique id provided when the port is opened
* data -> Array which contains the bytes to send
*/
this.write = function(data, callBack){
chrome.runtime.sendMessage(extensionId,
{
cmd: "write",
connectionId: serialConnectionId,
data: Array.prototype.slice.call(new Uint8Array(data))
},
function(response){
if(response.result === "ok"){
if(response.sendInfo.error !== undefined){
if(response.sendInfo.error === "disconnected" || response.sendInfo.error === "system_error"){
isSerialPortOpen = false;
closePort(function(){});
}
}
}
callBack(response);
}
);
}
}
/**
* Get the list of all serial devices connected to the pc.
* If there is no error it will return an array of object containing:
* - path
* - vendorId (optional)
* - productId (optional)
* - displayName (optional)
* Callback is a function to call to handle the app result.
*/
function getDevicesList(callBack){
chrome.runtime.sendMessage(extensionId, {cmd: "list"}, callBack);
}
/**
* Used to check if the Serial Interface app is installed on the browser.
* If it's installed return result: "ok" and the current version
*/
function isExtensionInstalled(callback){
chrome.runtime.sendMessage(extensionId, { cmd: "installed" },
function (response) {
if (response){
callback(true);
}else{
callback(false);
}
}
);
}