Author Topic: C# Windows form Application Data Subscriber ModbusTCP Communication  (Read 1062 times)

RockB

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hello,

I am trying to get data from Input holding registers from a PLC through ModbusTCP communication. I have added the ModbusTCP driver to MainForm.cs and have set the IP address.

I have two digital panel meters in the MainForm. If I set the "PLCAddressValue" property for these components and run the applications, I get the data just fine. However, the Incoming data has to be modified before it is displayed on the meter. For this purpose, I am exploring the DataSubscriber

I understand that Data subscriber automatically refreshes the data values. Thus, this avoids calling
Code: [Select]
modbusTCPCom1.BeginRead("address") repeatedly.

I want to subscribe to 8 consecutive registers. Their addresses are as follows: 40001 to 40008. How can get the values of these registers using data subscriber? I have gone through few posts and have this so far

Code: [Select]
private void MainForm_Load(object sender, EventArgs e)
        {
            Int val1 = modbusTCPCom1.Subscribe("40001",1,100, SubscribeCallBack);
        }

Question 1: How can I read/store values of all registers into an integer array and read individual values later? Do I need an individual subscription for each of those registers?

Question 2: What should the "SubscribeCallBack" event include to handle communication lost to PLC?

Question 3: What is the difference between DataSubscriber and DataSubscriber2?



Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
- Add a DataSubscriber2 to the form
- In the Properties Window click on PLCAddressItems property to open the window
- Add a new item
- Set PLCAddress to 40001
- Set NumberOfElements to 8
- Close that window
- Double click the DataSubscriber2 in the component tray to get back to code
- Use the array e.Values() to access the 8 values that are returned



DataSubscriber2 is a newer component that allows multiple items. DataSubscriber is for backward compatibility and only accepts a single address.

RockB

  • Newbie
  • *
  • Posts: 3
    • View Profile
@Archie When I try to Add addresses to "PLCAddressValueItems" in properties of datasubscriber2, I get an error saying "Object does not match the target type"

Edit: I have also observed this problem when I add "ChartBySampling" component and try to edit the "PLCAddressItems" property.

I am using the latest version of Advanced HMI (AdvancedHMIv399xR1). The solution of VS2017 has three projects in it: AdvancedHMIcs, AdvancedHMIDrivers and AdvancedHMIControls. I have manually removed the AdvancedHMI (vb project) since this going to be a C# application.

I have manually added "MfgControl.AdvancedHMI.Controls.dll" and "MfgControl.AdvancedHMI.Drivers.dll" references to AdvancedHMIcs project
« Last Edit: May 03, 2019, 04:05:49 PM by RockB »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
That error is a quirk of Visual Studio. You can resolve it like this:

- Window->Close All Document
- Build->Rebuild Solution
- Exit Visual Studio
- Re-open your solution



RockB

  • Newbie
  • *
  • Posts: 3
    • View Profile
Thanks, Archie! that worked.

I am getting temperature values from ModbusTCP. I want to plot them on a trend chart with moving X-axis. The scale of the Y-axis is usually fixed. I want X-axis to show time.

Which of the charts should I use: Basic Trend Chart, chart by array or chart by sampling? 


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Probably ChartBySampling

If you are using the latest beta version, then ChartWithLogging will also store the values to a file.