Author Topic: C# Subscribe  (Read 6084 times)

headzero

  • Newbie
  • *
  • Posts: 7
    • View Profile
C# Subscribe
« on: July 10, 2017, 10:48:55 AM »
Hi

Can anyone tell me what I am doing wrong with my C# subscribe/callback here. I get the message back with a value of 1 for the subID but the call back never fires.

private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                subID = testplc.Subscribe("dateTime[0]", 6, t, the_callback);
                testplc.PollRateOverride = 1000;
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex));
            }
            MessageBox.Show("Subscription " + Convert.ToString(subID) + " is subscribed" ,"Subscription ID");
        }

        private void the_callback(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {
            txtOutput.Text = e.Values[0];
            label1.Text = "Connected";
        }


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: C# Subscribe
« Reply #1 on: July 10, 2017, 11:43:30 AM »
From what I see th code looks ok. I would either add a BasicLabel to the form and set PLCAddressValue to the address or call a read with it to verify it can be read without error. Subscribe will return an ID and put it in the que, so just subscribing will not tell you if it is a valid tag.

headzero

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: C# Subscribe
« Reply #2 on: July 10, 2017, 11:58:09 AM »
Thank you for your reply. (I'm honored. I have followed your work since the VB6 days )

I have another button on the form that has a simple read and it works fine.


private void button1_Click(object sender, EventArgs e)
        {
            plcResults = testplc.Read("dateTime[0]", 6);
            txtOutput.Text = plcResults[0];
        }

headzero

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: C# Subscribe
« Reply #3 on: July 10, 2017, 12:34:30 PM »
I should mention that I am only referencing your library from a raw C# program.

Here is my simple test program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AdvancedHMIDrivers;
using MfgControl;

namespace AHMI_TestSubscription
{
    public partial class Form1 : Form
    {
        EthernetIPforCLXCom testplc = new EthernetIPforCLXCom();
        string[] plcResults = new string[7];
        int subID;
        int t = 500;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            testplc.IPAddress = "192.168.100.99";
            testplc.ProcessorSlot = 0;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                subID = testplc.Subscribe("dateTime[0]", 6, t, the_callback);
                testplc.PollRateOverride = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex));
            }
            MessageBox.Show("Subscription " + Convert.ToString(subID) + " is subscribed" ,"Subscription ID");
        }

        private void the_callback(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {
            txtOutput.Text = e.Values[0];
            label1.Text = "Connected";
        }

       
        private void button1_Click(object sender, EventArgs e)
        {
            plcResults = testplc.Read("dateTime[0]", 6);
            txtOutput.Text = plcResults[0];
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            testplc.UnSubscribe(subID);
            MessageBox.Show(subID + " is unsubscribed");
        }


    }
}

headzero

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: C# Subscribe
« Reply #4 on: July 10, 2017, 04:11:47 PM »
I started over and created a C# project within the AdvancedHMI solution. I am still getting the same results.
This is not a big deal as I am just experimenting but I would like to get it working.
 

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: C# Subscribe
« Reply #5 on: July 10, 2017, 04:53:44 PM »
I ran two quick tests using version 3.99w and it worked as expected.


- Build the solution
- In Solution Explorer under the AdvancedHMIcs project, open MainForm.cs
- From the Toolbox, add an EthernetIPforCLXCom driver
- From the Toolbox, add a BasicLabel to the form
- Set PLCAddressValue to DINTTag
- Ran the AdvancedHMIcs project to make sure the BasicLabel showed the value in order to validate communication and the tag name

- Closed the application and went back to form designer
- Deleted the BasicLabel
- From the Toolbox, add a Label from the All Windows Forms group
- Double click the form to get to the Load event handler
- Added the following code:

