Author Topic: Reading of multiple Modbus RTU bit registers  (Read 4279 times)

juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Reading of multiple Modbus RTU bit registers
« on: March 24, 2020, 01:20:19 AM »
Dear I have a problem with reading the states of the outputs of a PLC using modbus RTU.
I need to read the status of 8 outputs of a PLC, the manufacturer of the same determines that to get these states I must access using the function "01" to Read, with the following condition:

"For function 01H, reading multiple bits, always use multiple quantities of 16."

 My question would be with which element of advancedHMI can I achieve this?

I have tried several components of advancedHMI but the hex code I have at the exit of my PC is the following:

"05 01 2C 30 00 01 F4 D1"

and i need it to be

"05 01 2C 30 00 10 34 DD"

On the other hand, I need later a way to interpret and extract the bits that come in the frame to be able to establish the state of each output.

Attached image of the PLC manual

I appreciate your responses.

Best regards

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #1 on: March 24, 2020, 04:01:43 PM »
Do a quick test with the ModbusMaster app and 011312 address, with either 1 or 8 points, just to see if you will get an error or any values:

     https://www.advancedhmi.com/forum/index.php?topic=2567.0

juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #2 on: March 24, 2020, 04:42:11 PM »

Ok Godra I do the test.

How is the procedure to install "Modbus Master"?
I calculate that it will go in the "Drivers" folder, is this correct?

I add information about this.
I did tests with code in AdvancedHMI, and executing the following code:

 'Private Sub MainForm_Load (sender As Object, e As EventArgs) Handles MyBase.Load
    'Dim Q1 () As String
    'Q1 = ModbusRTUCom1.Read ("1: 11313", 16)
    'Label2.Text = Q1 (1)
    'End Sub

 I get the following through the serial port data output.

 "05 01 2C 30 00 10 34 DD"

Which gives me the following answer:

 "05 01 02 0B 00 4F 0C"

and I think that is correct, since my Modbus slave has the address "05"
Function code "01"
 and the function values ​​are
"02 0B" corresponding to binary would be "1000001011"
If I separate the bits, the last ones refer to the values ​​of the output that I have currently active, for example:

"1101" (The last BITS)
it tells me that I have outputs 1,3 and 4 of the PLC are activated and that is true.

Attached photos.

The problem I have now is that I can't display those bits in AdvancedHMI so I can use them for reference.
 Is there a way to see it?

Greetings and Thank you very much for the Help.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #3 on: March 24, 2020, 05:10:40 PM »
ModbusMaster is an app, so you just download the exe file and run it.

Why is it that you are using the code to read the values instead of using visual controls, like BasicLabel, or components like DataSubscriber2?

All the values are returned in the DataReceived event of the ModbusRTUCom driver, where you can also see what starting address the values are coming from and how many values have been returned. There should be bunch of examples in the forum so perform a search for "DataReceived".

juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #4 on: March 24, 2020, 05:19:18 PM »

sorry godra, interpret ModbysMaster as a patch.

On why I use code, it was simply to do a quick test with code that I had read in the advancedHMI forums.

I will try to do what you tell me with the event "DataReceived"

and I will publish the results

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #5 on: March 24, 2020, 05:46:48 PM »
Here is an example of codes that produce the same output:

Code: [Select]
    Private Qs As String = ""
    Private Sub DataSubscriber21_DataReturned(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber21.DataReturned
        If e.ErrorId = 0 Then
            If e.PlcAddress = "011312" Then 'or maybe "011313"
                Me.Label9.Text = ""
                Qs = ""
                For i = 0 To e.Values.Count - 1
                    If i = e.Values.Count - 1 Then
                        Qs &= "Q" & (i + 1) & " = " & e.Values(i)
                    Else
                        Qs &= "Q" & (i + 1) & " = " & e.Values(i) & " , "
                    End If
                Next
                Me.Label9.Text = Qs
            End If
        End If
    End Sub

    Private QQs As String = ""
    Private Sub ModbusRTUCom1_DataReceived(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles ModbusRTUCom1.DataReceived
        If e.ErrorId = 0 Then
            If e.PlcAddress = "011312" Then 'or maybe "011313"
                Me.Label10.Text = ""
                QQs = ""
                For i = 0 To e.Values.Count - 1
                    If i = e.Values.Count - 1 Then
                        QQs &= "Q" & (i + 1) & " = " & e.Values(i)
                    Else
                        QQs &= "Q" & (i + 1) & " = " & e.Values(i) & " , "
                    End If
                Next
                Me.Label10.Text = Qs
            End If
        End If
    End Sub

The code will work provided that you add a DataSubscriber2 to the form, and set it as in the attached picture, as well as add 2 labels and name them Label9 and Label10.

juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #6 on: March 24, 2020, 05:53:24 PM »
Thank you very much Godra for your prompt response, I am going to test the code and comment on the result.

I send a screenshot of what ModbusMaster generated.

The error appears in the image.

I have tried modifying the points.

I have tried 16 points and the error at the exit is
"05 01 02 0B"

1 point and 10 points.
the error at the exit is
"05 81 51 C0"

And I could not receive any response from the slave.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #7 on: March 24, 2020, 06:08:21 PM »
You should try to have only one program accessing a COM port at the time of testing.


juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #8 on: March 24, 2020, 06:15:57 PM »
You are right Godra .. sorry.

The error also continues.

Send screenshot

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #9 on: March 24, 2020, 06:27:03 PM »
You already got a response from address 11313 with 16 points, so try 011313 and 16 points.

This app is intended for quick testing so you can try reading any address.


juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #10 on: March 24, 2020, 07:28:06 PM »

Godra

I have changed the address to 011313 and the error persists.


Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #11 on: March 24, 2020, 08:46:22 PM »
Not receiving any response from the slave should indicate that the connection has not been established.

There is only so much I could suggest about it but you can still try the DataReceived event code.


juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #12 on: March 24, 2020, 08:50:36 PM »

I'm going to test with the event "DataReceived" and publish the result tomorrow.

Thank you very much Godra for all your help.

juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #13 on: March 25, 2020, 07:42:11 AM »
Dear Godra, good morning.

I just ran the test with the event "DataReceived".
The result remains the same, that is, I get an error, the response of the slave through what the serial port shows me is as follows:

"05 81 51 C0 6D"

Interpreting the PLC manufacturer's manual, it tells me that code 51 determines the following:

"
Exception Code 51

Telegram error (function code error, error coding
Registration, data quantity error) "

This error, I interpret, is generated by me because I am not sending you the Correct Mdbus Telegram,
I am sending the following telegram:

"05 01 2C 30 00 01 F4 D1"

When should I send the following telegram:

"05 01 2C 30 00 10 34 DD"

Since the manufacturer of the PLC asks me that when using the Modbus function "01" always use multiples of 16.

How could I send the telegram with the Modbus function "01" and multiples of 16 in AdvancedHMI?

I hope I have been clear in the development of the problem I have.

Thank you very much for the help.

juanma1364

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Reading of multiple Modbus RTU bit registers
« Reply #14 on: March 25, 2020, 10:30:04 AM »
I add information that may help.

I have tried "Modbus Poll" and had no problems, it shows me the bits corresponding to the state of the Outputs.

Attached image.

I only need to be able to see those bits in AdvancedHMI, to then sample them, with different AdvancedHMI components.

Greetings and thanks for the help.
« Last Edit: March 25, 2020, 10:32:42 AM by juanma1364 »