Author Topic: Problem reading negative value modbusrtu?  (Read 5013 times)

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Problem reading negative value modbusrtu?
« on: November 14, 2014, 12:18:47 PM »
Hello,

I think i found a problem when i'm trying to read a negative value.

When my servodrive is running clockwise the value is positive and there is problem
But when my servodrive is running counter clockwise i get a error.

For error see attachment

How can i solve this?

Greets

Martinus

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Problem reading negative value modbusrtu?
« Reply #1 on: November 15, 2014, 05:22:52 AM »
With it stopped at this line can you hover the cursor over "rawdata" to see what the values are.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Problem reading negative value modbusrtu?
« Reply #2 on: November 15, 2014, 05:26:53 AM »
I see where the problem is. Modify the code like this:
Code: [Select]
                    '* Ensure there is enought data to process
                    If rawData.Count >= (startByte + ResultingValuesIndex * BytesPerElement + BytesPerElement) Then
                        For i = 0 To BytesPerElement - 1
                            If address.Address.IndexOf("F4", 0, System.StringComparison.InvariantCultureIgnoreCase) < 0 AndAlso _
                               address.Address.IndexOf("L4", 0, System.StringComparison.InvariantCultureIgnoreCase) < 0 Then
                                '* Use Big Endian
                                Result += CInt(rawData(startByte + ResultingValuesIndex * BytesPerElement + i) * (2 ^ ((BytesPerElement - 1 - i) * 8)))
                            Else
                                '* Reverse the data into little endian so the .NET libaries can convert it to float
                                FloatBytes(i) = rawData(startByte + ResultingValuesIndex * BytesPerElement + i)
                            End If
                        Next
                        If address.Address.IndexOf("F4", 0, System.StringComparison.InvariantCultureIgnoreCase) >= 0 Or _
                               address.Address.IndexOf("L4", 0, System.StringComparison.InvariantCultureIgnoreCase) > 0 Then
                            '* F4 address designates it is a floating point type
                            SwapBytes(FloatBytes, 0)
                            SwapBytes(FloatBytes, 2)
                            ResultingValues(ResultingValuesIndex) = CStr(BitConverter.ToSingle(FloatBytes, 0))
                        Else
                            ResultingValues(ResultingValuesIndex) = CStr(Result)
                        End If

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Problem reading negative value modbusrtu?
« Reply #3 on: November 15, 2014, 07:53:53 AM »
With it stopped at this line can you hover the cursor over "rawdata" to see what the values are.

Hi Archie,

I have tested the code but then al retreived values are zero "0" nothing works then anymore.


As for the raw data value;
-      rawData   Count = 4   
      (0)   255   Byte
                (1)   255   Byte
                (2)   255   Byte
                (4)   255   Byte

Hope this helps out

Greets

Martinus


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Problem reading negative value modbusrtu?
« Reply #4 on: November 15, 2014, 08:17:16 AM »
I see where the problem is, but not completely sure how to fix it since I do not have a PLC to test the code, so try this code:
Code: [Select]
                    If rawData.Count >= (startByte + ResultingValuesIndex * BytesPerElement + BytesPerElement) Then
                        For i = 0 To BytesPerElement - 1
                            If address.Address.IndexOf("F4", 0, System.StringComparison.InvariantCultureIgnoreCase) < 0 AndAlso _
                               address.Address.IndexOf("L4", 0, System.StringComparison.InvariantCultureIgnoreCase) < 0 Then
                                '* Use Big Endian
                                Result += CInt(rawData(startByte + ResultingValuesIndex * BytesPerElement + i) * (2 ^ ((BytesPerElement - 1 - i) * 8)))
                            Else
                                '* Reverse the data into little endian so the .NET libaries can convert it to float
                                FloatBytes(i) = rawData(startByte + ResultingValuesIndex * BytesPerElement + i)
                            End If
                        Next
                        If address.Address.IndexOf("F4", 0, System.StringComparison.InvariantCultureIgnoreCase) >= 0  Then
                            '* F4 address designates it is a floating point type
                            SwapBytes(FloatBytes, 0)
                            SwapBytes(FloatBytes, 2)
                            ResultingValues(ResultingValuesIndex) = CStr(BitConverter.ToSingle(FloatBytes, 0))
                        ElseIf address.Address.IndexOf("F4", 0, System.StringComparison.InvariantCultureIgnoreCase) >= 0 Then
                            '* Convert bytes to a long integer
                            SwapBytes(FloatBytes, 0)
                            SwapBytes(FloatBytes, 2)
                            ResultingValues(ResultingValuesIndex) = CStr(BitConverter.ToInt32(FloatBytes, 0))
                        Else

                            ResultingValues(ResultingValuesIndex) = CStr(Result)
                        End If
                    End If

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Problem reading negative value modbusrtu?
« Reply #5 on: November 15, 2014, 09:07:14 AM »
Hello Archie,