Code: [Select]
private void MainForm_Load(object sender, EventArgs e)
        {
            ethernetIPforCLXCom1.Subscribe("DINTTag", 1, 0, SubscribeCallBack);
        }

        private void SubscribeCallBack(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {
            label2.Text = e.Values[0];
        }

Once again ran the AdvancedHMIcs project and the label2 updated with the value of DINTTag

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: C# Subscribe
« Reply #6 on: July 10, 2017, 05:00:37 PM »
As an additional test, I created the driver instance via code and it still worked as expected.
Code: [Select]
   public partial class MainForm : Form
    {
        AdvancedHMIDrivers.EthernetIPforCLXCom plc;
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            plc = new AdvancedHMIDrivers.EthernetIPforCLXCom();
            plc.IPAddress = "192.168.1.10";

            plc.Subscribe("DINTTag", 1, 0, SubscribeCallBack);
        }

        private void SubscribeCallBack(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {
            label2.Text = e.Values[0];
        }

    }

Are you using version 3.99w? Are you using the AdvancedHMIcs project?

headzero

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: C# Subscribe
« Reply #7 on: July 11, 2017, 03:15:16 PM »
Archie

After beating my head against it for quite some time (days), I decided to unzip a fresh copy of Ver.3.99w (I was using Ver.3.99w) and started from scratch. Everything worked perfectly. I don't know what the problem was or what solved it.

Thank you for your time and this amazing project.

MikeE

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: C# Subscribe
« Reply #8 on: June 13, 2023, 09:24:20 PM »
As per replying to this older topic. 

I have the same issue with subscribing not working; the driver now seems to have changed since the above was posted.  The third parameter is now missing?

Below is the entirety of the solution with a Windows form.  The read tag via the  push button works fine, but the subscription is not working.
The AdvancedHMI provided demo program works; but even the below very simple case is not tripping the event.
I tried a standard (non-UDT tag) and that made no difference.

Code: [Select]

using ClxDriver.Common;

namespace PLCTestB
{
    public partial class Form1 : Form
    {
        ClxDriver.EthernetIPforCLX plc;
        //  HearTBeat
        //IPC.ComsCycleOut
        public Form1()
        {
            InitializeComponent();
            plc = new ClxDriver.EthernetIPforCLX();
            plc.IPAddress = "192.168.1.31";
            plc.Subscribe("IPC.ComsCycleOut", 1, SubscribeCallBack);
        }

        private void SubscribeCallBack(object sender, PlcComEventArgs e)
        {
            label1.Text = e.Values[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = plc.Read("IPC.ComsCycleOut");

        }
    }
}


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: C# Subscribe
« Reply #9 on: June 14, 2023, 08:29:37 AM »
As per replying to this older topic. 

I have the same issue with subscribing not working; the driver now seems to have changed since the above was posted.  The third parameter is now missing?

Below is the entirety of the solution with a Windows form.  The read tag via the  push button works fine, but the subscription is not working.
The AdvancedHMI provided demo program works; but even the below very simple case is not tripping the event.
I tried a standard (non-UDT tag) and that made no difference.

Code: [Select]

using ClxDriver.Common;

namespace PLCTestB
{
    public partial class Form1 : Form
    {
        ClxDriver.EthernetIPforCLX plc;
        //  HearTBeat
        //IPC.ComsCycleOut
        public Form1()
        {
            InitializeComponent();
            plc = new ClxDriver.EthernetIPforCLX();
            plc.IPAddress = "192.168.1.31";
            plc.Subscribe("IPC.ComsCycleOut", 1, SubscribeCallBack);
        }

        private void SubscribeCallBack(object sender, PlcComEventArgs e)
        {
            label1.Text = e.Values[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = plc.Read("IPC.ComsCycleOut");

        }
    }
}

Have you tried it using a DataSubscriber?

christinejohn99

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: C# Subscribe
« Reply #10 on: June 20, 2023, 03:40:48 AM »
I get the message back with a value of 1 for the subID but the call back never fires. because i am very in [link removed by moderator] in USA.
« Last Edit: June 20, 2023, 07:31:04 AM by dmroeder »

MikeE

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: C# Subscribe
« Reply #11 on: June 20, 2023, 09:26:41 PM »
As per replying to this older topic. 

I have the same issue with subscribing not working; the driver now seems to have changed since the above was posted.  The third parameter is now missing?

Below is the entirety of the solution with a Windows form.  The read tag via the  push button works fine, but the subscription is not working.
The AdvancedHMI provided demo program works; but even the below very simple case is not tripping the event.
I tried a standard (non-UDT tag) and that made no difference.

Code: [Select]

using ClxDriver.Common;

namespace PLCTestB
{
    public partial class Form1 : Form
    {
        ClxDriver.EthernetIPforCLX plc;
        //  HearTBeat
        //IPC.ComsCycleOut
        public Form1()
        {
            InitializeComponent();
            plc = new ClxDriver.EthernetIPforCLX();
            plc.IPAddress = "192.168.1.31";
            plc.Subscribe("IPC.ComsCycleOut", 1, SubscribeCallBack);
        }

        private void SubscribeCallBack(object sender, PlcComEventArgs e)
        {
            label1.Text = e.Values[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = plc.Read("IPC.ComsCycleOut");

        }
    }
}

Have you tried it using a DataSubscriber?

DataSubscriber didn't work either; but I found the issue and bit of red-herring
Copying the example to recreate the issue, I didn't include the using ClxDriver...   :o

The actual issue in my real project appears to be multi-threading; which I'll solve in much lower priority for now.