Пример #1
0
        protected QueueStatus GetQueueStatus()
        {
            Win32Com.COMSTAT  comstat;
            Win32Com.COMMPROP commprop;
            uint num;

            this.CheckOnline();
            if (!Win32Com.ClearCommError(this.hPort, out num, out comstat))
            {
                this.ThrowException("Unexpected failure");
            }
            if (!Win32Com.GetCommProperties(this.hPort, out commprop))
            {
                this.ThrowException("Unexpected failure");
            }
            return(new QueueStatus(comstat.Flags, comstat.cbInQue, comstat.cbOutQue, commprop.dwCurrentRxQueue, commprop.dwCurrentTxQueue));
        }
Пример #2
0
 public bool Open()
 {
     Win32Com.DCB          lpDCB          = new Win32Com.DCB();
     Win32Com.COMMTIMEOUTS lpCommTimeouts = new Win32Com.COMMTIMEOUTS();
     Win32Com.OVERLAPPED   structure      = new Win32Com.OVERLAPPED();
     if (!this.online)
     {
         CommBaseSettings settings = this.CommSettings();
         this.hPort = Win32Com.CreateFile(settings.port, 0xc0000000, 0, IntPtr.Zero, 3, 0x40000000, IntPtr.Zero);
         if (this.hPort == ((IntPtr)(-1)))
         {
             if (Marshal.GetLastWin32Error() == 5L)
             {
                 return(false);
             }
             this.hPort = Win32Com.CreateFile(this.AltName(settings.port), 0xc0000000, 0, IntPtr.Zero, 3, 0x40000000, IntPtr.Zero);
             if (this.hPort == ((IntPtr)(-1)))
             {
                 if (Marshal.GetLastWin32Error() != 5L)
                 {
                     throw new CommPortException("Port Open Failure");
                 }
                 return(false);
             }
         }
         this.online = true;
         lpCommTimeouts.ReadIntervalTimeout        = uint.MaxValue;
         lpCommTimeouts.ReadTotalTimeoutConstant   = 0;
         lpCommTimeouts.ReadTotalTimeoutMultiplier = 0;
         if (settings.sendTimeoutMultiplier == 0)
         {
             if (Environment.OSVersion.Platform == PlatformID.Win32NT)
             {
                 lpCommTimeouts.WriteTotalTimeoutMultiplier = 0;
             }
             else
             {
                 lpCommTimeouts.WriteTotalTimeoutMultiplier = 0x2710;
             }
         }
         else
         {
             lpCommTimeouts.WriteTotalTimeoutMultiplier = settings.sendTimeoutMultiplier;
         }
         lpCommTimeouts.WriteTotalTimeoutConstant = settings.sendTimeoutConstant;
         lpDCB.init((settings.parity == Parity.odd) || (settings.parity == Parity.even), settings.txFlowCTS, settings.txFlowDSR, (int)settings.useDTR, settings.rxGateDSR, !settings.txWhenRxXoff, settings.txFlowX, settings.rxFlowX, (int)settings.useRTS);
         lpDCB.BaudRate = settings.baudRate;
         lpDCB.ByteSize = (byte)settings.dataBits;
         lpDCB.Parity   = (byte)settings.parity;
         lpDCB.StopBits = (byte)settings.stopBits;
         lpDCB.XoffChar = (byte)settings.XoffChar;
         lpDCB.XonChar  = (byte)settings.XonChar;
         if (((settings.rxQueue != 0) || (settings.txQueue != 0)) && !Win32Com.SetupComm(this.hPort, (uint)settings.rxQueue, (uint)settings.txQueue))
         {
             this.ThrowException("Bad queue settings");
         }
         if ((settings.rxLowWater == 0) || (settings.rxHighWater == 0))
         {
             Win32Com.COMMPROP commprop;
             if (!Win32Com.GetCommProperties(this.hPort, out commprop))
             {
                 commprop.dwCurrentRxQueue = 0;
             }
             if (commprop.dwCurrentRxQueue > 0)
             {
                 lpDCB.XoffLim = lpDCB.XonLim = (short)(commprop.dwCurrentRxQueue / 10);
             }
             else
             {
                 lpDCB.XoffLim = (short)(lpDCB.XonLim = 8);
             }
         }
         else
         {
             lpDCB.XoffLim = (short)settings.rxHighWater;
             lpDCB.XonLim  = (short)settings.rxLowWater;
         }
         if (!Win32Com.SetCommState(this.hPort, ref lpDCB))
         {
             this.ThrowException("Bad com settings");
         }
         if (!Win32Com.SetCommTimeouts(this.hPort, ref lpCommTimeouts))
         {
             this.ThrowException("Bad timeout settings");
         }
         this.stateBRK = 0;
         if (settings.useDTR == HSOutput.none)
         {
             this.stateDTR = 0;
         }
         if (settings.useDTR == HSOutput.online)
         {
             this.stateDTR = 1;
         }
         if (settings.useRTS == HSOutput.none)
         {
             this.stateRTS = 0;
         }
         if (settings.useRTS == HSOutput.online)
         {
             this.stateRTS = 1;
         }
         this.checkSends      = settings.checkAllSends;
         structure.Offset     = 0;
         structure.OffsetHigh = 0;
         if (this.checkSends)
         {
             structure.hEvent = this.writeEvent.Handle;
         }
         else
         {
             structure.hEvent = IntPtr.Zero;
         }
         this.ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf(structure));
         Marshal.StructureToPtr(structure, this.ptrUWO, true);
         this.writeCount          = 0;
         this.empty[0]            = true;
         this.dataQueued          = false;
         this.rxException         = null;
         this.rxExceptionReported = false;
         this.rxThread            = new Thread(new ThreadStart(this.ReceiveThread));//打开串口的同时也打开了接收线程
         this.rxThread.Name       = "CommBaseRx";
         this.rxThread.Priority   = ThreadPriority.AboveNormal;
         this.rxThread.Start();
         this.startEvent.WaitOne(500, false);
         this.auto = false;
         if (this.AfterOpen())
         {
             this.auto = settings.autoReopen;
             return(true);
         }
         this.Close();
     }
     return(false);
 }