Author Topic: ModbusRTU Serial: reading multiple values  (Read 4163 times)

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
ModbusRTU Serial: reading multiple values
« on: December 10, 2014, 08:01:22 PM »
Ultimately in the application I am working on, I want to read and write to various addresses, but don't need to poll the ModbusRTU device.  So I have tried some of the following code.

'****   This works to read one register ****

        Dim strValue1 As String
        Dim n_strMessage As String

        Try
            strValue1 = ModbusRTUCom1.Read("42817", 1)(0)
            lblReadResult.Text = strValue1

        Catch ex As Exception
            n_strMessage = "Function frmModbusRTUtest: failed with error: " & vbCrLf & ex.Message
            Call ShowMessageBox(clsDisplayMessage.enumDisplayMessageType.Critical, n_strMessage)
        End Try


'****   This code fails to read the multiple registers and generates an exception "No Reponse from PLC. Ensure baud rate is correct."    ****

        Dim strDate As String
        Dim strTime As String
        Dim strValue1 As String
        Dim strValue2 As String
        Dim strValue3 As String
        Dim strValue4 As String
        Dim strValue5 As String
        Dim strValue6 As String
        Dim n_strMessage As String


        Try
            strValue1 = ModbusRTUCom1.Read("42817", 1)(0)
            strValue2 = ModbusRTUCom1.Read("42818", 1)(0)
            strValue3 = ModbusRTUCom1.Read("42819", 1)(0)
            strValue4 = ModbusRTUCom1.Read("42820", 1)(0)
            strValue5 = ModbusRTUCom1.Read("42821", 1)(0)
            strValue6 = ModbusRTUCom1.Read("42822", 1)(0)

            strDate = strValue5 & "-" & strValue4 & "-" & strValue6
            strTime = strValue3 & "-" & strValue2 & "-" & strValue1
            lblReadResult.Text = strDate & "  " & strTime


        Catch ex As Exception
            n_strMessage = "Function frmModbusRTUtest: failed with error: " & vbCrLf & ex.Message
            Call ShowMessageBox(clsDisplayMessage.enumDisplayMessageType.Critical, n_strMessage)
        End Try



It actually worked one time, but has generated the exception all the other times.
Any suggestions???

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5311
    • View Profile
    • AdvancedHMI
Re: ModbusRTU Serial: reading multiple values
« Reply #1 on: December 10, 2014, 08:41:24 PM »
It took me a few tries, but I did get this to replicate. There was a race condition that made it erratic. I will post a new version tonight that fixes it.

To read multiple values, you can use this code:


dim values() as string
values=MobusRTUCom1.Reas("40001",5)

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5311
    • View Profile
    • AdvancedHMI
Re: ModbusRTU Serial: reading multiple values
« Reply #2 on: December 10, 2014, 11:32:25 PM »
Version 3.91 is now available. Try that one to see if it resolves the issues.

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: ModbusRTU Serial: reading multiple values
« Reply #3 on: December 11, 2014, 03:57:42 PM »
I started a new project using the 3.91 version. 

As before, this code still works:
 
Dim strValue1 As String
        Dim n_strMessage As String

        strValue1 = ModbusRTUCom1.Read("42817", 1)(0)   ' What does the (0) at the end of this line do???
        lblReadResult.Text = strValue1


This code works about 50% of the time and the other 50% returns an exception "No Reponse from PLC. Ensure baud rate is correct."

Dim strValue1 As String
Dim strValue2 As String

strValue1 = ModbusRTUCom1.Read("42817", 1)(0)
strValue2 = ModbusRTUCom1.Read("42818", 1)(0)
lblReadResult.Text = strValue2 & ":" & strValue1


Also tried this example for multiple values, the result was an exception "Illegal data address"

Dim values() As String
values = ModbusRTUCom1.Read("42817", 6)
lblReadResult.Text = values(0)


more suggestions?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5311
    • View Profile
    • AdvancedHMI
Re: ModbusRTU Serial: reading multiple values
« Reply #4 on: December 11, 2014, 08:03:44 PM »
        strValue1 = ModbusRTUCom1.Read("42817", 1)(0)   ' What does the (0) at the end of this line do???
Since you are specifying the number of elements to read, it will always return an array even if it is 1 value and a single element array, so the (0) tells it to use the first element of the array.

If you use this format, you do not need the (0) because it assumes you only want 1 element

strValue1 = ModbusRTUCom1.Read("42817")

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5311
    • View Profile
    • AdvancedHMI
Re: ModbusRTU Serial: reading multiple values
« Reply #5 on: December 11, 2014, 08:20:28 PM »
Have you tried the modification in this thread:

http://advancedhmi.com/forum/index.php?topic=529.new;topicseen#new

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5311
    • View Profile
    • AdvancedHMI
Re: ModbusRTU Serial: reading multiple values
« Reply #6 on: December 11, 2014, 09:41:10 PM »
Version 3.92 is now posted, see if it works better.

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: ModbusRTU Serial: reading multiple values
« Reply #7 on: December 16, 2014, 03:02:34 PM »
I am now using v3.93

I found out there is a 5 element register limit in reading and writing to my modbusRTU device.  I changed the code as follows:

            strValue1 = ModbusRTUCom1.Read("42817")
            strValue2 = ModbusRTUCom1.Read("42818")
            strValue3 = ModbusRTUCom1.Read("42819")
            strValue4 = ModbusRTUCom1.Read("42820")
            strValue5 = ModbusRTUCom1.Read("42821")
         

            strDate = strValue5 & "-" & strValue4
            strTime = strValue3 & ":" & strValue2 & ":" & strValue1
            lblReadResult.Text = strDate & "  " & strTime


The first time the code executes, it returns and displays the data.  Polling at 3 second intervals, it may or may not return the data the 2nd time, but by the third time we get the exception "No Response from PLC. Ensure driver settings are correct."

I am guessing it is some type of timing issue with my ModbusRTU device. 

The protocol documentation for my ModbusRTU device states:
Physical layer            =    RS485
Baud Rate             =    9600 bps
Data Length             =    8 bit
Parity                =    None
Stop Bit             =    1
START/STOP             =    silent interval of 3 characters
MIN TIME BETWEEN TWO RETRY    =    500 msec



Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5311
    • View Profile
    • AdvancedHMI
Re: ModbusRTU Serial: reading multiple values
« Reply #8 on: December 16, 2014, 08:32:19 PM »
I tried this code with version 3.94, which will be posted shortly, and could not get any errors:
Code: [Select]
        Dim value As String
        value = ModbusRTUCom1.Read("42817")
        value = ModbusRTUCom1.Read("42818")
        value = ModbusRTUCom1.Read("42819")
        value = ModbusRTUCom1.Read("42820")
        value = ModbusRTUCom1.Read("42821")
        value = ModbusRTUCom1.Read("42822")


        Dim values() As String
        values = ModbusRTUCom1.Read("42817", 6)

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: ModbusRTU Serial: reading multiple values
« Reply #9 on: December 17, 2014, 08:47:38 AM »
Thanks for the feedback.  I think the issue is some limitiation with my ModbusRtU device.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5311
    • View Profile
    • AdvancedHMI
Re: ModbusRTU Serial: reading multiple values
« Reply #10 on: December 17, 2014, 09:07:44 AM »
Does the documentation give a list of function codes that it supports? On reading holding registers, the function code 3 is the only one used whether its a single read or a multiple read.

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: ModbusRTU Serial: reading multiple values
« Reply #11 on: December 23, 2014, 06:57:00 PM »
This issue was that there is a 5 element limit to reading and writing on my ModbusRTU device.  Got to read the fine print......