Getting started on PC

The Getting Started guide shows you how to connect your IR module to your PC and how to record / transmit IR.

How to control IR module via PC

1. Setup the USB to TTL adapter.

2. Connect the TTL adapter and the IR Controller using the 4P cable and install the USB to TTL adapter driver on your PC.

3. If everything is set up properly, you should see the USB to TTL adapter in Device Manager like below

How to control

The IR Controller supports the standard UART interface, TTL level. It operates at 9600 baud, 8 data bits, no parity, and 1 stop bit (8N1).

Example code:


    private SerialPort serialPort1 = new SerialPort();
    
    .....
    private void InitComm()
        {
            try
            {
                this.serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                this.serialPort1.PortName = "COM4"; // Change this to your COM port no
                this.serialPort1.BaudRate = 9600;
                this.serialPort1.Parity = Parity.None;
                this.serialPort1.StopBits = StopBits.One;
                this.serialPort1.DataBits = 8;
                this.serialPort1.Encoding = Encoding.ASCII;
                this.serialPort1.Open();
            }
            catch (Exception err)
            {
                // Handle error
            }
        }

Now you are ready to start learning a new IR signal.

Learning IR

Learning a new IR signal is a two step process.

Step 1: Send start learning mode command

Step 2: Read the IR signal from IR Controller

Step 1

Send 224 (e0) to the IR controller via SerialPort to start the learning mode.


    // send 224 (e0 in hex)
    this.serialPort1.Write(new byte[] { 224 }, 0, 1);

When the device is ready to learn a new IR code, the buit-in indicator will turn blue. Now hold your remote close to the IR receiver LED and press the button you wish to record.




Step 2

Now you are ready to read the response from the IR controller.

Here is the complete C# code. Or GitHib


    class Program {
        const int START_LEARNING_MODE = 24;
        const int WAITING_IR_SIGNAL = 25;
        const int OK = 224; // Success response code from IR controller

        static int sCommandCode = 0;
        static SerialPort sSerialPort;

        static void Main(string[] args) {
            sSerialPort = new SerialPort("COM4");

            sSerialPort.BaudRate = 9600;
            sSerialPort.Parity = Parity.None;
            sSerialPort.StopBits = StopBits.One;
            sSerialPort.DataBits = 8;
            sSerialPort.Encoding = Encoding.ASCII;

            sSerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            sSerialPort.Open();

            Console.WriteLine("Start learning mode ...");
            sCommandCode = START_LEARNING_MODE;
            sSerialPort.Write(new byte[] { 224 }, 0, 1);
            Console.ReadKey();
        }

        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
            if (sCommandCode == START_LEARNING_MODE) {
                // Response for start learning mode command
                byte[] array = new byte[sSerialPort.BytesToRead];
                sSerialPort.Read(array, 0, sSerialPort.BytesToRead);

                int resvalue = array[0];
                if (resvalue == OK) {
                    Console.WriteLine("Ready to record the remote. Press any button now..");
                    sCommandCode = WAITING_IR_SIGNAL;
                }
                else {
                    Console.WriteLine("Errro");
                }
            }
            else if (sCommandCode == WAITING_IR_SIGNAL) {
                // Ir code 
                Thread.Sleep(1500); //  it can take time for the data to arrive
                int len = sSerialPort.BytesToRead;

                if (len > 0) {
                    Console.WriteLine("Got a valid IR signal....");
                    byte[] buffer = new byte[sSerialPort.BytesToRead];
                    sSerialPort.Read(buffer, 0, sSerialPort.BytesToRead);
                    string ir_signal = "";
                    int num = 0;
                    
                    for (int idx = 0; idx < len; idx++) {
                        ir_signal += buffer[idx];
                        // If not the last index, append "," to string
                        if (idx + 1 != len)  {
                            ir_signal += ",";
                        }
                        // Ignore the last digit in the array. It is the checksum
                        if (idx != len - 1) {
                            num += buffer[idx];
                        }
                    }

                    // received data checksum
                    byte received_checksum = (byte)num;
                    // ir signal checksum is the last byte
                    int ir_signal_checksum = buffer[len - 1];                                        
                    if (received_checksum == ir_signal_checksum)
                    {
                        Console.WriteLine("Your ir signal:");
                        Console.WriteLine(ir_signal);
                    }
                    else
                    {
                        Console.WriteLine("Invalid checksum:");
                    }
                }
            }
        }
    }

Here what console output looks like.



Sending IR

Now we are going to transmit the recorded IR code in the above step. The first byte should be 227 following the reset of the IR code. Here is the complete example in C#. Or you can get it on GitHib


    class Program
    {
        const int START_LEARNING_MODE = 26;
        const int ERROR = 255;

        static SerialPort sSerialPort;

        // Past the ir code from learn example here.
        const string ircode = "152,35....."; 

        static void Main(string[] args) {
            sSerialPort = new SerialPort("COM4");

            sSerialPort.BaudRate = 9600;
            sSerialPort.Parity = Parity.None;
            sSerialPort.StopBits = StopBits.One;
            sSerialPort.DataBits = 8;
            sSerialPort.Encoding = Encoding.ASCII;
            sSerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            sSerialPort.Open();

            Console.WriteLine("Sending IR Code ...");

            byte[] ir_array = ConvertStringArrayToByteArray(ircode);
            byte[] new_ir_array = new byte[ir_array.Length + 1]; //New Array and the size of a which is 4

            new_ir_array[0] = 227; // sending ir command.

            // copy ir code to this array starting from index 1
            for (int i = 1; i < ir_array.Length + 1; i++) {
                new_ir_array[i] = ir_array[i - 1];
            }
             
            sSerialPort.Write(new_ir_array, 0, new_ir_array.Length);
            Console.ReadKey();
        }

        private static byte[] ConvertStringArrayToByteArray(string str) {
            return str.Split(",".ToCharArray()).Select(x => byte.Parse(x.ToString())).ToArray();
        }

        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
            byte[] array = new byte[sSerialPort.BytesToRead];
            sSerialPort.Read(array, 0, sSerialPort.BytesToRead);

            int resvalue = array[0];

            if (resvalue == ERROR) {
                Console.WriteLine("Error sending ir !");
            }
            else {
                Console.WriteLine("Error sending ir !");
            }
        }
    }

Congratulations! Now you know how to learn and send IR codes with IRDevKit!