Converting Word to bits-JavaScript and WebServer CLP Siemens
I am getting a variable from the CLP, which is a Word(16).
However, I am not able to 'convert' this Word to 16 bits with their respective values (true or false).
1 Word = 16 bits.
I get the word with the value 'full'.
More details: It is a WebServer application. The files (html, css, ...) are stored inside the Siemens PLC (s7-1200 or s7-1500). Html pages with JavaScript will read some values from the CLP.
Values obtained in Tests:
bit8 256
bit9 512
bit10 1024
bit11 2048
bit12 4096
bit13 8192
bit14 16384
bit15 32768
bit0 1
bit1 2
bit2 4
bit3 8
bit4 16
bit5 32
bit6 64
bit7 128
When bit1 in the CLP is equal to true, it returns the integer value 2 by the Word tag, and so on. When bit1 and bit2 are active, it sums the values.
1 answers
I managed to solve the problem by creating a recursive function, starting with the highest value and decreasing.
The cBit vari variables are temporary variables, which I'm decrementing. The variables bit-- are the final bits originating from Word(16).
I will be optimizing the script better, but it follows the same:
function convertWordToBit(word) {
var cBit00=0;
var cBit01=0;
var cBit02=0;
var cBit03=0;
var cBit04=0;
var cBit05=0;
var cBit06=0;
var cBit07=0;
var cBit08=0;
var cBit09=0;
var cBit10=0;
var cBit11=0;
var cBit12=0;
var cBit13=0;
var cBit14=0;
var cBit15=0;
var bit00=false;
var bit01=false;
var bit02=false;
var bit03=false;
var bit04=false;
var bit05=false;
var bit06=false;
var bit07=false;
var bit08=false;
var bit09=false;
var bit10=false;
var bit11=false;
var bit12=false;
var bit13=false;
var bit14=false;
var bit15=false;
//Carrega valor da word
cBit15=word;
if(cBit15>=32768){
cBit15=cBit15-32768;
bit15=true;
}
if(cBit15<32768){
cBit14=cBit15;
}
if(cBit14>=16384){
cBit14=cBit14-16384;
bit14=true;
}
if(cBit14<16384){
cBit13=cBit14;
}
if(cBit13>=8192){
cBit13=cBit13-8192;
bit13=true;
}
if(cBit13<8192){
cBit12=cBit13;
}
if(cBit12>=4096){
cBit12=cBit12-4096;
bit12=true;
}
if(cBit12<4096){
cBit11=cBit12;
}
if(cBit11>=2048){
cBit11=cBit11-2048;
bit11=true;
}
if(cBit11<2048){
cBit10=cBit11;
}
if(cBit10>=1024){
cBit10=cBit10-1024;
bit10=true;
}
if(cBit10<1024){
cBit09=cBit10;
}
if(cBit09>=512){
cBit09=cBit09-512;
bit09=true;
}
if(cBit09<512){
cBit08=cBit09;
}
if(cBit08>=256){
cBit08=cBit08-256;
bit08=true;
}
if(cBit08<256){
cBit07=cBit08;
}
if(cBit07>=128){
cBit07=cBit07-128;
bit07=true;
}
if(cBit07<128){
cBit06=cBit07;
}
if(cBit06>=64){
cBit06=cBit06-64;
bit06=true;
}
if(cBit06<64){
cBit05=cBit06;
}
if(cBit05>=32){
cBit05=cBit05-32;
bit05=true;
}
if(cBit05<32){
cBit04=cBit05;
}
if(cBit04>=16){
cBit04=cBit04-16;
bit04=true;
}
if(cBit04<16){
cBit03=cBit04;
}
if(cBit03>=8){
cBit03=cBit03-8;
bit03=true;
}
if(cBit03<8){
cBit02=cBit03;
}
if(cBit02>=4){
cBit02=cBit02-4;
bit02=true;
}
if(cBit02<4){
cBit01=cBit02;
}
if(cBit01>=2){
cBit01=cBit01-2;
bit01=true;
}
if(cBit01<2){
cBit00=cBit01;
}
if(cBit00>=1){
cBit00=cBit00-1;
bit00=true;
}
//Exibindo valores
document.getElementById('eme8').innerHTML = bit08;
document.getElementById('eme9').innerHTML = bit09;
document.getElementById('eme10').innerHTML = bit10;
document.getElementById('eme11').innerHTML = bit11;
document.getElementById('eme12').innerHTML = bit12;
document.getElementById('eme13').innerHTML = bit13;
document.getElementById('eme14').innerHTML = bit14;
document.getElementById('eme15').innerHTML = bit15;
document.getElementById('eme0').innerHTML = bit00;
document.getElementById('eme1').innerHTML = bit01;
document.getElementById('eme2').innerHTML = bit02;
document.getElementById('eme3').innerHTML = bit03;
document.getElementById('eme4').innerHTML = bit04;
document.getElementById('eme5').innerHTML = bit05;
document.getElementById('eme6').innerHTML = bit06;
document.getElementById('eme7').innerHTML = bit07;
}