Author Topic: Console Application  (Read 2933 times)

mopo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Console Application
« on: October 03, 2016, 03:57:08 AM »
Hey Guys,
Great software! This could make my life so much easier.

I have the test app working great. I was wondering if anyone is able to point me in the right direction for using the library with a console app. The code below never fires an event.

Code: [Select]
        static void Main(string[] args)
        {

           
            AdvancedHMIDrivers.EthernetIPforSLCMicroCom SLC = new AdvancedHMIDrivers.EthernetIPforSLCMicroCom();
            SLC.IPAddress = "192.168.76.12";
            DataSubscriber ds = new DataSubscriber();
            ds.BeginInit();
            ds.PLCAddressValue = new PLCAddressItem("N9:3");
           
            ds.ComComponent = SLC;
           
            ds.EndInit();
            ds.DataChanged += Ds_DataChanged;
            ds.SuccessfulSubscription += Ds_SuccessfulSubscription;
            while (true)
            {
                Thread.Sleep(10);
            }
        }

        private static void Ds_SuccessfulSubscription(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private static void Ds_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {
            throw new NotImplementedException();
        }
    }

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5365
    • View Profile
    • AdvancedHMI
Re: Console Application
« Reply #1 on: October 04, 2016, 06:23:31 PM »
If you are writing everything in code, it's better to directly subscribe to the driver.
Code: [Select]
     SubscriptionID=EthernetIPforSLCMicroCom1.Subscribe("N7:0", 1, 500, AddressOf DataReceived)

Code: [Select]
    Private Sub DataReceived(sender As Object, e As Drivers.Common.PlcComEventArgs)
        If e.ErrorId = 0 Then
            Console.WriteLine(e.Values(0))
        End If
    End Sub
« Last Edit: October 04, 2016, 06:28:07 PM by Archie »

Godra

  • Hero Member
  • *****
  • Posts: 1457
    • View Profile
Re: Console Application
« Reply #2 on: October 04, 2016, 10:02:33 PM »
As Archie suggested, use the driver only and possibly create some sort of the mini DataSubscriber, similar to this:

Code: [Select]
    class Program
    {
        static int SubscriptionID;
        static string previousValue;
        static void Main(string[] args)
        {
            EthernetIPforSLCMicroCom SLC = new EthernetIPforSLCMicroCom();
            SLC.IPAddress = "192.168.76.12";
            SubscriptionID = SLC.Subscribe("N9:3", 1, 500, SLC_SubscriptionDataReceived);
            previousValue = SLC.Read("N9:3");

            SLC.SubscriptionDataReceived += SLC_SubscriptionDataReceived;

            while (true)
            {
                Thread.Sleep(10);
            }
        }

        private static void SLC_SubscriptionDataReceived(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {
            if (e.ErrorId == 0)
            {
                if (e.Values[0] != previousValue)
                {
                    Console.WriteLine("{0} Data Change, Old Value = {1}, New Value = {2}, {3}", e.PlcAddress, previousValue, e.Values[0], DateTime.Now);
                    previousValue = e.Values[0];
                }
            }
        }
    }

or the mini version of the DataSubscriber2, similar to this:

Code: [Select]
    class Program
    {
        static int SubscriptionID0;
        static int SubscriptionID1;
        static string[] previousValue = new string[2] {"", ""};
        static void Main(string[] args)
        {
            EthernetIPforSLCMicroCom SLC = new EthernetIPforSLCMicroCom();
            SubscriptionID0 = SLC.Subscribe("B3:0/0", 1, 500, SLC_SubscriptionDataReceived);
            previousValue[SubscriptionID0 - 1] = SLC.Read("B3:0/0");
            SubscriptionID1 = SLC.Subscribe("N7:0", 1, 500, SLC_SubscriptionDataReceived);
            previousValue[SubscriptionID1 - 1] = SLC.Read("N7:0");

            SLC.SubscriptionDataReceived += SLC_SubscriptionDataReceived;

            while (true)
            {
                Thread.Sleep(10);
            }
        }

        private static void SLC_SubscriptionDataReceived(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {
            if (e.ErrorId == 0)
            {
                if (e.Values[0] != previousValue[e.SubscriptionID - 1])
                {
                    Console.WriteLine("{0} Data Change, Old Value = {1}, New Value = {2}, {3}", e.PlcAddress, previousValue[e.SubscriptionID - 1], e.Values[0], DateTime.Now);
                    previousValue[e.SubscriptionID - 1] = e.Values[0];
                }
            }
        }
    }
« Last Edit: October 05, 2016, 09:07:14 AM by Godra »

mopo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Console Application
« Reply #3 on: October 04, 2016, 11:22:29 PM »
Thanks Guys,
That worked great!
I found that the Subscribe event handler didn't fire so I just added a SubscriptionDataReceived handler and that fired.

Happy days!

All I need now is time to write some apps with this cool library!

TheColonel26

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Console Application
« Reply #4 on: October 05, 2016, 12:46:21 PM »
I'm trying to do the same thing (run a test console app). but I;m getting a null reference exception thrown for OmronEthernetFINSCom.synchronizingObject.

Since I don't have a form to use as the synchronizingObject in my test app what can I do to resolve this?

Code: [Select]
            OmronEthernetFINSCom comComponent = new OmronEthernetFINSCom()
            {
                IPAddress = "192.168.1.80",
                ProtocolType = OmronEthernetFINSCom.ProtocolOptions.UDP
            };

            main.Initialize(comComponent);


Code: [Select]
public void Initialize(OmronEthernetFINSCom comComponent)
        {

            this.comComponent = comComponent;
            this.comComponent.Subscribe(RUN, 1, 100, DataSubRun_DataReceived);
            this.comComponent.Subscribe(FIXTURE_ID, 1, 100, DataSubFixtureID_DataReceived);
}

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5365
    • View Profile
    • AdvancedHMI
Re: Console Application
« Reply #5 on: October 05, 2016, 01:45:06 PM »
You are probably the first person to try a non-GUI application with the Omron drivers, therefore it has never been tested. But I believe there is an easy fix:

- In Solution Explorer under the AdvancedHMIDrivers project, expand down to the \Omron\FINS folder
- Right click FINSBaseCom.vb and select View Code
- Go to Line 767 which you should see this code:
Code: [Select]
                     If Not SubscriptionList(i).MarkForDeletion Then
                         m_SynchronizingObject.BeginInvoke(SubscriptionList(i).dlgCallback, x)
                     End If
- Replace that code with this code:
Code: [Select]
                          If Not SubscriptionList(i).MarkForDeletion Then
                              If m_SynchronizingObject IsNot Nothing AndAlso m_SynchronizingObject.InvokeRequired Then
                                  m_SynchronizingObject.BeginInvoke(SubscriptionList(i).dlgCallback, x)
                              Else
                                  SubscriptionList(i).dlgCallback(Me, f)
                              End If
                          End If

TheColonel26

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Console Application
« Reply #6 on: October 05, 2016, 03:11:04 PM »
Awesome! Thanks

Note: I also needed to change Line 544

Code: [Select]
m_SynchronizingObject.BeginInvoke(SubscriptionList(i).dlgCallback, x)
to

Code: [Select]
  If m_SynchronizingObject IsNot Nothing AndAlso m_SynchronizingObject.InvokeRequired Then
      m_SynchronizingObject.BeginInvoke(SubscriptionList(i).dlgCallback, x)
  Else
      SubscriptionList(i).dlgCallback(Me, f)
  End If