@@ -12,6 +12,7 @@ class SerialConnectionHandler {
1212 this . timeout = timeout ;
1313 this . currentPort = null ;
1414 this . currentReader = null ;
15+ this . registerEvents ( ) ;
1516 }
1617
1718 async requestSerialPort ( ) {
@@ -27,7 +28,7 @@ class SerialConnectionHandler {
2728 }
2829 }
2930
30- isConnected ( ) {
31+ isConnected ( ) {
3132 return this . currentPort ?. readable != null ;
3233 }
3334
@@ -44,6 +45,7 @@ class SerialConnectionHandler {
4445 flowControl : this . flowControl
4546 } ) ;
4647 console . log ( '✅ Connected to serial port.' ) ;
48+ if ( this . onConnect ) this . onConnect ( ) ;
4749 return true ;
4850 } catch ( error ) {
4951 return false ;
@@ -58,6 +60,7 @@ class SerialConnectionHandler {
5860 await this . currentReader ?. cancel ( ) ;
5961 await port . close ( ) ;
6062 console . log ( '🔌 Disconnected from serial port.' ) ;
63+ if ( this . onDisconnect ) this . onDisconnect ( ) ;
6164 } catch ( error ) {
6265 console . error ( '💣 Error occurred while disconnecting: ' + error . message ) ;
6366 } ;
@@ -106,7 +109,7 @@ class SerialConnectionHandler {
106109 if ( timeout ) {
107110 timeoutID = setTimeout ( ( ) => {
108111 console . log ( '⌛️ Timeout occurred while reading.' ) ;
109- if ( this . currentPort . readable ) reader ?. cancel ( ) ;
112+ if ( this . currentPort ? .readable ) reader ?. cancel ( ) ;
110113 } , timeout ) ;
111114 }
112115
@@ -129,8 +132,7 @@ class SerialConnectionHandler {
129132 }
130133
131134 } catch ( error ) {
132- console . log ( '💣 Error occurred while reading: ' ) ;
133- console . log ( error ) ;
135+ console . log ( '💣 Error occurred while reading: ' + error . message ) ;
134136 } finally {
135137 keepReading = false ;
136138 // console.log('🔓 Releasing reader lock...');
@@ -141,17 +143,17 @@ class SerialConnectionHandler {
141143 return bytesRead ;
142144 }
143145
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 ;
148150 }
149151 // console.log('Writing 1 to the serial port...');
150152 // Write a 1 to the serial port
151153 const writer = this . currentPort . writable . getWriter ( ) ;
152154 await writer . write ( new Uint8Array ( [ 1 ] ) ) ;
153155 await writer . close ( ) ;
154- }
156+ }
155157
156158 async getFrame ( totalBytes ) {
157159 if ( ! this . currentPort ) return ;
@@ -161,4 +163,18 @@ class SerialConnectionHandler {
161163 // Read the given amount of bytes
162164 return await this . readBytes ( totalBytes , this . timeout ) ;
163165 }
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+ }
164180}
0 commit comments