Node.js進行串口通信的實現(xiàn)示例
一、 安裝 serialport 庫
首先,需要安裝 serialport 庫??梢酝ㄟ^ npm 安裝:
npm install serialport
二.、實現(xiàn)方法
1.打開串口并配置參數(shù)
創(chuàng)建一個串口對象并配置串口參數(shù),例如波特率、數(shù)據(jù)位、停止位和校驗位等。
const SerialPort = require('serialport');
// 創(chuàng)建串口對象
const port = new SerialPort('/dev/tty-usbserial1', {
baudRate: 9600, // 波特率
dataBits: 8, // 數(shù)據(jù)位
parity: 'none', // 校驗位
stopBits: 1, // 停止位
autoOpen: false // 不自動打開串口
});
// 打開串口
port.open((err) => {
if (err) {
console.error('Error opening port:', err.message);
return;
}
console.log('Port opened successfully');
});
2. 向串口傳遞信息
使用 write 方法向串口發(fā)送數(shù)據(jù)。
// 向串口發(fā)送數(shù)據(jù)
port.write('Hello Serial Port', (err) => {
if (err) {
console.error('Error on write:', err.message);
return;
}
console.log('Message written');
});
3. 接收串口信息
通過監(jiān)聽 data 事件來接收串口發(fā)送的數(shù)據(jù)。
// 監(jiān)聽數(shù)據(jù)事件
port.on('data', (data) => {
console.log('Received data:', data.toString());
});
4. 處理錯誤
監(jiān)聽 error 事件來處理串口通信中可能出現(xiàn)的錯誤。
// 監(jiān)聽錯誤事件
port.on('error', (err) => {
console.error('Error:', err.message);
});
5. 關(guān)閉串口
在完成通信后,可以關(guān)閉串口以釋放資源。
// 關(guān)閉串口
setTimeout(() => {
port.close((err) => {
if (err) {
console.error('Error closing port:', err.message);
return;
}
console.log('Port closed successfully');
});
}, 10000); // 10秒后關(guān)閉串口
6. 使用解析器
為了更好地處理接收到的數(shù)據(jù),可以使用解析器。例如,使用 @serialport/parser-inter-byte-timeout 解析器來處理分包問題。
const { InterByteTimeoutParser } = require('@serialport/parser-inter-byte-timeout');
const parser = port.pipe(new InterByteTimeoutParser({ interval: 300 }));
parser.on('data', (data) => {
console.log('Received data:', data.toString());
});
7. 獲取串口列表
可以使用 SerialPort.list() 方法獲取當前系統(tǒng)中可用的串口列表。
SerialPort.list().then((ports) => {
ports.forEach((port) => {
console.log('Available port:', port.path);
});
});
三、 完整示例代碼
以下是一個完整的示例代碼,展示了如何在 Node.js 中打開串口、發(fā)送數(shù)據(jù)和接收數(shù)據(jù):
const SerialPort = require('serialport');
const { InterByteTimeoutParser } = require('@serialport/parser-inter-byte-timeout');
// 創(chuàng)建串口對象
const port = new SerialPort('/dev/tty-usbserial1', {
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
autoOpen: false
});
// 打開串口
port.open((err) => {
if (err) {
console.error('Error opening port:', err.message);
return;
}
console.log('Port opened successfully');
// 創(chuàng)建解析器
const parser = port.pipe(new InterByteTimeoutParser({ interval: 300 }));
// 監(jiān)聽數(shù)據(jù)事件
parser.on('data', (data) => {
console.log('Received data:', data.toString());
});
// 監(jiān)聽錯誤事件
port.on('error', (err) => {
console.error('Error:', err.message);
});
// 向串口發(fā)送數(shù)據(jù)
port.write('Hello Serial Port', (err) => {
if (err) {
console.error('Error on write:', err.message);
return;
}
console.log('Message written');
});
// 關(guān)閉串口
setTimeout(() => {
port.close((err) => {
if (err) {
console.error('Error closing port:', err.message);
return;
}
console.log('Port closed successfully');
});
}, 10000); // 10秒后關(guān)閉串口
});
到此這篇關(guān)于Node.js進行串口通信的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Node.js 串口通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Node.js中path模塊的resolve()和join()方法的區(qū)別
這篇文章主要介紹了詳解Node.js中path模塊的resolve()和join()方法的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10

