Animatroller.Framework.Expander.Renard.SendSerialData C# (CSharp) Method

SendSerialData() protected method

protected SendSerialData ( byte data ) : void
data byte
return void
        protected void SendSerialData(byte[] data)
        {
            if (data.Length > 600)
                throw new ArgumentOutOfRangeException("Max data size is 600 bytes");

            if (data.Length == 0)
                return;

            lock (lockObject)
            {
                sendCounter++;
                //log.Info("Sending packet {0} to Renard", sendCounter);

                try
                {
                    var outputBuffer = new byte[data.Length * 2 + 10];
                    int index = 0;

                    for (int i = 0; i < data.Length; i++)
                    {
                        if(i % 8 == 0)
                        {
                            if(sendBytes >= 100)
                            {
                                // Send PAD
                                outputBuffer[index++] = 0x7D;
                                sendBytes = 0;
                            }

                            // Send address
                            outputBuffer[index++] = 0x7E;
                            outputBuffer[index++] = (byte)(0x80 + (i / 8));
                            sendBytes += 2;
                        }

                        byte b = data[i];
                        switch(b)
                        {
                            case 0x7D:
                                outputBuffer[index++] = 0x7F;
                                outputBuffer[index++] = 0x2F;
                                sendBytes += 2;
                                break;

                            case 0x7E:
                                outputBuffer[index++] = 0x7F;
                                outputBuffer[index++] = 0x30;
                                sendBytes += 2;
                                break;

                            case 0x7F:
                                outputBuffer[index++] = 0x7F;
                                outputBuffer[index++] = 0x31;
                                sendBytes += 2;
                                break;

                            default:
                                outputBuffer[index++] = b;
                                sendBytes++;
                                break;
                        }
                    }

                    serialPort.Write(outputBuffer, 0, index);
                }
                catch (Exception ex)
                {
                    log.Info("SendSerialCommand exception: " + ex.Message);
                    // Ignore
                }
            }
        }