我正在尝试检查寄存器中的值,特别是具有 22 位的寄存器,每个位都需要单独查看。寄存器号是 40321,可以是 40321.01、40321.02 等,具体取决于您要查找的位。如果这些位之一已激活,我需要能够在屏幕上显示。通常每个位都是 0,但是在事件期间该位会变成 1,在这种情况下我想为每个位显示特定消息。
我最初认为每个情况都是不同位的 switch 语句是可行的方法,但是现在我认为用 if 语句检查每个位的状态,然后根据答案移动到 switch 语句是要走的路。但我无法让该程序正常工作,因此希望这里有人对我应该尝试的内容提出建议。谢谢!
faultRegister = tag.read("Fault Register")
switch(faultRegister) {
case 40321.00:
tag.write("Fault Description", "The fault that has occured is: \n SHORT CIRCUIT DETECTED\n Please remove power, \n check wiring before resuming.")
break;
case 40321.01:
tag.write("Fault Description", "The fault that has occured is: \n DRIVE OVER TEMPERATURE \n Please remove power, \n increase air flow, add heatsink, \n or reduce duty cycle.")
break;
case 40321.02:
tag.write("Fault Description", "The fault that has occured is: \n OVER VOLTAGE \n Please remove power, \n DC bus above drive rating, \n Output shutdown.")
break;
case 40321.03:
tag.write("Fault Description", "The fault that has occured is: \n UNDER VOLTAGE \n Please remove power, \n DC bus below minimum rating, \n check for drooping or collapsing, \n improve power source, reduce acceleration.")
break;
case 40321.04:
tag.write("Fault Description", "The fault that has occured is: \n MOTOR TEMPERATURE SENSOR \n ACTIVE \n Please remove power, \n check wiring and input voltage, \n reset and retry.")
break;
case 40321.05:
tag.write("Fault Description", "The fault that has occured is: \n FEEDBACK ERROR OR ENCODER \n POWER ERROR \n Please remove power, \n allow drive to cool down, \n reset and retry.")
break;
case 40321.06:
tag.write("Fault Description", "The fault that has occured is: \n MOTOR PHASING ERROR \n Please remove power, \n allow drive to cool down, \n reset and retry.")
break;
case 40321.07:
tag.write("Fault Description", "The fault that has occured is: \n CURRENT OUTPUT LIMITED \n Please remove power, \n allow drive to cool down \n reset and retry.")
break;
case 40321.08:
tag.write("Fault Description", "The fault that has occured is: \n VOLTAGE OUTPUT LIMITED \n Please remove power, \n allow drive to cool down \n reset and retry.")
break;
case 40321.09:
tag.write("Fault Description", "The fault that has occured is: \n POSITIVE LIMIT SWITCH ACTIVE \n Please remove power, \n allow drive to cool down \n reset and retry.")
break;
case 40321.10:
tag.write("Fault Description", "The fault that has occured is: \n NEGATIVE LIMIT SWITCH ACTIVE \n Please remove power, \n allow drive to cool down \n reset and retry.")
break;
default:
tag.write("Fault Description", "Drive is operating without fault")
}
您可以使用 if 语句和 switch 语句的组合来实现此目的。我在这里向您展示一个示例,告诉您如何检查 22 位寄存器中每个位的状态并为每个位显示特定消息: 这是代码:
const register = 40321.02; // add your values here
40321.02, etc.
// just split register to individual bits
const bits = register.toString().split('.'); // split to decimal points
// iterate over bits
for (let i = 0; i < bits.length; i++) {
const bit = parseFloat(bits[i]);
// checking status of bits
if (bit === 0) {
console.log(`Bit ${i + 1} is inactive`);
} else if (bit === 1) {
console.log(`Bit ${i + 1} is active`);
// using switch for specific message
bit
switch (i + 1) {
case 1:
console.log('Display message for bit 1');
break;
case 2:
console.log('Display message for bit 2');
break;
// Add more cases (if u need)
}
} else {
console.log(`Bit ${i + 1} is in an undefined state`);
}
}
此代码首先使用小数点作为分隔符将寄存器值拆分为各个位。然后检查每个位的状态并显示一条消息。如果位 1 处于活动状态,则会在 switch 语句中进入该情况。
请务必将 (40321.02) 替换为您要检查的实际值。此外,兄弟,您应该根据需要自定义每个位的 switch 语句案例和消息。我希望您会发现这些片段很有帮助。如果您不明白,请告诉我。