String Byte Swap
I am successfully getting a string that is 13 words long, which is two ASCII characters per word over Modbus TCP/IP. Unfortunately the chars within each word are reversed. Is there any way in Calcs & Events to first swap the two parts of the word, then concatenate them back together to get them in the correct order? If not in Calcs & Events but maybe display in the right order in Axiom with a custom script?
Example:
EM 02 00 08 95 97 24 1M 70 >>>>>>>>> ME 20 00 80 59 79 42 M1 07
(added spaces for clarity)
P.S. I cannot change the format at the data source (device)
Any help would be much appreciated!
Cheers,
Tim
4 replies
-
Hi timothy. vertin ,
You could try this script in Axiom. It would be an OnBeginValueChange function linked to a ValueBox that is displaying the string value.
public void ValueBox1_OnBeginValueChange(object sender, TagSubscriptionArgs subscription, TVQ tvq) { if (tvq == null) return; var text = tvq.ValueToString(); for (int i = 1 ; i < text.Length; i += 2) text = SwapChars(text, i - 1, i); tvq.Value = text; } static string SwapChars(String str, int index1, int index2) { char[] strChar = str.ToCharArray(); char temp = strChar[index1]; strChar[index1] = strChar[index2]; strChar[index2] = temp; return new String(strChar); }
-
Hey Steve,
Thank you for the quick reply. I will give this a shot, this looks great! Is there any way to write the result back to a different tag in the historian?
Regards,
Tim