1. Install the serialport library
First, the serialport library needs to be installed. It can be installed via npm:
npm install serialport
2. Implementation method
1. Open the serial port and configure the parameters
Create a serial port object and configure serial port parameters such as baud rate, data bit, stop bit and check bit.
const SerialPort = require('serialport'); // Create a serial port objectconst port = new SerialPort('/dev/tty-usbserial1', { baudRate: 9600, // Baud rate dataBits: 8, // Data bits parity: 'none', // Check bit stopBits: 1, // Stop position autoOpen: false // The serial port is not opened automatically}); // Open the serial port((err) => { if (err) { ('Error opening port:', ); return; } ('Port opened successfully'); });
2. Pass information to the serial port
Use the write method to send data to the serial port.
// Send data to the serial port('Hello Serial Port', (err) => { if (err) { ('Error on write:', ); return; } ('Message written'); });
3. Receive serial port information
Receive data sent by the serial port by listening to the data event.
// Listen to data events('data', (data) => { ('Received data:', ()); });
4. Handle errors
Listen to the error event to handle possible errors in serial communication.
// Listen to error events('error', (err) => { ('Error:', ); });
5. Close the serial port
After the communication is completed, the serial port can be closed to free up resources.
// Close the serial portsetTimeout(() => { ((err) => { if (err) { ('Error closing port:', ); return; } ('Port closed successfully'); }); }, 10000); // Close the serial port after 10 seconds
6. Use the parser
To better process the received data, a parser can be used. For example, use the @serialport/parser-inter-byte-timeout parser to handle subcontracting issues.
const { InterByteTimeoutParser } = require('@serialport/parser-inter-byte-timeout'); const parser = (new InterByteTimeoutParser({ interval: 300 })); ('data', (data) => { ('Received data:', ()); });
7. Get the serial port list
You can use the () method to obtain the list of available serial ports in the current system.
().then((ports) => { ((port) => { ('Available port:', ); }); });
3. Complete sample code
Here is a complete sample code showing how to open the serial port, send data, and receive data in :
const SerialPort = require('serialport'); const { InterByteTimeoutParser } = require('@serialport/parser-inter-byte-timeout'); // Create a serial port objectconst port = new SerialPort('/dev/tty-usbserial1', { baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1, autoOpen: false }); // Open the serial port((err) => { if (err) { ('Error opening port:', ); return; } ('Port opened successfully'); // Create a parser const parser = (new InterByteTimeoutParser({ interval: 300 })); // Listen to data events ('data', (data) => { ('Received data:', ()); }); // Listen to error events ('error', (err) => { ('Error:', ); }); // Send data to the serial port ('Hello Serial Port', (err) => { if (err) { ('Error on write:', ); return; } ('Message written'); }); // Close the serial port setTimeout(() => { ((err) => { if (err) { ('Error closing port:', ); return; } ('Port closed successfully'); }); }, 10000); // Close the serial port after 10 seconds});
This is the end of this article about the implementation example of serial communication. For more related serial communication content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!