示例#1
0
        private void AsyncSendThread(List <String> msgs, UInt32 Nreps, Boolean bWait)
        {
            for (uint k = 0; k < Nreps; ++k)
            {
                foreach (String cmd in msgs)
                {
                    if (cmd.Trim() == "")
                    {
                        continue;
                    }

                    if (m_bAbortAsyncSend)
                    {
                        break;
                    }

                    Send(cmd.Trim());

                    if (bWait)
                    {
                        uint w = 0;

                        while (!IsCmdCompleted && !m_bAbortAsyncSend)
                        {
                            System.Threading.Thread.Sleep(100);

                            if (++w == 10)
                            {
                                ClearRxDebit();

                                NewRespData?.Invoke(this, "[[giving up on answer]]");
                                break;
                            }
                        }
                    }
                }

                if (m_bAbortAsyncSend)
                {
                    break;
                }
            }

            AsyncSendCompleted?.Invoke(this);

            return;
        }
示例#2
0
        private void Connectorthread()
        {
            StringBuilder rx            = new StringBuilder(10000);
            Boolean       bRxInProgress = false;

            while (!m_bDoDisconnect)
            {
                Byte rxbyte;

                try
                {
                    rxbyte = (Byte)m_serialPort.ReadByte();
                }
                catch (TimeoutException)
                {
                    rxbyte = 0;
                }
                catch
                {
                    ConnectionState = EnConnState.BROKEN;
                    ConnectionStateChange?.Invoke(this, null);
                    break;
                }

                if (rxbyte > 0)
                {
                    if (!bRxInProgress)
                    {
                        if ((rxbyte == (Byte)':') || (rxbyte == (Byte)'\'') || (rxbyte == (Byte)'#'))
                        {
                            bRxInProgress = true;
                            rx.Clear();
                            rx.Append((Char)rxbyte);
                        }
                    }
                    else
                    {
                        if (rxbyte != 0x03)
                        {
                            rx.Append((Char)rxbyte);
                        }
                        else
                        {
                            bRxInProgress = false;

                            String msg = rx.ToString();

                            if (msg[0] == ':')
                            {
                                NewRespData?.Invoke(this, msg);

                                System.Threading.Monitor.Enter(m_txmsgs);
                                if (m_rxdebit > 0)
                                {
                                    --m_rxdebit;
                                }
                                System.Threading.Monitor.Exit(m_txmsgs);
                            }
                            else if (msg[0] == '\'')
                            {
                                NewDisplayData?.Invoke(this, msg);
                            }
                            else if (msg[0] == '#')
                            {
                                NewDAQData?.Invoke(this, msg);
                            }
                            else
                            {
                                NewCorruptData?.Invoke(this, msg);
                            }
                        }
                    }
                }

                String txmsg = null;

                System.Threading.Monitor.Enter(m_txmsgs);
                if (m_txmsgs.Count > 0)
                {
                    txmsg = m_txmsgs.Dequeue();
                }
                System.Threading.Monitor.Exit(m_txmsgs);

                if (txmsg != null)
                {
                    NewRespData?.Invoke(this, txmsg);

                    Byte[] tmp = Encoding.ASCII.GetBytes(txmsg);
                    Byte[] tx  = new Byte[tmp.Length + 1];
                    tmp.CopyTo(tx, 0);
                    tx[tx.Length - 1] = 10;

                    m_serialPort.Write(tx, 0, tx.Length);
                }
            }

            if (m_serialPort.IsOpen)
            {
                m_serialPort.Close();
            }

            m_bConnected = false;

            ConnectionState = EnConnState.DISCONNECTED;
            ConnectionStateChange?.Invoke(this, null);

            return;
        }