ganutenator
Lifetime Supporting Member
I actually solved this, but I thought this info may help others.
Condition which prompted this need:
I was using a function provided to me by wago to read the serial number and MAC address of the plc. The IT/PC Programmer at work requested this information to help solve a production problem. We are an OEM.
Disclaimer: Most unlike every single plc programming job that I have ever had, No One gets to see the code. But I do realize that comments are also for myself and for the next guy when I die.
The function block that I can't edit, spits out the MAC address as an array [0..5] of bytes in decimal representing the MAC address.
My IT guy wanted that in a string delimited by a hyphen.
Tech support sent me this code. It confused me at first, so I rewrote it differently at the bottom (for others, cuz I care):
I rewrote it as this:
Condition which prompted this need:
I was using a function provided to me by wago to read the serial number and MAC address of the plc. The IT/PC Programmer at work requested this information to help solve a production problem. We are an OEM.
Disclaimer: Most unlike every single plc programming job that I have ever had, No One gets to see the code. But I do realize that comments are also for myself and for the next guy when I die.
The function block that I can't edit, spits out the MAC address as an array [0..5] of bytes in decimal representing the MAC address.
My IT guy wanted that in a string delimited by a hyphen.
Tech support sent me this code. It confused me at first, so I rewrote it differently at the bottom (for others, cuz I care):
Code:
Do_Convert(CLK:=Convert_Enab);
IF Do_Convert.Q = TRUE THEN
HexMask := 16#000F;
Rotate := 0;
FOR I := 0 TO 3 DO
HexNum[I] := DecNumber AND HexMask;
HexNum[I] := ROR(HexNum[I] , Rotate);
HexNumByte[I] := WORD_TO_BYTE(HexNum[I]);
Rotate := Rotate + 4;
HexMask := ROL(HexMask , 4);
CASE HexNumByte[I] OF
0 : CharVal[I] := '0';
1 : CharVal[I] := '1';
2 : CharVal[I] := '2';
3 : CharVal[I] := '3';
4 : CharVal[I] := '4';
5 : CharVal[I] := '5';
6 : CharVal[I] := '6';
7 : CharVal[I] := '7';
8 : CharVal[I] := '8';
9 : CharVal[I] := '9';
10 : CharVal[I] := 'A';
11 : CharVal[I] := 'B';
12 : CharVal[I] := 'C';
13 : CharVal[I] := 'D';
14 : CharVal[I] := 'E';
15 : CharVal[I] := 'F';
END_CASE
END_FOR
END_IF
I rewrote it as this:
Code:
(*AND's w/ 0000 1111 to process the least significant byte*)
HexNum[0]:= DecNumber AND 16#F;
HexNumByte[0]:= WORD_TO_BYTE(HexNum[0]);
(*AND's w/ 1111 0000 to process the most significant byte*)
HexNum[1]:= DecNumber AND 16#F0;
HexNumByte[1]:= WORD_TO_BYTE((HexNum[1] /16));
FOR i:= 0 TO 1 DO
CASE HexNumByte[i] OF
0 : CharVal[i] := '0';
1 : CharVal[i] := '1';
2 : CharVal[i] := '2';
3 : CharVal[i] := '3';
4 : CharVal[i] := '4';
5 : CharVal[i] := '5';
6 : CharVal[i] := '6';
7 : CharVal[i] := '7';
8 : CharVal[i] := '8';
9 : CharVal[i] := '9';
10 : CharVal[i] := 'A';
11 : CharVal[i] := 'B';
12 : CharVal[i] := 'C';
13 : CharVal[i] := 'D';
14 : CharVal[i] := 'E';
15 : CharVal[i] := 'F';
END_CASE
END_FOR