AdvancedHMI Software

General Category => Open Discussion => Topic started by: TheColonel26 on September 29, 2016, 10:46:55 AM

Title: C# Data Subscriber in a non UI class
Post by: TheColonel26 on September 29, 2016, 10:46:55 AM
Code: [Select]

public partial class Main : Form
    {
       
        public Main()
        {
            InitializeComponent();


            Program.Logic.Initialize(omronEthernetFINSCom1);
            Program.Logic.DataSubFixtureID.DataChanged += DataSubFixtureID_DataChanged;
            Program.Logic.SequenceDone += Logic_SequenceDone;
        }

        private void Logic_SequenceDone(object sender, Vitrek_Library.MainSequenceDoneEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void DataSubFixtureID_DataChanged(object sender,
            MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {

            int fixtureId = Convert.ToInt32(e.Values[0]);
            lblPartNumber.Text = $@"Part Number: {fixtureId}";
        }
    }

public class Logic{
        public DataSubscriber DataSubFixtureID;

        public void Initialize(IComComponent comComponent)
        {
            data = new DataInterface();
            data.Load();

            SeqRight = new Sequences();
            SeqRight.SequenceDone += SeqRight_SequenceDone;
            SeqRight.Initialize(new System.IO.Ports.SerialPort());
            SeqLeft = new Sequences();
            SeqLeft.SequenceDone += SeqLeft_SequenceDone;
            SeqLeft.Initialize(new System.IO.Ports.SerialPort());


            DataSubFixtureID = new DataSubscriber
            {
                PLCAddressValue = new PLCAddressItem("D10"),
                ComComponent = comComponent
            };

        }
}

I get no errors but the DataChanged Event is never fired.

I have a DigitalPanelMeter using the same comComponent using the same address and it works just fine.
Title: Re: C# Data Subscriber in a non UI class
Post by: Archie on September 29, 2016, 11:54:09 AM
The DataSubscriber implement ISupportInitialize for proper subscribing. When creating a DataSubscriber in code, be sure to call BeginInit() before setting properties and then calling EndInit() afterwards.
Title: Re: C# Data Subscriber in a non UI class
Post by: TheColonel26 on October 05, 2016, 07:09:02 AM
Just realized I never replied back. This fixed it thank you.