Node.js進(jìn)行串口通信的實(shí)現(xiàn)示例
一、 安裝 serialport 庫
首先,需要安裝 serialport 庫??梢酝ㄟ^ npm 安裝:
npm install serialport
二.、實(shí)現(xiàn)方法
1.打開串口并配置參數(shù)
創(chuàng)建一個(gè)串口對(duì)象并配置串口參數(shù),例如波特率、數(shù)據(jù)位、停止位和校驗(yàn)位等。
const SerialPort = require('serialport'); // 創(chuàng)建串口對(duì)象 const port = new SerialPort('/dev/tty-usbserial1', { baudRate: 9600, // 波特率 dataBits: 8, // 數(shù)據(jù)位 parity: 'none', // 校驗(yàn)位 stopBits: 1, // 停止位 autoOpen: false // 不自動(dòng)打開串口 }); // 打開串口 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. 處理錯(cuò)誤
監(jiān)聽 error 事件來處理串口通信中可能出現(xiàn)的錯(cuò)誤。
// 監(jiān)聽錯(cuò)誤事件 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() 方法獲取當(dāng)前系統(tǒng)中可用的串口列表。
SerialPort.list().then((ports) => { ports.forEach((port) => { console.log('Available port:', port.path); }); });
三、 完整示例代碼
以下是一個(gè)完整的示例代碼,展示了如何在 Node.js 中打開串口、發(fā)送數(shù)據(jù)和接收數(shù)據(jù):
const SerialPort = require('serialport'); const { InterByteTimeoutParser } = require('@serialport/parser-inter-byte-timeout'); // 創(chuàng)建串口對(duì)象 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)聽錯(cuò)誤事件 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進(jìn)行串口通信的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Node.js 串口通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于Node.js和Socket.IO實(shí)現(xiàn)實(shí)時(shí)通信功能
- 淺析如何在Bash中調(diào)用Node運(yùn)行JS文件進(jìn)行數(shù)據(jù)通信
- Node.js中的進(jìn)程間通信
- 基于node的tcp客戶端和服務(wù)端的簡單通信
- Node與Python 雙向通信的實(shí)現(xiàn)代碼
- Nodejs環(huán)境實(shí)現(xiàn)socket通信過程解析
- node實(shí)現(xiàn)socket鏈接與GPRS進(jìn)行通信的方法
- node 利用進(jìn)程通信實(shí)現(xiàn)Cluster共享內(nèi)存
相關(guān)文章
詳解Node.js中path模塊的resolve()和join()方法的區(qū)別
這篇文章主要介紹了詳解Node.js中path模塊的resolve()和join()方法的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10VsCode與Node.js知識(shí)點(diǎn)詳解
在本篇文章中小編給大家分享了關(guān)于VsCode與Node.js的相關(guān)知識(shí)點(diǎn)以及安裝等內(nèi)容,需要的朋友們可以參考下。2019-09-09詳解node中創(chuàng)建服務(wù)進(jìn)程
本篇文章主要介紹了詳解node中創(chuàng)建服務(wù)進(jìn)程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05詳解nodejs的express如何自動(dòng)生成項(xiàng)目框架
本篇文章主要介紹了nodejs的express如何自動(dòng)生成項(xiàng)目框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Pycharm配置Node.js運(yùn)行js代碼詳細(xì)過程
在PyCharm中寫JavaScript代碼并進(jìn)行調(diào)試是非常方便的,但是有些用戶可能對(duì)如何在PyCharm中準(zhǔn)確地運(yùn)行JavaScript代碼感到困惑,這篇文章主要給大家介紹了關(guān)于Pycharm配置Node.js運(yùn)行js代碼的相關(guān)資料,需要的朋友可以參考下2023-11-11