Author Topic: REAL and TIME datatypes SwapBytes and SwapWords ModbusTCP  (Read 1067 times)

EE_Kraig

  • Newbie
  • *
  • Posts: 37
    • View Profile
REAL and TIME datatypes SwapBytes and SwapWords ModbusTCP
« on: March 07, 2017, 05:13:27 PM »
If I set the Modbus driver like so:

SwapBytes = False
SwapWords = True

and I read a 32-bit real and convert like so:
Code: [Select]
         string[] values = modbusTCPCom1.Read("30006", 2);
         byte[] byteValue = new byte[4];

         BitConverter.GetBytes(Convert.ToInt16(values[0])).CopyTo(byteValue, 0);
         var floatValue = ConvertByteToFloat(byteValue);

      public static float[] ConvertByteToFloat(byte[] array)
      {
         float[] floatArr = new float[array.Length / 4];
         for (int i = 0; i < floatArr.Length; i++)
         {
            if (BitConverter.IsLittleEndian)
            {
               Array.Reverse(array, i * 4, 4);
            }
            floatArr[i] = BitConverter.ToSingle(array, i * 4);
         }
         return floatArr;
      }

All is great. But, I have a couple of TIME variables that then get broken. If I swap the bytes and words settings, then the TIME vars are correct and the float is all messed up.

Can I have my cake and eat it too or am I going to have to manipulate one of them in code? Maybe a tweak in the Modbus driver?

EE_Kraig

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: REAL and TIME datatypes SwapBytes and SwapWords ModbusTCP
« Reply #1 on: March 07, 2017, 05:31:42 PM »
Actually, the SwapBytes then messes up my HOA. So it looks like a driver change to how it handles Reals would be nice. Not too worried about the Time datatype since I should have made those Reals anyway.

Thanks!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: REAL and TIME datatypes SwapBytes and SwapWords ModbusTCP
« Reply #2 on: March 07, 2017, 05:43:06 PM »
Did you try reading you float like this:

modbusTCPCom1.Read("F30006")

EE_Kraig

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: REAL and TIME datatypes SwapBytes and SwapWords ModbusTCP
« Reply #3 on: March 07, 2017, 08:28:47 PM »
Did you try reading you float like this:

modbusTCPCom1.Read("F30006")

10 = 0x41200000

SwapBytes=True
SwapWords=False
2.33625E-41
0x00004120
HOA works

SwapBytes=False
SwapWords=True
1.634774E-19
0x20410000
HOA broke

SwapBytes=True
SwapWords=True
10
0x41200000
HOA works

TIME works too.

Thanks!

Chipprogr

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: REAL and TIME datatypes SwapBytes and SwapWords ModbusTCP
« Reply #4 on: June 13, 2017, 05:51:33 PM »
Sorry to raise an old thread but...

     how would one know to place an 'F' in front of the Modbus address to read as a float? Is there a document somewhere that shows this and maybe other switches?

Thanks
Eric

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: REAL and TIME datatypes SwapBytes and SwapWords ModbusTCP
« Reply #5 on: June 14, 2017, 06:08:15 PM »
how would one know to place an 'F' in front of the Modbus address to read as a float? Is there a document somewhere that shows this and maybe other switches?
http://advancedhmi.com/documentation/index.php?title=ModbusRTUCom

http://advancedhmi.com/forum/index.php?topic=765.msg3647#msg3647