[英]NodeJS and node-serialport i'm having trouble writing
我在通過RS232串行連接向RFID閱讀器發送命令時遇到問題。 我可以連接到閱讀器並向其中寫入消息,但沒有得到任何回應。
在讀者的文檔中,我可以找到關於此問題的可能解釋(第16頁):
如果協議幀出現故障,閱讀器將沒有答復。
以下信息可用於協議幀格式:
因此,清單(將所有標簽置於范圍內)命令的十六進制為0x02 0x00 0x09 0x00 0xB0 0x01 0x00 0xCA 0x86
我100%確保閱讀器正常工作並且串行設置正確,但是我不確定我是否以正確的方式使用了Buffer。
這是我目前的代碼:
settings.json
{
"serialport":"COM3",
"baudrate":38400
}
app.js
var settings = require('./settings');
var serialport= require('serialport');
var SerialPort = serialport.SerialPort;
var inventorycommand = new Buffer([0x02,0x00,0x09,0x00,0xB0,0x01,0x00,0xCA,0x86],'hex');
var serialconnection = new SerialPort(settings.serialport,{baudRate:settings.baudrate,parity:'even',encoding:'hex'},false);
serialconnection.open(portOpened);
function portOpened(err) {
if(err)console.log('ERR: '+ err);
console.log('serial port opened: '+ settings.serialport+' with baudrate '+ settings.baudrate);
setTimeout(function(){
serialconnection.write(inventorycommand.toString('hex'));
console.log(inventorycommand.toString('hex'));
},1000);
serialconnection.on('data',dataReceived);
serialconnection.on('close', portClosed);
serialconnection.on('error',errorReceived);
function dataReceived(data) {
console.log('data received: ' +data);
}
function portClosed() {
console.log('port closed.')
}
function errorReceived(err) {
console.log('error: ' + err);
}
}
似乎將回調函數添加到write()
函數調用中解決了該問題。
serialconnection.write(inventorycommand,function(err,result){
if(err){
console.log('ERR: ' + err);
}
console.log('result:' + result)
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.