Exemplo n.º 1
0
        public SerialReadWriteEvent serial_waitforevent(IntPtr handle, SerialReadWriteEvent rwevent, int timeout)
        {
            SerialReadWriteEvent result = nserial_waitforevent(handle, rwevent, timeout);

            errno = Marshal.GetLastWin32Error();
            return(result);
        }
Exemplo n.º 2
0
        public SerialReadWriteEvent serial_waitforevent(SafeSerialHandle handle, SerialReadWriteEvent rwevent, int timeout)
        {
            SerialReadWriteEvent result = UnsafeNativeMethods.serial_waitforevent(handle, rwevent, timeout);

            errno = Marshal.GetLastWin32Error();
            return(result);
        }
 internal static extern SerialReadWriteEvent serial_waitforevent(SafeSerialHandle handle, SerialReadWriteEvent rwevent, int timeout);
Exemplo n.º 4
0
        private unsafe void ReadWriteThread()
        {
            WaitHandle[] handles = new WaitHandle[] {
                m_StopRunning,
                m_Buffer.Serial.ReadBufferNotFull,
                m_Buffer.Serial.WriteBufferNotEmpty
            };
            m_Buffer.WriteEvent += SerialBufferWriteEvent;

            while (m_IsRunning) {
                SerialReadWriteEvent rwevent = SerialReadWriteEvent.NoEvent;

                int handle = WaitHandle.WaitAny(handles, -1);
                switch (handle) {
                case 0: // StopRunning - Should abort
                    m_IsRunning = false;
                    continue;
                }

                // These are not in the switch statement to ensure that we can actually
                // read/write simultaneously.
                if (m_Buffer.Serial.ReadBufferNotFull.WaitOne(0)) {
                    rwevent |= SerialReadWriteEvent.ReadEvent;
                }
                if (m_Buffer.Serial.WriteBufferNotEmpty.WaitOne(0)) {
                    rwevent |= SerialReadWriteEvent.WriteEvent;
                }

                SerialReadWriteEvent result = m_Dll.serial_waitforevent(m_Handle, rwevent, 500);
                if (result == SerialReadWriteEvent.Error) {
                    if (Log.SerialTrace(System.Diagnostics.TraceEventType.Error))
                        Log.Serial.TraceEvent(System.Diagnostics.TraceEventType.Error, 0,
                            "{0}: ReadWriteThread: Error waiting for event; errno={1}; description={2}",
                            m_Name, m_Dll.errno, m_Dll.serial_error(m_Handle));
                    m_IsRunning = false;
                    continue;
                } else if (Log.SerialTrace(System.Diagnostics.TraceEventType.Verbose)) {
                    Log.Serial.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0,
                        "{0}: ReadWriteThread: serial_waitforevent({1}, {2}) == {3}",
                        m_Name, m_HandlePtr, rwevent, result);
                }

                if ((result & SerialReadWriteEvent.ReadEvent) != 0) {
                    int rresult;
                    fixed (byte* b = m_Buffer.Serial.ReadBuffer.Array) {
                        byte* bo = b + m_Buffer.Serial.ReadBuffer.End;
                        int length = m_Buffer.Serial.ReadBuffer.WriteLength;
                        rresult = m_Dll.serial_read(m_Handle, (IntPtr)bo, length);
                        if (rresult < 0) {
                            if (Log.SerialTrace(System.Diagnostics.TraceEventType.Error))
                                Log.Serial.TraceEvent(System.Diagnostics.TraceEventType.Error, 0,
                                    "{0}: ReadWriteThread: Error reading data; errno={1}; description={2}",
                                    m_Name, m_Dll.errno, m_Dll.serial_error(m_Handle));
                            m_IsRunning = false;
                            continue;
                        } else {
                            if (Log.SerialTrace(System.Diagnostics.TraceEventType.Verbose))
                                Log.Serial.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0,
                                    "{0}: ReadWriteThread: serial_read({1}, {2}, {3}) == {4}",
                                    m_Name, m_HandlePtr, (IntPtr)bo, length, rresult);
                            if (rresult > 0) m_Buffer.Serial.ReadBufferProduce(rresult);
                        }
                    }
                    if (rresult > 0) OnDataReceived(this, new SerialDataReceivedEventArgs(SerialData.Chars));
                }

                if ((result & SerialReadWriteEvent.WriteEvent) != 0) {
                    int wresult;
                    fixed (byte * b = m_Buffer.Serial.WriteBuffer.Array) {
                        byte* bo = b + m_Buffer.Serial.WriteBuffer.Start;
                        int length = m_Buffer.Serial.WriteBuffer.ReadLength;
                        wresult = m_Dll.serial_write(m_Handle, (IntPtr)bo, length);
                        if (wresult < 0) {
                            if (Log.SerialTrace(System.Diagnostics.TraceEventType.Error))
                                Log.Serial.TraceEvent(System.Diagnostics.TraceEventType.Error, 0,
                                    "{0}: ReadWriteThread: Error writing data; errno={1}; description={2}",
                                    m_Name, m_Dll.errno, m_Dll.serial_error(m_Handle));
                            m_IsRunning = false;
                        } else {
                            if (Log.SerialTrace(System.Diagnostics.TraceEventType.Verbose))
                                Log.Serial.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0,
                                    "{0}: ReadWriteThread: serial_write({1}, {2}, {3}) == {4}",
                                    m_Name, m_HandlePtr, (IntPtr)bo, length, wresult);
                            if (wresult > 0) {
                                m_Buffer.Serial.WriteBufferConsume (wresult);
                                m_Buffer.Serial.TxEmptyEvent ();
                            }
                        }
                    }
                }
            }
            m_Buffer.WriteEvent -= SerialBufferWriteEvent;

            // Clear the write buffer. Anything that's still in the driver serial buffer will continue to write. The I/O was cancelled
            // so no need to purge the actual driver itself.
            m_Buffer.Serial.Purge();

            // We must notify the stream that any blocking waits should abort.
            m_Buffer.Serial.DeviceDead();
        }
Exemplo n.º 5
0
 private static extern SerialReadWriteEvent nserial_waitforevent(IntPtr handle, SerialReadWriteEvent rwevent, int timeout);