Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:46:06+00:00 2026-06-07T17:46:06+00:00

May I ask for help with the following? I am attempting to connect and

  • 0

May I ask for help with the following?

I am attempting to connect and control three pieces of household electronic equipment by computer through a GlobalCache GC-100 and iTach. As you will see in the following code, I created a class (“GlobalCacheAdapter”) that can communicate and control the equipment, and created an instance of the class for each piece of equipment. Although each instance seems to work well with communicating and in controlling each piece of equipment, the *feedback returned from the equipment* seems only to be visible at the defining class level’s – “ReaderThreadProc” procedure. Further processing of the feedback is required for each piece of equipment and I am uncertain as to how to forward this feedback at the equipment specific instance-level. I suspect that an instance-specific EventHandler will need to be implemented; however I am not aware as to how to implement this type of instance-specific EventHandler in order to complete processing and update the appropriate controls.

Any help wold be greatly appreciated.

  using System;
  using System.IO;
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  using System.Windows.Forms;

  namespace WindowsFormsApplication1
  {
       public partial class Form1 : Form
       {

             // Create three new instances of GlobalCacheAdaptor and connect.
             // GC-100 (Elan) 192.168.1.70 4998  
             // GC-100 (TuneSuite) 192.168.1.70 5000  
             // GC iTach (Lighting) 192.168.1.71 4999

             private GlobalCacheAdaptor elanGlobalCacheAdaptor;
             private GlobalCacheAdaptor tuneSuiteGlobalCacheAdaptor;
             private GlobalCacheAdaptor lutronGlobalCacheAdaptor;

             public Form1()
             {
                  InitializeComponent();

                  elanGlobalCacheAdaptor = new GlobalCacheAdaptor();
                  elanGlobalCacheAdaptor.ConnectToDevice(IPAddress.Parse("192.168.1.70"), 4998);
                  tuneSuiteGlobalCacheAdaptor = new GlobalCacheAdaptor();
                  tuneSuiteGlobalCacheAdaptor.ConnectToDevice(IPAddress.Parse("192.168.1.70"), 5000);
                  lutronGlobalCacheAdaptor = new GlobalCacheAdaptor();
                  lutronGlobalCacheAdaptor.ConnectToDevice(IPAddress.Parse("192.168.1.71"), 4999);

                  elanTextBox.Text = elanGlobalCacheAdaptor._line;
                  tuneSuiteTextBox.Text = tuneSuiteGlobalCacheAdaptor._line;
                  lutronTextBox.Text = lutronGlobalCacheAdaptor._line;
           }

           private void btnZoneOnOff_Click(object sender, EventArgs e)    {    elanGlobalCacheAdaptor.SendMessage("sendir,4:3,1,40000,4,1,21,181,21,181,21,181,21,181,21,181,21,181,21,181,21,181,21,181,21,181,21,181,21,800" + Environment.NewLine); }
           private void btnSourceInput1_Click(object sender, EventArgs e) {    elanGlobalCacheAdaptor.SendMessage("sendir,4:3,1,40000,1,1,20,179,20,179,20,179,20,179,20,179,20,179,20,179,20,278,20,179,20,179,20,179,20,780" + Environment.NewLine); }        
           private void btnSystemOff_Click(object sender, EventArgs e)    {    elanGlobalCacheAdaptor.SendMessage("sendir,4:3,1,40000,1,1,20,184,20,184,20,184,20,184,20,184,20,286,20,286,20,286,20,184,20,184,20,184,20,820" + Environment.NewLine); }

           private void btnLightOff_Click(object sender, EventArgs e)     {    lutronGlobalCacheAdaptor.SendMessage("sdl,14,0,0,S2\x0d"); }
           private void btnLightOn_Click(object sender, EventArgs e)      {    lutronGlobalCacheAdaptor.SendMessage("sdl,14,100,0,S2\x0d"); }

           private void btnChannel31_Click(object sender, EventArgs e)    {    tuneSuiteGlobalCacheAdaptor.SendMessage("\xB8\x4D\xB5\x33\x31\x00\x30\x21\xB8\x0D"); }
           private void btnChannel30_Click(object sender, EventArgs e)    {    tuneSuiteGlobalCacheAdaptor.SendMessage("\xB8\x4D\xB5\x33\x30\x00\x30\x21\xB8\x0D"); }
      }
}

public class GlobalCacheAdaptor
{
      public Socket _multicastListener;
      public string _preferredDeviceID;
      public IPAddress _deviceAddress;
      public Socket _deviceSocket;
      public StreamWriter _deviceWriter;
      public bool _isConnected;
      public int _port;
      public IPAddress _address;
      public string _line;

