0

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

null
    • smason
    • 7 mths ago
    • Reported - view

    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);
      }
    • timothy_vertin
    • 7 mths ago
    • Reported - view

    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

      • timothy_vertin
      • 7 mths ago
      • Reported - view

      timothy. vertin 

      Never mind, that is a silly question, this doesn't make any sense since the script will only run when the Axiom script is executed, please disregard.  I would like to do this with that last, say 10, strings for display though.  Is this possible with one of the controls or with multiple value boxes and I can display in a grid?

      Cheers,

      Tim

      • smason
      • 7 mths ago
      • Reported - view

      timothy. vertin 
      So once you create the function, I think you should be able to link any number of ValueBoxes to it.

Content aside

print this pagePrint this page
  • 7 mths agoLast active
  • 4Replies
  • 52Views
  • 2 Following