Пример #1
0
        private void CheckResult()
        {
            uint nNumberOfBytesTransferred = 0U;

            if (writeCount <= 0)
            {
                return;
            }
            if (Win32Com.GetOverlappedResult(hPort, ptrUWO, out nNumberOfBytesTransferred, checkSends))
            {
                if (!checkSends)
                {
                    return;
                }
                writeCount -= (int)nNumberOfBytesTransferred;
                if (writeCount != 0)
                {
                    ThrowException("Send Timeout");
                }
                writeCount = 0;
            }
            else
            {
                if ((long)Marshal.GetLastWin32Error() == 996L)
                {
                    return;
                }
                ThrowException("Write Error");
            }
        }
Пример #2
0
 protected void SendImmediate(byte tosend)
 {
     CheckOnline();
     if (Win32Com.TransmitCommChar(hPort, tosend))
     {
         return;
     }
     ThrowException("Transmission failure");
 }
Пример #3
0
        protected CommBase.ModemStatus GetModemStatus()
        {
            CheckOnline();
            uint lpModemStat;

            if (!Win32Com.GetCommModemStatus(hPort, out lpModemStat))
            {
                ThrowException("Unexpected failure");
            }
            return(new CommBase.ModemStatus(lpModemStat));
        }
Пример #4
0
 private void InternalClose()
 {
     Win32Com.CancelIo(m_hPort);
     if (m_RxThread != null)
     {
         m_RxThread.Abort();
         m_RxThread.Join(100);
         m_RxThread = (Thread)null;
     }
     Win32Com.CloseHandle(m_hPort);
     if (m_ptrUWO != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(m_ptrUWO);
     }
 }
Пример #5
0
        protected CommBase.QueueStatus GetQueueStatus()
        {
            CheckOnline();
            uint lpErrors;

            Win32Com.COMSTAT cs;
            if (!Win32Com.ClearCommError(hPort, out lpErrors, out cs))
            {
                ThrowException("Unexpected failure");
            }
            Win32Com.COMMPROP cp;
            if (!Win32Com.GetCommProperties(hPort, out cp))
            {
                ThrowException("Unexpected failure");
            }
            return(new CommBase.QueueStatus(cs.Flags, cs.cbInQue, cs.cbOutQue, cp.dwCurrentRxQueue, cp.dwCurrentTxQueue));
        }
Пример #6
0
 private void InternalClose()
 {
     Win32Com.CancelIo(hPort);
     if (rxThread != null)
     {
         rxThread.Abort();
         rxThread.Join(100);
         rxThread = (Thread)null;
     }
     Win32Com.CloseHandle(hPort);
     if (ptrUWO != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(ptrUWO);
     }
     stateRTS = 2;
     stateDTR = 2;
     stateBRK = 2;
     online   = false;
 }
