forked from onmachine/chrome-serial-monitor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
old_main.js
134 lines (116 loc) · 3.33 KB
/
old_main.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
//const device = '/dev/tty.usbserial-A100DUTY';
const device = '/dev/ttyACM0';
const serial = chrome.serial;
const timeout = 100;
function SerialConnection() {
this.connectionId = -1;
this.callbacks = {};
}
SerialConnection.prototype.connect = function(device, callback) {
serial.open(device, this.onOpen.bind(this))
this.callbacks.connect = callback;
};
SerialConnection.prototype.read = function(callback) {
// Only works for open serial ports.
if (this.connectionId < 0) {
throw 'Invalid connection';
}
serial.read(this.connectionId, 1, this.onRead.bind(this));
this.callbacks.read = callback;
};
SerialConnection.prototype.readLine = function(callback) {
// Only works for open serial ports.
if (this.connectionId < 0) {
throw 'Invalid connection';
}
var line = '';
// Keep reading bytes until we've found a newline.
var readLineHelper = function(readInfo) {
var char = readInfo.message;
if (char == '') {
// Nothing in the buffer. Try reading again after a small timeout.
setTimeout(function() {
this.read(readLineHelper);
}.bind(this), timeout);
return;
}
if (char == '\n') {
// End of line.
callback(line);
line = '';
}
line += char;
this.read(readLineHelper)
}.bind(this)
this.read(readLineHelper);
};
SerialConnection.prototype.write = function(msg, callback) {
// Only works for open serial ports.
if (this.connectionId < 0) {
throw 'Invalid connection';
}
this.callbacks.write = callback;
this._stringToArrayBuffer(msg, function(array) {
serial.write(this.connectionId, array, this.onWrite.bind(this));
}.bind(this));
};
SerialConnection.prototype.onOpen = function(connectionInfo) {
this.connectionId = connectionInfo.connectionId;
if (this.callbacks.connect) {
this.callbacks.connect();
}
};
SerialConnection.prototype.onRead = function(readInfo) {
if (this.callbacks.read) {
this.callbacks.read(readInfo);
}
};
SerialConnection.prototype.onWrite = function(writeInfo) {
log('wrote:' + writeInfo.bytesWritten);
if (this.callbacks.write) {
this.callbacks.write(writeInfo);
}
};
/** From tcp-client */
SerialConnection.prototype._arrayBufferToString = function(buf, callback) {
var blob = new Blob([buf]);
var f = new FileReader();
f.onload = function(e) {
callback(e.target.result)
}
f.readAsText(blob);
}
SerialConnection.prototype._stringToArrayBuffer = function(str, callback) {
var blob = new Blob([str]);
var f = new FileReader();
f.onload = function(e) {
callback(e.target.result);
}
f.readAsArrayBuffer(blob);
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
var ser = new SerialConnection();
ser.connect(device, function() {
log('connected to: ' + device);
ser.write('hello arduino', function() {
});
readNextLine();
});
function readNextLine() {
ser.readLine(function(line) {
log('readline: ' + line);
readNextLine();
});
}
function log(msg) {
var buffer = document.querySelector('#buffer');
buffer.innerHTML += msg + '<br/>';
}
var is_on = false;
document.querySelector('button').addEventListener('click', function() {
is_on = !is_on;
ser.write(is_on ? 'y' : 'n');
});