0

C# socket ir learn

I just received my iTach WF2IR and am playing around a bit in C#, currently I am focusing on writing some code to learn IR codes from my remotes. It seems to work, even though I'm no expert on socket programming. I did notice something I wasn't expecting, perhaps I'm approaching this in the wrong way. Let me explain.... I read in the documentation that the iTach sends learnt codes to the connection that sent the "get_IRL" command. So I assume that I need to make use of an asynchronous connection..

I have something like this on winform:

private Socket _client;

private void Connect_Click(object sender, EventArgs e)

{
            Socket newsock = new Socket(AddressFamily.InterNetwork,
                            SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.103"), 4998);
            newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
}

  void Connected(IAsyncResult iar)
        {
            _client = (Socket)iar.AsyncState;
            try
            {
                _client.EndConnect(iar);
                Console.WriteLine("Connected to", _client.RemoteEndPoint.ToString());
                
                _client.BeginReceive(data, 0, size, SocketFlags.None,new AsyncCallback(ReceiveData), _client);
            }
            catch (SocketException se)
            {
                Console.WriteLine("Error connecting: ", se.Message);
            }
        }

 

In the Connected delegate I call the BeginReceive method where the ReceiveData delegate is passed the the AsyncCallback...

The I send a "get_IRL" command to the iTach and the first response that I get in the ReceiveData method (euhm delegate) is IR Learner Enabled↵

The ReceiveData delegate looks like this

 void ReceiveData(IAsyncResult iar)
 {
            Socket remote = (Socket)iar.AsyncState;
            int recv = remote.EndReceive(iar);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);

            Console.WriteLine("Receive Data: " + stringData);

}

If I leave it like this then I will not receive any further messages from the iTach, I need to tell the socket to "BeginReceive" again, calling the same delage again.

void ReceiveData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int recv = remote.EndReceive(iar);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);

            Console.WriteLine("Receive Data: " + stringData);

            remote.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), remote);

}

When I now send IR commands from my remote to the iTach I receive them, but the funny thing is that it is always in 4 separate messages (packets?). Is this expected behaviour, or is the code I've written not 100% correct?

Now I parse the response I get from the iTach to see whether the message I'm receiving is some sort of response from a command or part of a "learning session". And since I noticed a learning session goes in 4 messages I also have a counter... Here's the complete code.. My question is the following, am I approaching this in the correct way, or should I do it differently?

 

 void ReceiveData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int recv = remote.EndReceive(iar);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);

            Console.WriteLine("Receive Data: " + stringData);

            remote.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), remote);

            if (String.Compare(stringData, "sendir") > 0)
            {
                _learningcounter = 1;
                _learnmessage = stringData;
            }
            else if (_learningcounter < 4)
            {
                _learnmessage = _learnmessage + stringData;
                _learningcounter++;
            }
            else
            {
                _learnmessage = "";
                _learningcounter = 0;
            }

            if (_learningcounter == 4)
            {
                Console.WriteLine("_learnmessage: " + _learnmessage);
            }

            remote.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), remote);

        }

 

 

2 comments

Please sign in to leave a comment.