TCP IP Communication between Beijer HMI and other Devices

Discussion of configuring and troubleshooting communication to PLC's or other devices using iX Developer.
ajack
Posts: 44
Joined: Wed Feb 22, 2012 12:01 am

Re: TCP IP Communication between Beijer HMI and other Device

Post by ajack »

Hi Adam,

Can you please help to provide Timer Threading Script?

Thanks so much!

AMitchneck
Posts: 137
Joined: Mon Jun 11, 2012 2:10 pm

Re: TCP IP Communication between Beijer HMI and other Device

Post by AMitchneck »

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: Select all

//--------------------------------------------------------------
// 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

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

Re: TCP IP Communication between Beijer HMI and other Device

Post by ajack »

Thanks Adam!

:-D

Post Reply