Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 13 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: TCP IP Communication between Beijer HMI and other Device
PostPosted: Wed Jun 06, 2018 11:46 am 

Joined: Wed Feb 22, 2012 12:01 am
Posts: 44

Offline
Hi Adam,

Can you please help to provide Timer Threading Script?

Thanks so much!


 Profile  
 
 Post subject: Re: TCP IP Communication between Beijer HMI and other Device
PostPosted: Wed Jun 06, 2018 12:33 pm 

Joined: Mon Jun 11, 2012 2:10 pm
Posts: 119

Offline
Hi ajack,

I've pulled the core part of this out of one of my script modules (Comms). This uses the Created event to start the thread and timer. Sadly, iX does not have a Destroyed event so what I've done is use the class destructor to clean up the threading.

Code:
//--------------------------------------------------------------
// Press F1 to get help about using script.
// To access an object that is not located in the current class, start the call with Globals.
// When using events and timers be cautious not to generate memoryleaks,
// please see the help for more information.
//---------------------------------------------------------------

namespace Neo.ApplicationFramework.Generated
{
   using System;
   using System.Collections;
   using System.Collections.Generic;
   using System.Net;
   using System.Net.Sockets;
   using System.Reflection;
   using System.Text;
   using Thread = System.Threading.Thread;
   using ThreadStart = System.Threading.ThreadStart;
   using AutoResetEvent = System.Threading.AutoResetEvent;
   using Timer = System.Threading.Timer;
   using Timeout = System.Threading.Timeout;
   using Neo.ApplicationFramework.Tools;
   using Neo.ApplicationFramework.Common.Graphics.Logic;
   using Neo.ApplicationFramework.Controls;
   using Neo.ApplicationFramework.Interfaces;
   
   public partial class Comms
   {
      private AutoResetEvent Comms_Event = null;
      private Timer Sync_Timer = null;
      Thread Comms_Thread = null;
      bool Active = false;
      
      void Comms_Created(System.Object sender, System.EventArgs e)
      {
         Active = true;
         Comms_Thread = new Thread(new ThreadStart(CommsThread));
         Comms_Event = new AutoResetEvent(true);
         Comms_Timer = new Timer(Comms_Timer_Tick, null, 1000, 1000); // call timer function every second
         Comms_Thread.Start();
      }
      
      ~Comms() // this function gets called when the Beijer project is closed
      {
         if (Active)
         {
            Active = false;
            
            // stop timer
            try { Comms_Timer.Dispose(); }
            catch { }
            
            // set event to enable thread to terminate
            Comms_Event.Set();
               
            // wait for thread to finish
            try { if (!Comms_Thread.Join(2000)) Comms_Thread.Abort(); }
            catch { }
            
            Comms_Event.Close();
         }
      }
      
      private void Comms_Timer_Tick(Object state)
      {
         // set event
         Comms_Event.Set();
      }
      
      private void EventThread()
      {
         // initialize socket here
         
         while (Active)
         {
            Comms_Event.WaitOne();
            if (!Active) return;
            
            // check socket connection here
            
            // do socket I/O here
         }
      }
   }
}

_________________
Adam M.
Controls Engineer
FlexEnergy


 Profile  
 
 Post subject: Re: TCP IP Communication between Beijer HMI and other Device
PostPosted: Thu Jun 07, 2018 7:29 am 

Joined: Wed Feb 22, 2012 12:01 am
Posts: 44

Offline
Thanks Adam!

:-D


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ]  Go to page Previous  1, 2

All times are UTC - 7 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: