示例#1
0
        /// <summary>
        /// Send packet. If sender is busy, than it waits 100ms before reporting send failure.
        /// </summary>
        /// <param name="in_packet">Packet to send</param>
        /// <param name="in_packet_length">Length of the packet in bytes</param>
        /// <returns>True if sending started, false if sending can't be started</returns>
        public bool SendPacket(byte[] in_packet, int in_packet_length)
        {
            int source_pos;

            if (m_serial_port == null)
            {
                return(false);
            }

            // accuire sender lock
            if (m_sender_lock.Wait(100))
            {
                // SLIP encode block
                m_transmit_data_length = 0;
                source_pos             = 0;
                SLIP.EncodeBlock(in_packet, ref source_pos, in_packet_length, m_transmit_buffer, ref m_transmit_data_length, SLIP.EncoderFlags.StorePacketStart | SLIP.EncoderFlags.StorePacketEnd);

                // Begin sending the data to the remote device.
                try
                {
                    m_serial_port.BaseStream.BeginWrite(m_transmit_buffer, 0, m_transmit_data_length, m_transmitter_callback, this);

                    return(true);
                }
                catch
                {
                    // stop thread because of the unexpected error
                    m_stop_requested = true;
                    m_thread_event.Set();
                }
            }

            return(false);
        }
示例#2
0
        /// <summary>
        /// Default constructor
        /// </summary>
        public UARTCommunicator()
        {
            m_thread_stopped        = new ManualResetEvent(false);
            m_sender_lock           = new SemaphoreSlim(1, 1);
            m_thread_event          = new AutoResetEvent(false);
            m_stop_requested        = false;
            m_thread                = null;
            m_receive_data_buffer   = new byte[ReceiveBufferSize];
            m_receive_packet_buffer = new byte[ReceiveBufferSize];
            m_received_data_length  = 0;
            m_transmit_buffer       = new byte[TransmitBufferSize];

            m_receiver_slip = new SLIP();

            m_receiver_callback    = new AsyncCallback(ReceiveCallback);
            m_transmitter_callback = new AsyncCallback(SendCallback);
        }