Sorry for the troubles but unfortunate this code is nor working either.

Al the read value turn to zero "0"


Is there anything i could do to help


greets

martinus

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Problem reading negative value modbusrtu?
« Reply #6 on: November 15, 2014, 10:54:53 AM »
I made a mistake in the code. This should be right:

Code: [Select]
                        If address.Address.IndexOf("F4", 0, System.StringComparison.InvariantCultureIgnoreCase) >= 0  Then
                            '* F4 address designates it is a floating point type
                            SwapBytes(FloatBytes, 0)
                            SwapBytes(FloatBytes, 2)
                            ResultingValues(ResultingValuesIndex) = CStr(BitConverter.ToSingle(FloatBytes, 0))
                        ElseIf address.Address.IndexOf("L4", 0, System.StringComparison.InvariantCultureIgnoreCase) >= 0 Then
                            '* Convert bytes to a long integer
                            SwapBytes(FloatBytes, 0)
                            SwapBytes(FloatBytes, 2)
                            ResultingValues(ResultingValuesIndex) = CStr(BitConverter.ToInt32(FloatBytes, 0))
                        Else
                            ResultingValues(ResultingValuesIndex) = CStr(Result)
                        End If

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Problem reading negative value modbusrtu?
« Reply #7 on: November 16, 2014, 05:23:51 AM »
Hello archie,

I wil try as soon as i have time, now its sunday time for the birthday party of my son

greets

Martinus

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Problem reading negative value modbusrtu?
« Reply #8 on: November 17, 2014, 08:14:41 AM »
Hi Archie,

I have tried but no luck.

I have used a blank advancedhmi solution, made the changes in the code
put in a modbusrtu with a basiclabel with plcvalue l47696

The parameter which i'm reading
_n_act (actual speed of motor)

INT32       CANopen 606C:0h
INT16       Modbus 7696
R/-

greets

Martinus

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Problem reading negative value modbusrtu?
« Reply #9 on: November 17, 2014, 08:54:25 AM »
Did you use an upper case "L"?

The address should be L47696

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Problem reading negative value modbusrtu?
« Reply #10 on: November 17, 2014, 01:07:28 PM »
Did you use an upper case "L"?

The address should be L47696

Hi Archie,

yes i used a capital L


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Problem reading negative value modbusrtu?
« Reply #11 on: November 17, 2014, 09:29:12 PM »
I will have to do some testing to figure this out when I can get to a ModbusRTU device, which will be Wednesday or Thurday.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Problem reading negative value modbusrtu?
« Reply #12 on: November 19, 2014, 08:33:13 PM »
I tried using the latest code change posted above with an AutomationDirect Click PLC using address L416385 (INT32), which I set to -1, and it works.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Problem reading negative value modbusrtu?
« Reply #13 on: November 19, 2014, 08:58:24 PM »
With further testing I discovered another problem with reading Long integers. If you read more than 1 long integer that are addressed within 20 elements, the read optimizer return a 0 for all except the first.

This will be fixed in the next release.

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Problem reading negative value modbusrtu?
« Reply #14 on: November 20, 2014, 08:17:23 AM »
Hello archie,

Good to hear that you found a problem...or not good :-)

Do you have a idea when the next versin wil be  released?

Thanks

Martinus