AdvancedHMI Software

General Category => Support Questions => Topic started by: juanma1364 on March 24, 2020, 01:20:19 AM

Title: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 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
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra 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
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 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.
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra 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".
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 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
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra 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.
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 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.
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra 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.

Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 on March 24, 2020, 06:15:57 PM
You are right Godra .. sorry.

The error also continues.

Send screenshot
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra 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.

Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 on March 24, 2020, 07:28:06 PM

Godra

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

Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra 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.

Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 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.
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 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.
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 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.
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra on March 25, 2020, 12:36:56 PM
You should turn off your packet monitoring since it doesn't really do good to your troubleshooting within AHMI.

Go back to your reply #2 and try those things again since you stated that you got a correct response.

I don't have any other suggestions for you.

Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 on March 25, 2020, 04:54:06 PM
Ok Godra, Thank you very much for your answers.

I don't always use the serial port monitor, only when AdvancedHMI doesn't give me the frame I need and I want to see what it's sending.

Also the serial monitor in this case was not causing me problems.

It was just that it was necessary to send the correct frame requested by the PLC Manufacturer.

I already managed to find the solution, modify this:

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

For this:

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

This last one correctly sends me the frame I need and I get the response from the PLC at Bit level.

I can only optimize the code

Thanks and regards
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra on March 25, 2020, 05:00:56 PM
If that works then the DataReceived event has to work as well.
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 on March 25, 2020, 05:03:53 PM
Surely godra

At this moment I am proceeding to implement the event "DataReceived" with the necessary modifications and I test.

Thank you very much again Godra
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra on March 25, 2020, 05:13:49 PM
You need to understand that your code is executed only when you run the program.

If you want to receive values continuously then you need to subscribe to reading them and that's when you should consider using the DataSubscriber2 component and its Datareceived or DataChanged event.

Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 on March 25, 2020, 05:21:33 PM

Right Godra,
The code is in the

"Private Sub MainForm_Load"

I only did it as proof.

Now I am going to use the AdvancedHMI components to carry out the continuous execution of the program at BITS level.

Godra..you are a genius.

If you have an example code for me that you want to share, I would appreciate it

Thank you
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra on March 25, 2020, 05:31:24 PM
The example code is already in the reply #5.

Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 on March 25, 2020, 05:35:12 PM
Ok Godra..I will try to use that with the associated events.
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 on March 25, 2020, 06:58:13 PM

Godra, I've tried the component
"Data Subscriber 21" and the following code:

 Private Qs As String = ""
    Private Sub DataSubscriber21_DataReturned (As Object sender, and As Drivers.Common.PlcComEventArgs)
        If e.ErrorId = 0 Then
            If e.PlcAddress = "011313,16" Then
                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, and As Drivers.Common.PlcComEventArgs) Handles ModbusRTUCom1.DataReceived
        If e.ErrorId = 0 Then
            If e.PlcAddress = "011313.6" Then
                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


And I still don't get the plot I need.

this gives me the following data output:

"05 01 2C 30 00 01 F4 D1"

And I need this data output:

"05 01 2C 30 00 10 34 DD"

I have this problem with several AdvancedHMI components in their Property "PLCAddressValue", the Modbus function "01" with the address "011313,16" does not support me

The only thing that works for me is the code:

"ModbusRTUCom1.Read (" 011313 ", 16)"

I hope I don't bother with so many questions.

Greetings and thanks
Title: Re: Reading of multiple Modbus RTU bit registers
Post by: Godra on March 25, 2020, 08:42:48 PM
You are too new to AHMI and just doing bunch of weird things.


This is the format for the Read function:

ModbusRTUCom1.Read (PLCAddress, NumberOfElements) and that's why you have it as ModbusRTUCom1.Read (" 011313 ", 16)

So, PLC Address = "011313" and NumberOfElements = 16 and you don't combine these together into "011313,16".


Most controls are designed for 1 element only and don't have the NumberOfElements property.

DataSubscriber2 allows you to enter the NumberOfElements separately, so it should not be added to its PLCAddress as well.

Title: Re: Reading of multiple Modbus RTU bit registers
Post by: juanma1364 on March 25, 2020, 10:00:01 PM
Godra you're right, this is my first experience with AdvancedHMI.

The idea is precisely to know it to get to use it more frequently.

I take advantage of your comments and all your answers to move forward.

Thank you very much Godra