コード例 #1
0
        void ThreadSendNormalCommands()
        {
            int nIndex       = 0;
            int nCommandIdx  = 0;
            int nNumcommands = 0;
            WriteToSerialPort        delegateWrite = _serialManager.Write;
            TransferFinishedDelegate delegateEndTx = this.EndNormalTransfer;
            bool bErrorOccurred = false;

            while (true)
            {
                nIndex = EventWaitHandle.WaitAny(_hadleNormalTransfer);

                switch (nIndex)
                {
                case (int)ENORMAL_COMMANDS.START_SEQUENCE:

                    // Disable commands
                    Invoke(new Action(() => buttonSend.Enabled  = false));
                    Invoke(new Action(() => buttonClear.Enabled = false));

                    // Initialize timer for transmission
                    _normalTransferAnswerTimer          = new System.Timers.Timer(90000);
                    _normalTransferAnswerTimer.Elapsed += new ElapsedEventHandler(OnTimedNormalEvent);
                    _normalTransferAnswerTimer.Stop();

                    // Save number of commands
                    nNumcommands = _commandNormalTransfer.Length;

                    // Current index command
                    nCommandIdx = 0;

                    // Used to skip null commands
                    while ((nCommandIdx < nNumcommands) && (String.IsNullOrEmpty(_commandNormalTransfer[nCommandIdx]) == true))
                    {
                        ++nCommandIdx;
                    }

                    if (nCommandIdx < nNumcommands)
                    {
                        // Start Timer
                        _normalTransferAnswerTimer.Enabled = true;
                        _normalTransferAnswerTimer.Start();

                        // Write command
                        this.Invoke(delegateWrite, _commandNormalTransfer[nCommandIdx]);
                    }
                    else
                    {
                        // Signal to end transfer
                        _hadleNormalTransfer[(int)ENORMAL_COMMANDS.END_TRANSFER].Set();
                    }
                    break;

                case (int)ENORMAL_COMMANDS.RESPONSE_RECEIVED:

                    string message;

                    // Get element from queue
                    while (_normalTransferResponseQueue.Count > 0)
                    {
                        message = (string)_normalTransferResponseQueue.Dequeue();

                        // Process response
                        bool bProceed = true;

                        _normalTransferAnswerTimer.Stop();

                        // Command right
                        if (String.Equals(message, "@OK;") == true)
                        {
                            // OK answer

                            // Save response
                            _responseToNormalCommands.Add(message);
                        }
                        // Command send was wrong
                        else if (String.Equals(message, "@KO;") == true)
                        {
                            MessageBox.Show("Error in answer by CNC", "Warning",
                                            MessageBoxButtons.OK, MessageBoxIcon.Warning);

                            bProceed = false;

                            bErrorOccurred = true;
                        }
                        // For others answers
                        else
                        {
                            // Save response
                            _responseToNormalCommands.Add(message);
                        }

                        if (bProceed == false)
                        {
                            // Signal to end transfer
                            _hadleNormalTransfer[(int)ENORMAL_COMMANDS.END_TRANSFER].Set();
                        }
                        else
                        {
                            // Increment command
                            ++nCommandIdx;

                            // Used to skip null commands
                            while ((nCommandIdx < nNumcommands) && (String.IsNullOrEmpty(_commandNormalTransfer[nCommandIdx]) == true))
                            {
                                ++nCommandIdx;
                            }

                            // Continue to transfer
                            if (nCommandIdx < nNumcommands)
                            {
                                // Start Timer
                                _normalTransferAnswerTimer.Enabled = true;
                                _normalTransferAnswerTimer.Start();

                                Thread.Sleep(20);

                                // Write command
                                this.Invoke(delegateWrite, _commandNormalTransfer[nCommandIdx]);
                            }
                            else
                            {
                                // Signal to end transfer
                                _hadleNormalTransfer[(int)ENORMAL_COMMANDS.END_TRANSFER].Set();
                            }
                        }
                    }
                    break;

                case (int)ENORMAL_COMMANDS.TIMER_ELAPSED_EVENT:

                    _normalTransferAnswerTimer.Stop();

                    MessageBox.Show("Timer elapsed waiting for answer", "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                    bErrorOccurred = true;

                    // Signal to end transfer
                    _hadleNormalTransfer[(int)ENORMAL_COMMANDS.END_TRANSFER].Set();
                    break;

                case (int)ENORMAL_COMMANDS.END_TRANSFER:

                    // Stop timer
                    _normalTransferAnswerTimer.Stop();
                    _normalTransferAnswerTimer.Enabled = false;
                    _normalTransferAnswerTimer.Dispose();

                    this.Invoke(delegateEndTx, !bErrorOccurred);
                    bErrorOccurred = false;
                    break;

                default:
                    // abort thread
                    break;
                }
            }
        }
コード例 #2
0
        private void Thread_SendBatchCommands()
        {
            int nIndex       = 0;
            int nCommandIdx  = 0;
            int nNumcommands = 0;
            WriteToSerialPort             delegateWrite           = _serialManager.Write;
            TransferFinishedDelegate      delegateEndBatch        = this.BatchEnd;
            BatchUpdateCommandsTxDelegate delegateUpdateCommandTx = this.SetCommandsTransferred;

            int[] Commands = new int[] { nCommandIdx, nNumcommands };

            while (true)
            {
                nIndex = EventWaitHandle.WaitAny(_hadleBatch);

                switch (nIndex)
                {
                case (int)EBATCH_EVENT.START_EVENT:

                    // Disable commands
                    buttonSend.Enabled  = false;
                    buttonClear.Enabled = false;

                    // Initialize timer for transmission
                    _batchAnswerTimer          = new System.Timers.Timer(200000);
                    _batchAnswerTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                    _batchAnswerTimer.Interval = 200000;
                    // Prevent garbage collector to remove timer in a long time preocess
                    GC.KeepAlive(_batchAnswerTimer);
                    _batchAnswerTimer.Stop();

                    // Save number of commands
                    nNumcommands = _commandBatch.Length;

                    // Current index command
                    nCommandIdx = 0;

                    // Used to skip null commands
                    while ((nCommandIdx < nNumcommands) && (String.IsNullOrEmpty(_commandBatch[nCommandIdx]) == true))
                    {
                        ++nCommandIdx;
                    }

                    if (nCommandIdx < nNumcommands)
                    {
                        // Start Timer
                        _batchAnswerTimer.Enabled = true;
                        _batchAnswerTimer.Start();

                        // Write command
                        this.Invoke(delegateWrite, _commandBatch[nCommandIdx]);
                    }
                    else
                    {
                        // Signal to end transfer
                        _hadleBatch[(int)EBATCH_EVENT.END_TRANSFER].Set();
                    }
                    break;

                case (int)EBATCH_EVENT.PROCEED_NEXT_COMMAND:

                    // Increment command
                    ++nCommandIdx;

                    // Used to skip null commands
                    while ((nCommandIdx < nNumcommands) && (String.IsNullOrEmpty(_commandBatch[nCommandIdx]) == true))
                    {
                        ++nCommandIdx;
                    }

                    if (nCommandIdx < nNumcommands)
                    {
                        Thread.Sleep(30);

                        // Start Timer
                        _batchAnswerTimer.Enabled = true;
                        _batchAnswerTimer.Start();

                        // Write command
                        this.Invoke(delegateWrite, _commandBatch[nCommandIdx]);
                    }
                    else
                    {
                        // Signal to end transfer
                        _hadleBatch[(int)EBATCH_EVENT.END_TRANSFER].Set();
                    }
                    break;

                case (int)EBATCH_EVENT.RESPONSE_RECEIVED_EVENT:

                    string message;

                    // Get element from queue
                    while (_batchResponseQueue.Count > 0)
                    {
                        message = (string)_batchResponseQueue.Dequeue();

                        // Process response

                        // Command right
                        if (String.Equals(message, "@OK;") == true)
                        {
                            // OK answer
                            Commands[0] = nCommandIdx + 1;
                            Commands[1] = nNumcommands;
                            this.Invoke(delegateUpdateCommandTx, Commands);
                        }
                        // Current point ended to process
                        else if (String.Equals(message, "@NEXT;") == true)
                        {
                            // Proceed with next command
                            _hadleBatch[(int)EBATCH_EVENT.PROCEED_NEXT_COMMAND].Set();
                        }
                        // Command send was wrong
                        else if (String.Equals(message, "@KO;") == true)
                        {
                            MessageBox.Show("Error in answer by CNC", "Warning",
                                            MessageBoxButtons.OK, MessageBoxIcon.Warning);

                            // Signal to end transfer
                            _hadleBatch[(int)EBATCH_EVENT.END_TRANSFER].Set();
                        }
                        // For others answers
                        else
                        {
                            MessageBox.Show("Error in answer by CNC", "Warning",
                                            MessageBoxButtons.OK, MessageBoxIcon.Warning);

                            // Signal to end transfer
                            _hadleBatch[(int)EBATCH_EVENT.END_TRANSFER].Set();
                        }
                    }

                    break;

                case (int)EBATCH_EVENT.TIMER_ELAPSED_EVENT:

                    MessageBox.Show("Timer elapsed waiting for answer", "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // Signal to end transfer
                    _hadleBatch[(int)EBATCH_EVENT.END_TRANSFER].Set();
                    break;

                case (int)EBATCH_EVENT.END_TRANSFER:

                    // Stop timer
                    _batchAnswerTimer.Stop();
                    _batchAnswerTimer.Enabled = false;
                    _batchAnswerTimer.Dispose();

                    this.Invoke(delegateEndBatch, true);
                    break;

                default:
                    // abort thread
                    break;
                }
            }
        }