我正在运行在Oled i2c显示器上打印数据的nodejs有时我收到“ I \ O错误”,这表示电缆问题
但是在我再次运行代码后,一切正常,可以运行几个小时
如何使用try-catch避免此问题?这是代码停止时出现的错误
fs.js:114
throw err;
^
Error: EREMOTEIO: remote I/O error, write
at Object.writeSync (fs.js:573:3)
at Bus.i2cWriteSync (/home/pi/node_modules/i2c-bus/i2c-bus.js:348:13)
at Oled._transfer (/home/pi/node_modules/oled-i2c-bus/oled.js:136:25)
at Oled.<anonymous> (/home/pi/node_modules/oled-i2c-bus/oled.js:329:12)
at /home/pi/node_modules/oled-i2c-bus/oled.js:176:9
at Oled._readI2C (/home/pi/node_modules/oled-i2c-bus/oled.js:160:5)
at tick (/home/pi/node_modules/oled-i2c-bus/oled.js:171:10)
at Timeout._onTimeout (/home/pi/node_modules/oled-i2c-bus/oled.js:183:27)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
这是我的Try-Catch代码
var myArgs = process.argv.slice(2); //take everything after the 2nd arrg-default!
var api = require('mikronode');
var ping = require('ping');
var i2c = require('i2c-bus'),
i2cBus = i2c.openSync(1),
oled = require('oled-i2c-bus');
var font = require('oled-font-5x7');
var opts = {
width: 128,
height: 32,
address: 0x3D
};
var oled = new oled(i2cBus, opts);
var dateFormat = require('dateformat');
oled.clearDisplay();
oled.setCursor(1, 1);
//oled.writeString(font, size 1-3, 'text', color 1\0, true\false-wrapping );
oled.writeString(font, 1, 'Startup Finsih!', 1, true);
var Ts=dateFormat(new Date(), "dd/mm/yy H:MM:ss");
oled.setCursor(1, 16);
oled.writeString(font, 1, Ts, 1, true);
var Nosate="";
var device = new api(myArgs[0]);
var host = myArgs;
var pingRepaly;
ping.sys.probe(host,function(isAlive){
var pingRepaly = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
// console.log(pingRepaly);
if (isAlive == true)
{
console.log(host + " Router is Online!" );
CheckRouter();
else
{
console.log(host + "Router is Offline!" );
process.exit()
}
});
function CheckRouter(){
console.log('Starting the loop');
console.log('***********************************');
var requestLoop = setInterval(function(){
device.connect()
.then(([login])=>{
return login('admin','MyPassW0rd12!');
})
.then(function(conn) {
conn.closeOnDone(true);
var chan3=conn.openChannel("GPS");
chan3.write('/system/script/environment/print\n?name=UnitGPS\n=.proplist=value');
chan3.on('done',function(data3){
Coordinates=data3.data[0][0].value;
Coordinates = Coordinates.split('!');
//console.log('Bus GPS Location is :----------> ' ,Coordinates);
console.log('Bus GPS Location is : ' , Coordinates[2],',',Coordinates[1],',',Coordinates[3],',', Coordinates[4]);
Nosate = Coordinates[4];
chan3.close();
// conn.close();
});
console.log(new Date().toString(),'Done \n\n')
});
if (Nosate < 5)
{
try{
oled.clearDisplay();
Ts=dateFormat(new Date(), "d/m/yy H:MM:ss");
oled.setCursor(1, 0);
oled.writeString(font, 1, Ts, 1, true);
}
catch {}
}
},3000)
}
[出现错误时,它仍会退出代码adnd卡住谢谢,
尝试将代码包装在try/catch
中:
var requestLoop = setInterval(function() {
try {
device.connect()
.then(([login]) => { return login('admin','**redacted**'); })
.then(function (conn) {
// rest of the code
});
// rest of the code
} catch { }
}, 3000);
这将防止您的程序意外崩溃。
通常来说,吞咽异常(捕获它们却什么也不做)是一个坏主意。您应该尝试对它们执行有意义的操作-例如,登录到文件。