mirror of
				https://github.com/Pablo2048/modbus-rtu-master.git
				synced 2025-10-31 00:12:42 +01:00 
			
		
		
		
	Merge pull request #1 from Pablo2048/development
Better disconnect handling
This commit is contained in:
		| @@ -37,15 +37,24 @@ class ModbusRTUMaster { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     async disconnect() { |     async disconnect() { | ||||||
|         if (this.reader) { |         console.log('Disconnect requested'); | ||||||
|             await this.reader.releaseLock(); |         try { | ||||||
|         } |             if (this.reader) { | ||||||
|         if (this.writer) { |                 await this.reader.cancel(); | ||||||
|             await this.writer.releaseLock(); |                 await this.reader.releaseLock(); | ||||||
|         } |                 this.reader = null; | ||||||
|         if (this.port) { |             } | ||||||
|             await this.port.close(); |             if (this.writer) { | ||||||
|             console.log('Serial port closed'); |                 await this.writer.releaseLock(); | ||||||
|  |                 this.writer = null; | ||||||
|  |             } | ||||||
|  |             if (this.port) { | ||||||
|  |                 await this.port.close(); | ||||||
|  |                 this.port = null; | ||||||
|  |                 console.log('Serial port closed'); | ||||||
|  |             } | ||||||
|  |         } catch (error) { | ||||||
|  |             console.error('Error during disconnect:', error); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -161,10 +170,16 @@ class ModbusRTUMaster { | |||||||
|             } |             } | ||||||
|         } catch (error) { |         } catch (error) { | ||||||
|             console.error('Error receiving response:', error.message); |             console.error('Error receiving response:', error.message); | ||||||
|  |             if (this.reader) { | ||||||
|  |                 await this.reader.cancel(); | ||||||
|  |                 await this.reader.releaseLock(); | ||||||
|  |                 this.reader = null; | ||||||
|  |                 this.reader = this.port.readable.getReader(); | ||||||
|  |             } | ||||||
|             if (error.message.includes('Device has been lost')) { |             if (error.message.includes('Device has been lost')) { | ||||||
|                 await this.handleDeviceLost(); |                 await this.handleDeviceLost(); | ||||||
|             } |             } | ||||||
|             return { error: error.message }; |             return { error: error.message }; // TODO: or throw error; ? | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user