JavaScript API to access serial devices using a Tibbo Web232/Web485 board
npm install @tibbotech/web-serial-library section of the HTML file.
html
`
$3
#### Instantiating Serial Object
In order to use this library, it is necessary to first instantiate the TibboWebSerial.Serial object, passing the correct device as a parameter. This is best done through an onclick event. The code below shows the most basic example of how to do this:
`html
`
When the button is clicked, a list of available USB devices will be displayed. The device called "Web232" or "Web485" should be selected. An instance of TibboWebSerial.Serial called serial has now been created!
#### Declaring Callbacks
There are a number of callbacks functions available, each one is detailed here. To use a callback, it must first be declared. A good time to do this would be just after instantiating the serial object. For example, to generate a callback when a successful connection is made to the device, the connectOK callback could be used. First, the callback should be declared after the serial object is instantiated using the following line of code:
`javascript
serial.connectOK = this.onConnectOK;
`
Next, the body of the callback must be added. In order to display an alert when the connection is ok, the following function could be added:
`javascript
function onConnectOK() {
alert("Successfully connected to device");
}
`
Thus, the previous example becomes:
`html
`
$3
| Function | Parameter | Description | Return Value |
| --------------------------- | ------------------------------------- | :----------------------------------------------------------- | ------------------------------------- |
| setBaud(Baud)` | Number | Sets baud to value passed in parameter