Пример #7
0
        public CommBase.PortStatus IsPortAvailable(string s)
        {
            IntPtr file = Win32Com.CreateFile(s, 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);

            if (file == (IntPtr)(-1))
            {
                if ((long)Marshal.GetLastWin32Error() == 5L)
                {
                    return(CommBase.PortStatus.unavailable);
                }
                file = Win32Com.CreateFile(AltName(s), 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
                if (file == (IntPtr)(-1))
                {
                    return((long)Marshal.GetLastWin32Error() == 5L ? CommBase.PortStatus.unavailable : CommBase.PortStatus.absent);
                }
            }
            Win32Com.CloseHandle(file);
            return(CommBase.PortStatus.available);
        }
Пример #8
0
        protected void Send(byte[] tosend)
        {
            uint lpNumberOfBytesWritten = 0U;

            CheckOnline();
            CheckResult();
            m_writeCount = tosend.GetLength(0);
            if (Win32Com.WriteFile(m_hPort, tosend, (uint)m_writeCount, out lpNumberOfBytesWritten, m_ptrUWO))
            {
                m_writeCount -= (int)lpNumberOfBytesWritten;
            }
            else
            {
                if ((long)Marshal.GetLastWin32Error() != 997L)
                {
                    ThrowException("Send failed");
                }
            }
        }
Пример #9
0
        public static PortStatus IsPortAvailable(int iComPort)
        {
            string comPort = "COM" + iComPort.ToString();
            IntPtr file    = Win32Com.CreateFile(comPort, 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);

            if (file == (IntPtr)(-1))
            {
                if ((long)Marshal.GetLastWin32Error() == 5L)
                {
                    return(PortStatus.Unavailable);
                }
                file = Win32Com.CreateFile(AltName(comPort), 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
                if (file == (IntPtr)(-1))
                {
                    return((long)Marshal.GetLastWin32Error() == 5L ? PortStatus.Unavailable : PortStatus.Absent);
                }
            }
            Win32Com.CloseHandle(file);
            return(PortStatus.Available);
        }
Пример #10
0
        private void ReceiveThread()
        {
            byte[]         lpBuffer       = new byte[1];
            bool           flag           = true;
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);

            Win32Com.OVERLAPPED overlapped = new Win32Com.OVERLAPPED();
            uint   num1 = 0U;
            IntPtr num2 = Marshal.AllocHGlobal(Marshal.SizeOf((object)overlapped));
            IntPtr num3 = Marshal.AllocHGlobal(Marshal.SizeOf((object)num1));

            overlapped.Offset     = 0U;
            overlapped.OffsetHigh = 0U;
            overlapped.hEvent     = autoResetEvent.Handle;
            Marshal.StructureToPtr((object)overlapped, num2, true);
            try
            {
                while (Win32Com.SetCommMask(hPort, 509U))
                {
                    Marshal.WriteInt32(num3, 0);
                    if (flag)
                    {
                        startEvent.Set();
                        flag = false;
                    }
                    if (!Win32Com.WaitCommEvent(hPort, num3, num2))
                    {
                        if ((long)Marshal.GetLastWin32Error() != 997L)
                        {
                            throw new CommPortException("IO Error [002]");
                        }
                        autoResetEvent.WaitOne();
                    }
                    uint num4 = (uint)Marshal.ReadInt32(num3);
                    if (((int)num4 & 128) != 0)
                    {
                        uint lpErrors;
                        if (!Win32Com.ClearCommError(hPort, out lpErrors, IntPtr.Zero))
                        {
                            throw new CommPortException("IO Error [003]");
                        }
                        int           num5          = 0;
                        StringBuilder stringBuilder = new StringBuilder("UART Error: ", 40);
                        if (((int)lpErrors & 8) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Framing,");
                            ++num5;
                        }
                        if (((int)lpErrors & 1024) != 0)
                        {
                            stringBuilder = stringBuilder.Append("IO,");
                            ++num5;
                        }
                        if (((int)lpErrors & 2) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Overrun,");
                            ++num5;
                        }
                        if (((int)lpErrors & 1) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Receive Cverflow,");
                            ++num5;
                        }
                        if (((int)lpErrors & 4) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Parity,");
                            ++num5;
                        }
                        if (((int)lpErrors & 256) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Transmit Overflow,");
                            ++num5;
                        }
                        if (num5 > 0)
                        {
                            stringBuilder.Length = stringBuilder.Length - 1;
                            throw new CommPortException(stringBuilder.ToString());
                        }
                        else
                        {
                            if ((int)lpErrors != 16)
                            {
                                throw new CommPortException("IO Error [003]");
                            }
                            num4 |= 64U;
                        }
                    }
                    if (((int)num4 & 1) != 0)
                    {
                        uint nNumberOfBytesRead;
                        do
                        {
                            nNumberOfBytesRead = 0U;
                            if (!Win32Com.ReadFile(hPort, lpBuffer, 1U, out nNumberOfBytesRead, num2))
                            {
                                Marshal.GetLastWin32Error();
                                throw new CommPortException("IO Error [004]");
                            }
                            else if ((int)nNumberOfBytesRead == 1)
                            {
                                OnRxChar(lpBuffer[0]);
                            }
                        }while (nNumberOfBytesRead > 0U);
                    }
                    if (((int)num4 & 4) != 0)
                    {
                        lock (empty)
                            empty[0] = true;
                        OnTxDone();
                    }
                    if (((int)num4 & 64) != 0)
                    {
                        OnBreak();
                    }
                    uint val = 0U;
                    if (((int)num4 & 8) != 0)
                    {
                        val |= 16U;
                    }
                    if (((int)num4 & 16) != 0)
                    {
                        val |= 32U;
                    }
                    if (((int)num4 & 32) != 0)
                    {
                        val |= 128U;
                    }
                    if (((int)num4 & 256) != 0)
                    {
                        val |= 64U;
                    }
                    if ((int)val != 0)
                    {
                        uint lpModemStat;
                        if (!Win32Com.GetCommModemStatus(hPort, out lpModemStat))
                        {
                            throw new CommPortException("IO Error [005]");
                        }
                        OnStatusChange(new CommBase.ModemStatus(val), new CommBase.ModemStatus(lpModemStat));
                    }
                }
                throw new CommPortException("IO Error [001]");
            }
            catch (Exception ex)
            {
                Win32Com.CancelIo(hPort);
                if (num3 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(num3);
                }
                if (num2 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(num2);
                }
                if (ex is ThreadAbortException)
                {
                    return;
                }
                rxException = ex;
                OnRxException(ex);
            }
        }
Пример #11
0
 public bool Open()
 {
     Win32Com.DCB          lpDCB          = new Win32Com.DCB();
     Win32Com.COMMTIMEOUTS lpCommTimeouts = new Win32Com.COMMTIMEOUTS();
     Win32Com.OVERLAPPED   overlapped     = new Win32Com.OVERLAPPED();
     if (online)
     {
         return(false);
     }
     CommBase.CommBaseSettings commBaseSettings = CommSettings();
     hPort = Win32Com.CreateFile(commBaseSettings.port, 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
     if (hPort == (IntPtr)(-1))
     {
         if ((long)Marshal.GetLastWin32Error() == 5L)
         {
             return(false);
         }
         hPort = Win32Com.CreateFile(AltName(commBaseSettings.port), 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
         if (hPort == (IntPtr)(-1))
         {
             if ((long)Marshal.GetLastWin32Error() == 5L)
             {
                 return(false);
             }
             else
             {
                 throw new CommPortException("Port Open Failure");
             }
         }
     }
     online = true;
     lpCommTimeouts.ReadIntervalTimeout         = uint.MaxValue;
     lpCommTimeouts.ReadTotalTimeoutConstant    = 0U;
     lpCommTimeouts.ReadTotalTimeoutMultiplier  = 0U;
     lpCommTimeouts.WriteTotalTimeoutMultiplier = (int)commBaseSettings.sendTimeoutMultiplier != 0 ? commBaseSettings.sendTimeoutMultiplier : (Environment.OSVersion.Platform != PlatformID.Win32NT ? 10000U : 0U);
     lpCommTimeouts.WriteTotalTimeoutConstant   = commBaseSettings.sendTimeoutConstant;
     lpDCB.init(commBaseSettings.parity == CommBase.Parity.odd || commBaseSettings.parity == CommBase.Parity.even, commBaseSettings.txFlowCTS, commBaseSettings.txFlowDSR, (int)commBaseSettings.useDTR, commBaseSettings.rxGateDSR, !commBaseSettings.txWhenRxXoff, commBaseSettings.txFlowX, commBaseSettings.rxFlowX, (int)commBaseSettings.useRTS);
     lpDCB.BaudRate = commBaseSettings.baudRate;
     lpDCB.ByteSize = (byte)commBaseSettings.dataBits;
     lpDCB.Parity   = (byte)commBaseSettings.parity;
     lpDCB.StopBits = (byte)commBaseSettings.stopBits;
     lpDCB.XoffChar = (byte)commBaseSettings.XoffChar;
     lpDCB.XonChar  = (byte)commBaseSettings.XonChar;
     if ((commBaseSettings.rxQueue != 0 || commBaseSettings.txQueue != 0) && !Win32Com.SetupComm(hPort, (uint)commBaseSettings.rxQueue, (uint)commBaseSettings.txQueue))
     {
         ThrowException("Bad queue settings");
     }
     if (commBaseSettings.rxLowWater == 0 || commBaseSettings.rxHighWater == 0)
     {
         Win32Com.COMMPROP cp;
         if (!Win32Com.GetCommProperties(hPort, out cp))
         {
             cp.dwCurrentRxQueue = 0U;
         }
         lpDCB.XoffLim = cp.dwCurrentRxQueue <= 0U ? (lpDCB.XonLim = (short)8) : (lpDCB.XonLim = (short)((int)cp.dwCurrentRxQueue / 10));
     }
     else
     {
         lpDCB.XoffLim = (short)commBaseSettings.rxHighWater;
         lpDCB.XonLim  = (short)commBaseSettings.rxLowWater;
     }
     if (!Win32Com.SetCommState(hPort, ref lpDCB))
     {
         ThrowException("Bad com settings");
     }
     if (!Win32Com.SetCommTimeouts(hPort, ref lpCommTimeouts))
     {
         ThrowException("Bad timeout settings");
     }
     stateBRK = 0;
     if (commBaseSettings.useDTR == CommBase.HSOutput.none)
     {
         stateDTR = 0;
     }
     if (commBaseSettings.useDTR == CommBase.HSOutput.online)
     {
         stateDTR = 1;
     }
     if (commBaseSettings.useRTS == CommBase.HSOutput.none)
     {
         stateRTS = 0;
     }
     if (commBaseSettings.useRTS == CommBase.HSOutput.online)
     {
         stateRTS = 1;
     }
     checkSends            = commBaseSettings.checkAllSends;
     overlapped.Offset     = 0U;
     overlapped.OffsetHigh = 0U;
     overlapped.hEvent     = !checkSends ? IntPtr.Zero : writeEvent.Handle;
     ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf((object)overlapped));
     Marshal.StructureToPtr((object)overlapped, ptrUWO, true);
     writeCount          = 0;
     empty[0]            = true;
     dataQueued          = false;
     rxException         = (Exception)null;
     rxExceptionReported = false;
     rxThread            = new Thread(new ThreadStart(ReceiveThread));
     rxThread.Name       = "CommBaseRx";
     rxThread.Priority   = ThreadPriority.AboveNormal;
     rxThread.Start();
     startEvent.WaitOne(500, false);
     auto = false;
     if (AfterOpen())
     {
         auto = commBaseSettings.autoReopen;
         return(true);
     }
     else
     {
         Close();
         return(false);
     }
 }
Пример #12
0
 internal static extern bool GetCommTimeouts(IntPtr hFile, out Win32Com.COMMTIMEOUTS lpCommTimeouts);
Пример #13
0
 internal static extern bool GetCommState(IntPtr hFile, ref Win32Com.DCB lpDCB);
Пример #14
0
 internal static extern bool GetCommProperties(IntPtr hFile, out Win32Com.COMMPROP cp);
Пример #15
0
 internal static extern bool ClearCommError(IntPtr hFile, out uint lpErrors, out Win32Com.COMSTAT cs);
Пример #16
0
 internal static extern bool BuildCommDCBAndTimeouts(string lpDef, ref Win32Com.DCB lpDCB, ref Win32Com.COMMTIMEOUTS lpCommTimeouts);