      public GlobalCacheAdaptor() { }
      public static readonly GlobalCacheAdaptor Instance = new GlobalCacheAdaptor();

      public bool IsListening { get { return _multicastListener != null; } }

      public GlobalCacheAdaptor ConnectToDevice(IPAddress address, int port)
      {
            if (_deviceSocket != null) _deviceSocket.Close();
            try
            {
                  _port = port;
                  _address = address;
                  _deviceSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                  _deviceSocket.Connect(new IPEndPoint(address, port)); ;
                  _deviceAddress = address;
                  var stream = new NetworkStream(_deviceSocket);
                  var reader = new StreamReader(stream);
                  var writer = new StreamWriter(stream) { NewLine = "\r", AutoFlush = true };
                  _deviceWriter = writer;
                  writer.WriteLine("getdevices");
                  var readerThread = new Thread(ReaderThreadProc) { IsBackground = true };
                  readerThread.Start(reader);
                  _isConnected = true;
                  return Instance;
           }
           catch { DisconnectFromDevice(); MessageBox.Show("ConnectToDevice Error."); throw; }
}
     public void SendMessage(string message)
     {
           try
           {
                 var stream = new NetworkStream(_deviceSocket);
                 var reader = new StreamReader(stream);
                 var writer = new StreamWriter(stream) { NewLine = "\r", AutoFlush = true };
                 _deviceWriter = writer;
                 writer.WriteLine(message);
                 var readerThread = new Thread(ReaderThreadProc) { IsBackground = true };
                 readerThread.Start(reader);
           }
           catch { MessageBox.Show("SendMessage() Error."); }
    }

    public void DisconnectFromDevice()
    {
         if (_deviceSocket != null)
         {
               try { _deviceSocket.Close(); _isConnected = false; }
               catch { MessageBox.Show("DisconnectFromDevice Error."); }
               _deviceSocket = null;
         }
        _deviceWriter = null;
        _deviceAddress = null;
    }

    **private void ReaderThreadProc(object state)**
    {
          var reader = (StreamReader)state;
          try
          {
              while (true)
              {
                 var line = reader.ReadLine();
                 if (line == null) break;
                 _line = _line + line + Environment.NewLine;
              }
             **// Feedback from each piece of equipment is visible here.
               // Need to create EventHandler to notify the TextBoxes to update with _line**
           }
           catch { MessageBox.Show("ReaderThreadProc Error."); }
    }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:46:08+00:00Added an answer on June 7, 2026 at 5:46 pm

    From my understanding of the question, you want to do something like this?

    enter image description here

    You need to know when a GlobalCacheAdapter updates and which one updated in order to update textboxes on a form. My question to you is this – do you actually need to know which updated?

    If you declare in your class an event handler like this:

    public class GlobalCacheAdaptor
    {
        public event EventHandler<EventArgs> Updated;
    
        protected virtual void OnUpdated()
        {
            var handler = Updated;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    
        private void Foo()
        {
            // When an update is received, raise Updated event
            OnUpdated();
        }
    }
    

    Then in your form subscribe to Updated for all three GlobalCacheHandler instances

    public Form1()
    {
        elanGlobalCacheAdaptor.Updated += (s,e) => 
        {
            elanTextBox.Text = elanGlobalCacheAdaptor._line;
        }
    
        tuneSuiteGlobalCacheAdaptor.Updated += (s,e) => 
        {
            tuneSuiteTextBox.Text = tuneSuiteGlobalCacheAdaptor._line;
        }
    
        lutronGlobalCacheAdaptor.Updated += (s,e) => 
        {
            lutronTextBox.Text = lutronGlobalCacheAdaptor._line;
        }
    }
    

    You should be able to update the correct text box when the appropriate cache handler raises the Updated event.

    Finally you may need to handle cross-thread interactions. if so, see this article on MSDN, particularly the part “Thread-Safe Calls to a Windows Forms Control”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greeting everyone, may I ask your help for following question? I'm using following code
May I ask for your help with the following please ? I am trying
I want to ask for help. I need to write code that remotely connect
May I kindly ask you for help. I must write a program in python
May is ask you about how to connect to Microsoft SQL server 2005 enterprise
May I ask someone to help me write this in LINQ, DataTable dt =
may i ask for help...i manage to add data to database..but i got problem
Why you may ask? Because i have built the app on mysql and need
May i ask you how can i calculate the period by deduct two time
This may be a stupid question to ask, but still I need to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.