|  |  |  | @ -14,8 +14,8 @@ export async function connectToSerialPort(port, baud) { | 
		
	
		
			
				|  |  |  |  |     const readableStreamClosed = port.readable.pipeTo(textDecoder.writable); | 
		
	
		
			
				|  |  |  |  |     globals.setReader(textDecoder.readable.getReader()); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     const controller = new AbortController(); | 
		
	
		
			
				|  |  |  |  |     const signal = controller.signal; | 
		
	
		
			
				|  |  |  |  |     globals.setController(new AbortController()); | 
		
	
		
			
				|  |  |  |  |     const signal = globals.controller.signal; | 
		
	
		
			
				|  |  |  |  |     try { | 
		
	
		
			
				|  |  |  |  |         globals.setPortConnected(true); | 
		
	
		
			
				|  |  |  |  |         while (globals.isPortConnected) { | 
		
	
	
		
			
				
					|  |  |  | @ -34,23 +34,22 @@ export async function connectToSerialPort(port, baud) { | 
		
	
		
			
				|  |  |  |  |                 addText(line); | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         console.log("Exiting serial print loop") | 
		
	
		
			
				|  |  |  |  |     } catch (error) { | 
		
	
		
			
				|  |  |  |  |         console.error('Error reading data from serial port:', error); | 
		
	
		
			
				|  |  |  |  |     } finally { | 
		
	
		
			
				|  |  |  |  |         // Port cleanup
 | 
		
	
		
			
				|  |  |  |  |         controller.abort(); | 
		
	
		
			
				|  |  |  |  |         console.log("Cleaning up port.") | 
		
	
		
			
				|  |  |  |  |         globals.controller.abort(); | 
		
	
		
			
				|  |  |  |  |         console.log("Releasing writer lock") | 
		
	
		
			
				|  |  |  |  |         globals.writer.releaseLock(); | 
		
	
		
			
				|  |  |  |  |         globals.reader.releaseLock(); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         try { | 
		
	
		
			
				|  |  |  |  |             globals.reader.cancel(); | 
		
	
		
			
				|  |  |  |  |         } catch (error) { | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         try { | 
		
	
		
			
				|  |  |  |  |             await readableStreamClosed; | 
		
	
		
			
				|  |  |  |  |         } catch (error) { | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         // Since we are using a readableSteam we should call cancel instead of globals.reader.releaseLock();
 | 
		
	
		
			
				|  |  |  |  |         globals.reader.cancel().then(() => { | 
		
	
		
			
				|  |  |  |  |             console.log('Stream canceled'); | 
		
	
		
			
				|  |  |  |  |         }).catch(error => { | 
		
	
		
			
				|  |  |  |  |             console.error('Error canceling the stream:', error); | 
		
	
		
			
				|  |  |  |  |         }); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         try { | 
		
	
		
			
				|  |  |  |  |             await readableStreamClosed; | 
		
	
	
		
			
				
					|  |  |  | @ -62,12 +61,13 @@ export async function connectToSerialPort(port, baud) { | 
		
	
		
			
				|  |  |  |  |         } catch (error) { | 
		
	
		
			
				|  |  |  |  |             console.error("Error encountered closing port:", error); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         console.log("Port closed.") | 
		
	
		
			
				|  |  |  |  |         onPortDisconnect() | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | export function transmitContents(input) { | 
		
	
		
			
				|  |  |  |  |     const encoded_string = globals.encoder.encode(input + '\r') | 
		
	
		
			
				|  |  |  |  |     console.log("Binary Contents: ", encoded_string) | 
		
	
		
			
				|  |  |  |  |     globals.writer.write(encoded_string) | 
		
	
		
			
				|  |  |  |  |     console.log("Binary Contents: ", encoded_string); | 
		
	
		
			
				|  |  |  |  |     globals.writer.write(encoded_string); | 
		
	
		
			
				|  |  |  |  | } | 
		
	
	
		
			
				
					|  |  |  | 
 |