javascript 使用switch和if语句检查号码状态[关闭]

h7appiyu  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(90)

已关闭,此问题需要details or clarity。它目前不接受回答。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

21小时前关门了。
Improve this question
我试图检查一个寄存器的值,特别是一个寄存器,有22位,每个需要单独查看。寄存器编号为40321,可以是40321.01、40321.02等,具体取决于您要查找的位。我需要能够显示在屏幕上,如果其中一位已成为活动。通常每个位都是0,但是在事件期间,该位将变为1,在这种情况下,我想为每个位显示特定的消息。
我原本认为每个case都是一个不同位的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")
}
blpfk2vs

blpfk2vs1#

您可以将数字存储在类型为Number的变量中,然后处理该值以找出每个位的值0/1
一旦你有了这些信息,你就可以在处理过的信息上切换情况条件,寻址单个比特。
这里有一个演示,它将存储在register中的值作为22位值,并将返回一个数组,其中包含与寄存器中的位数一样多的值。一旦数组被返回,你就可以在它上面设置条件。例如,在演示中,我正在检查最高有效位是否设置为零。

function getIndividualBits(value) {
    
    //convert the number to a binary string representation
    let binaryStr = value.toString(2);
            
    // Convert the binary string to an array of integers, starting from the least significant bit
    let bitArray = Array.from(binaryStr).reverse().map(bit => parseInt(bit, 10));
    
    return bitArray;
}

//register value as number literal in binary
let register = 0b1111110001010101010000;
//get an array where at each index contains the value of the n-th bit (LSB is index zero)
let result = getIndividualBits(register);
//Expected output: [0,0,0,0,1,0,1,0,1,0,1,0,1,0,0,0,1,1,1,1,1,1]
console.log(result);  

//for example check if bit 22 (most significant) is zero => false
console.log( result[22] == 0 )

在这里,我使用getIndividualBits函数来帮助另一个函数,该函数将根据传递值中的第一个设置为1的位返回消息:

function getIndividualBits(value) {        
    let binaryStr = value.toString(2);    
    return Array.from(binaryStr).reverse().map(bit => parseInt(bit, 10));    
}

function checkFaults(value){

  const faultDescriptions = [
      //index: 0
      "The fault that has occured is: \n SHORT CIRCUIT DETECTED\n Please remove power, \n check wiring before resuming.",
      //index: 1
      "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.",
      //index: 2
      "The fault that has occured is: \n OVER VOLTAGE \n Please remove power, \n DC bus above drive rating, \n Output shutdown.",
      //index: 3
      "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.",
      //index: 4
      "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.",
      //index: 5
      "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.",
      //index: 6
      "The fault that has occured is: \n MOTOR PHASING ERROR \n Please remove power, \n allow drive to cool down, \n reset and retry.",
      //index: 7
      "The fault that has occured is: \n CURRENT OUTPUT LIMITED \n Please remove power, \n allow drive to cool down \n reset and retry.",
      //index: 8
      "The fault that has occured is: \n VOLTAGE OUTPUT LIMITED \n Please remove power, \n allow drive to cool down \n reset and retry.",
      //index: 9
      "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.",
      //index: 10
      "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."
  ];
  
  const defaultMsg = "Drive is operating without fault";
  
  const bits = getIndividualBits(value);
  //for each fault descriptions
  for(let i=0;i<faultDescriptions.length;i++){
    //if the bits value at that position is == 1
    if(bits[i] == 1)
      //return the corresponding description
      return faultDescriptions[i];
  }
  
  //if no faults where found, return the default case message
  return defaultMsg;
}

let register = 0b1111110001010101010000;
let msg = checkFaults(register);

console.log(msg);
//=> "MOTOR TEMPERATURE SENSOR" - index 4 corresponding to 4th LSB (zero based)

相关问题