@@ -12,6 +12,7 @@ class SerialConnectionHandler {
12
12
this . timeout = timeout ;
13
13
this . currentPort = null ;
14
14
this . currentReader = null ;
15
+ this . registerEvents ( ) ;
15
16
}
16
17
17
18
async requestSerialPort ( ) {
@@ -27,7 +28,7 @@ class SerialConnectionHandler {
27
28
}
28
29
}
29
30
30
- isConnected ( ) {
31
+ isConnected ( ) {
31
32
return this . currentPort ?. readable != null ;
32
33
}
33
34
@@ -44,6 +45,7 @@ class SerialConnectionHandler {
44
45
flowControl : this . flowControl
45
46
} ) ;
46
47
console . log ( '✅ Connected to serial port.' ) ;
48
+ if ( this . onConnect ) this . onConnect ( ) ;
47
49
return true ;
48
50
} catch ( error ) {
49
51
return false ;
@@ -58,6 +60,7 @@ class SerialConnectionHandler {
58
60
await this . currentReader ?. cancel ( ) ;
59
61
await port . close ( ) ;
60
62
console . log ( '🔌 Disconnected from serial port.' ) ;
63
+ if ( this . onDisconnect ) this . onDisconnect ( ) ;
61
64
} catch ( error ) {
62
65
console . error ( '💣 Error occurred while disconnecting: ' + error . message ) ;
63
66
} ;
@@ -106,7 +109,7 @@ class SerialConnectionHandler {
106
109
if ( timeout ) {
107
110
timeoutID = setTimeout ( ( ) => {
108
111
console . log ( '⌛️ Timeout occurred while reading.' ) ;
109
- if ( this . currentPort . readable ) reader ?. cancel ( ) ;
112
+ if ( this . currentPort ? .readable ) reader ?. cancel ( ) ;
110
113
} , timeout ) ;
111
114
}
112
115
@@ -129,8 +132,7 @@ class SerialConnectionHandler {
129
132
}
130
133
131
134
} catch ( error ) {
132
- console . log ( '💣 Error occurred while reading: ' ) ;
133
- console . log ( error ) ;
135
+ console . log ( '💣 Error occurred while reading: ' + error . message ) ;
134
136
} finally {
135
137
keepReading = false ;
136
138
// console.log('🔓 Releasing reader lock...');
@@ -141,17 +143,17 @@ class SerialConnectionHandler {
141
143
return bytesRead ;
142
144
}
143
145
144
- async requestFrame ( ) {
145
- if ( ! this . currentPort ?. writable ) {
146
- console . log ( '🚫 Port is not writable. Ignoring request...' ) ;
147
- return ;
146
+ async requestFrame ( ) {
147
+ if ( ! this . currentPort ?. writable ) {
148
+ console . log ( '🚫 Port is not writable. Ignoring request...' ) ;
149
+ return ;
148
150
}
149
151
// console.log('Writing 1 to the serial port...');
150
152
// Write a 1 to the serial port
151
153
const writer = this . currentPort . writable . getWriter ( ) ;
152
154
await writer . write ( new Uint8Array ( [ 1 ] ) ) ;
153
155
await writer . close ( ) ;
154
- }
156
+ }
155
157
156
158
async getFrame ( totalBytes ) {
157
159
if ( ! this . currentPort ) return ;
@@ -161,4 +163,18 @@ class SerialConnectionHandler {
161
163
// Read the given amount of bytes
162
164
return await this . readBytes ( totalBytes , this . timeout ) ;
163
165
}
166
+
167
+ registerEvents ( ) {
168
+ navigator . serial . addEventListener ( "connect" , ( e ) => {
169
+ // Connect to `e.target` or add it to a list of available ports.
170
+ console . log ( '🔌 Serial port became available. VID: 0x' + e . target . getInfo ( ) . usbVendorId . toString ( 16 ) ) ;
171
+ this . autoConnect ( ) ;
172
+ } ) ;
173
+
174
+ navigator . serial . addEventListener ( "disconnect" , ( e ) => {
175
+ console . log ( '❌ Serial port lost. VID: 0x' + e . target . getInfo ( ) . usbVendorId . toString ( 16 ) ) ;
176
+ this . currentPort = null ;
177
+ if ( this . onDisconnect ) this . onDisconnect ( ) ;
178
+ } ) ;
179
+ }
164
180
}
0 commit comments