@@ -72,21 +72,14 @@ class Serial extends EventTarget {
7272 */
7373 selectProtocol ( portPath ) {
7474 // Determine which protocol to use based on port path
75- let protocol ;
76-
77- // Select protocol based on port path. Default to webSerial for
78- // typical serial device identifiers (e.g., COM1, /dev/ttyUSB0).
79- if ( portPath === "virtual" ) {
80- protocol = this . _protocols . find ( ( p ) => p . name === "virtual" ) ?. instance ;
81- } else if ( portPath === "manual" || / ^ ( t c p | w s ) : \/ \/ ( [ A - Z a - z 0 - 9 . - ] + ) (?: : ( \d + ) ) ? $ / . test ( portPath ) ) {
82- protocol = this . _protocols . find ( ( p ) => p . name === "websocket" ) ?. instance ;
83- } else if ( portPath . startsWith ( "bluetooth" ) ) {
84- protocol = this . _protocols . find ( ( p ) => p . name === "webbluetooth" ) ?. instance ;
85- } else {
86- protocol = this . _protocols . find ( ( p ) => p . name === "webserial" ) ?. instance ;
75+ const s = typeof portPath === "string" ? portPath : "" ;
76+ // Default to webserial for typical serial device identifiers.
77+ if ( s === "virtual" ) return this . _getProtocol ?. ( "virtual" ) ;
78+ if ( s === "manual" || / ^ ( t c p | w s | w s s ) : \/ \/ [ A - Z a - z 0 - 9 . - ] + (?: : \d + ) ? ( \/ .* ) ? $ / . test ( s ) ) {
79+ return this . _getProtocol ?. ( "websocket" ) ;
8780 }
88-
89- return protocol ;
81+ if ( s . startsWith ( "bluetooth" ) ) return this . _getProtocol ?. ( "webbluetooth" ) ;
82+ return this . _getProtocol ?. ( "webserial" ) ;
9083 }
9184
9285 /**
@@ -102,10 +95,9 @@ class Serial extends EventTarget {
10295 result = await this . _protocol . connect ( path , options , callback ) ;
10396 } catch ( error ) {
10497 console . error ( `${ this . logHead } Error during connection:` , error ) ;
105- } finally {
106- callback && callback ( result ) ;
107- return result ;
10898 }
99+ callback && callback ( result ) ;
100+ return result ;
109101 }
110102
111103 /**
@@ -119,10 +111,9 @@ class Serial extends EventTarget {
119111 result = await this . _protocol ?. disconnect ( ) ;
120112 } catch ( error ) {
121113 console . error ( `${ this . logHead } Error during disconnect:` , error ) ;
122- } finally {
123- callback && callback ( result ) ;
124- return result ;
125114 }
115+ callback && callback ( result ) ;
116+ return result ;
126117 }
127118
128119 /**
@@ -134,7 +125,6 @@ class Serial extends EventTarget {
134125 if ( callback ) callback ( { bytesSent : 0 } ) ;
135126 return { bytesSent : 0 } ;
136127 }
137-
138128 return this . _protocol . send ( data , callback ) ;
139129 }
140130
@@ -175,12 +165,11 @@ class Serial extends EventTarget {
175165 let result = false ;
176166 try {
177167 const targetProtocol = this . _protocols . find ( ( p ) => p . name === protocolType ?. toLowerCase ( ) ) ?. instance ;
178- result = await targetProtocol . requestPermissionDevice ( showAllDevices ) ;
168+ result = await targetProtocol ? .requestPermissionDevice ( showAllDevices ) ;
179169 } catch ( error ) {
180170 console . error ( `${ this . logHead } Error requesting device permission:` , error ) ;
181- } finally {
182- return result ;
183171 }
172+ return result ;
184173 }
185174
186175 /**
0 commit comments