@@ -110,9 +110,7 @@ function openPort(sock, portPath, baudrate, connMode) {
110
110
var cid = findPortId ( portPath ) ;
111
111
if ( cid ) {
112
112
//Already open; ensure correct baudrate, socket, and connMode, then resolve.
113
- changeBaudrate ( cid , baudrate )
114
- . then ( function ( ) { updatePortSocket ( cid , sock ) } )
115
- . then ( function ( ) { findPort ( cid ) . mode = connMode } )
113
+ updatePort ( sock , cid , connMode , baudrate )
116
114
. then ( function ( ) { resolve ( cid ) } )
117
115
. catch ( function ( e ) { reject ( e ) } ) ;
118
116
} else {
@@ -127,7 +125,7 @@ function openPort(sock, portPath, baudrate, connMode) {
127
125
function ( openInfo ) {
128
126
if ( ! chrome . runtime . lastError ) {
129
127
// No error; create serial port object
130
- addPort ( sock , openInfo . connectionId , connMode , portPath , baudrate ) ;
128
+ addPort ( openInfo . connectionId , sock , connMode , portPath , baudrate ) ;
131
129
log ( "Port " + portPath + " open with ID " + openInfo . connectionId , mStat ) ;
132
130
resolve ( openInfo . connectionId ) ;
133
131
} else {
@@ -281,36 +279,20 @@ chrome.serial.onReceiveError.addListener(function(info) {
281
279
// log("Error: PortID "+info.connectionId+" "+info.error, mDeep);
282
280
} ) ;
283
281
284
- function updatePortSocket ( cid , newSocket ) {
285
- /* Update port cid's socket references
286
- cid is the open port's connection identifier
287
- newSocket is the new socket object*/
288
- let cIdx = findPortIdx ( cid ) ;
289
- if ( cIdx > - 1 ) {
290
- let sIdx = ( newSocket ) ? findSocketIdx ( newSocket ) : - 1 ;
291
- if ( ports [ cIdx ] . socketIdx !== sIdx ) {
292
- // newSocket is different; update required
293
- if ( ports [ cIdx ] . socketIdx !== - 1 ) {
294
- // Adjust existing socket's record
295
- sockets [ ports [ cIdx ] . socketIdx ] . serialIdx = - 1 ;
296
- }
297
- // Update port and socket records
298
- ports [ cIdx ] . socket = newSocket ;
299
- ports [ cIdx ] . socketIdx = sIdx ;
300
- sockets [ sIdx ] . serialIdx = cIdx ;
301
- }
302
- }
303
- }
304
-
305
- function addPort ( socket , cid , connMode , portPath , portBaudrate ) {
282
+ function addPort ( cid , socket , connMode , portPath , portBaudrate ) {
306
283
// Add new serial port record
307
284
let idx = findSocketIdx ( socket ) ;
285
+ /* if (idx = -1) {
286
+ log("Adding port at index " + ports.length, mDbug);
287
+ } else {
288
+ log("Adding port at index " + sockets.length + " referencing socket at index " + idx, mDbug);
289
+ }*/
308
290
ports . push ( {
291
+ connId : cid ,
292
+ path : portPath ,
309
293
socket : socket ,
310
294
socketIdx : idx ,
311
- connId : cid ,
312
295
mode : connMode ,
313
- path : portPath ,
314
296
baud : portBaudrate ,
315
297
packet : { }
316
298
} ) ;
@@ -320,6 +302,39 @@ function addPort(socket, cid, connMode, portPath, portBaudrate) {
320
302
if ( idx > - 1 ) { sockets [ idx ] . serialIdx = ports . length - 1 }
321
303
}
322
304
305
+ function updatePort ( socket , cid , connMode , portBaudrate ) {
306
+ // Update port attributes if necessary
307
+ // Automatically handles special cases like baudrate changes and sockets<->ports links
308
+ return new Promise ( function ( resolve , reject ) {
309
+ let cIdx = findPortIdx ( cid ) ;
310
+ // log("Updating port at index " + cIdx, mDbug);
311
+ if ( cIdx > - 1 ) {
312
+ //Update sockets<->ports links as necessary
313
+ let sIdx = ( socket ) ? findSocketIdx ( socket ) : - 1 ;
314
+ if ( ports [ cIdx ] . socketIdx !== sIdx ) {
315
+ // newSocket is different; update required
316
+ // log(" Linking to socket index " + sIdx, mDbug);
317
+ if ( ports [ cIdx ] . socketIdx !== - 1 ) {
318
+ // Adjust existing socket's record
319
+ sockets [ ports [ cIdx ] . socketIdx ] . serialIdx = - 1 ;
320
+ }
321
+ // Update port and socket records
322
+ ports [ cIdx ] . socket = socket ;
323
+ ports [ cIdx ] . socketIdx = sIdx ;
324
+ if ( sIdx > - 1 ) {
325
+ sockets [ sIdx ] . serialIdx = cIdx ;
326
+ }
327
+ }
328
+ //Update connection mode
329
+ ports [ cIdx ] . mode = connMode ;
330
+ //Update baudrate
331
+ changeBaudrate ( cid , portBaudrate )
332
+ . then ( function ( p ) { resolve ( p ) } )
333
+ . catch ( function ( e ) { reject ( e ) } ) ;
334
+ }
335
+ } )
336
+ }
337
+
323
338
function findPortId ( portPath ) {
324
339
/* Return id (cid) of serial port associated with portPath
325
340
Returns null if not found*/
0 commit comments