Serial connection to microcontroller
npm install @mangar2/serialhelper- Meta
- Class SerialConnection
- Methods
- close
- listAvailablePorts
- on
- open
- sendData
Called when at least one byte has been read from the serial connection
new SerialConnection()
Class shielding the serial port class and provides async interfaces The functions open , close , and send data are async functions . Read data by registering the callback 'data' with the on function
#### close
async close ()
Closes the serial port
#### listAvailablePorts
async listAvailablePorts ()
Lists all available ports to console
#### on
on (event, callback)
Sets a callback .
##### on Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| event | string | event name ( not case sensitive ) for the callback ( supports 'data' ) | |
| callback | Data | function ( . . . parameter ) | |
##### on throws
| Type | Description |
| ---- | ----------- |
| Error | if the event is not supported |
| Error | if the callback is not 'function' |
#### open
async open (serialPortName, baudRate) => {Promise}
Opens the serial port
##### open Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| serialPortName | string | name of the serial port ( example "COM1" ) | |
| baudRate | number | baud rate ( example 9600 ) | |
##### open returns
| Type | Description |
| ---- | ----------- |
| Promise | |
#### sendData
async sendData (byteArray) => {}
Sends data to the serial port
##### sendData Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| byteArray | Array. | serial data as byte stream | |
##### sendData returns
| Type | Description |
| ---- | ----------- |
| `` | true , if sending was successful |