private static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            tempSerialReceiveBuff = new byte[serial.BytesToRead];

            serial.Read(tempSerialReceiveBuff, 0, tempSerialReceiveBuff.Length);
            if (!Busy)
            {
                // Just ignore read data that we didnt asked for
                return;
            }

            switch (uartReceiver.CollectData(tempSerialReceiveBuff))
            {
            case UARTResult.Done:
                taskState = 0;
                Running   = 0;
                Busy      = false;
                // All data collected, process it in a given success delegate if checksum matches
                ChecksumClass cs = new ChecksumClass();
                if (cs.VerifyChecksumFromReceivedMessage(uartReceiver.bData))
                {
                    // Checksum match
                    var dataWithoutChecksum = UARTHelperClass.RemoveChecksumFromMessage(uartReceiver.bData);
                    uartReceiver.Reset();
                    FormCustomConsole.WriteLineWithConsole("Finished from DataReceived method");
                    _successCallback(dataWithoutChecksum);
                }
                else
                {
                    if (_useRetry)
                    {
                        if (_retryCount >= _TOTAL_RETRY)
                        {
                            FormCustomConsole.WriteWithConsole("Aborting process (checksum doesnt match) at ");
                            FormCustomConsole.WriteLineWithConsole(DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond);
                            Busy = false;
                            _failCallback();
                        }
                        else
                        {
                            // Leave busy flag we are not finished yet
                            _retryCount++;
                            _sleepBeforeRetry = 1;
                            taskState         = 2;
                        }
                    }
                    else
                    {
                        // Checksum not valid
                        Busy = false;
                        _failCallback();
                    }
                }
                break;

            case UARTResult.WaitMoreData:
                Running = 1;
                break;

            case UARTResult.Error:
            default:
                uartReceiver.Reset();
                Running = 0;
                throw new Exception("Error with UARTResult in SerialDriver.serialPort_DataReceived");
            }
        }
        private static void TickTask()
        {
            switch (taskState)
            {
            case 0:     // IDLE
                //Console.WriteLine("Tick IDLE");
                break;

            case 1:
                //Console.WriteLine("Tick RUNNING " + Running.ToString());
                if (Running == 0)
                {
                    Console.WriteLine("Tick Finished");
                    taskState = 0;
                    return;
                }

                if (Busy == false)
                {
                    // This shouldnt happen
                    throw new Exception("Wrong busy flag in SerialDriver.TickTask");
                }


                if (Running++ >= MAX_TIMEOUTS)
                {
                    // Timeout, retry if possible or restart everything and call fail delegate
                    FormCustomConsole.WriteWithConsole("Tick timeout at ");
                    FormCustomConsole.WriteLineWithConsole(DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond);
                    uartReceiver.Reset();
                    taskState = 0;
                    if (_useRetry)
                    {
                        if (_retryCount >= _TOTAL_RETRY)
                        {
                            FormCustomConsole.WriteWithConsole("Aborting process at ");
                            FormCustomConsole.WriteLineWithConsole(DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond);
                            Busy = false;
                            _failCallback();
                        }
                        else
                        {
                            // Leave busy flag we are not finished yet
                            _retryCount++;
                            _sleepBeforeRetry = 1;
                            taskState         = 2;
                        }
                    }
                    else
                    {
                        Busy = false;
                        _failCallback();
                    }
                    return;
                }
                break;

            case 2:     // State for retry delay\
                //Console.WriteLine("Retry delay state " + _sleepBeforeRetry);
                if (_sleepBeforeRetry++ >= _TOTAL_SLEEP_CNT)
                {
                    if (serial.IsOpen)
                    {
                        FormCustomConsole.WriteWithConsole("Sending again at ");
                        FormCustomConsole.WriteLineWithConsole(DateTime.Now.Minute + ":" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond);
                        serial.Write(dataPointer, 0, dataPointer.Length);
                        taskState = 1;     // Put SM where it belongs
                        Running   = 1;
                    }
                    else
                    {
                        // Abort everything
                        System.Windows.Forms.MessageBox.Show("Port is not open!");
                        Busy = false;
                        _failCallback();
                        taskState = 0;
                    }
                }
                break;

            default:
                throw new Exception("Corrupted state in SerialDriver.TickTask");
            }
        }