public void Open()
        {
            int int32 = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Interaction.IIf(this.meMode == Rs232.Mode.Overlapped, (object)1073741824, (object)0)));

            if (this.miPort <= 0)
            {
                throw new ApplicationException("COM Port not defined, use Port property to set it before invoking InitPort");
            }
            try
            {
                this.mhRS = Rs232.CreateFile("COM" + this.miPort.ToString(), -1073741824, 0, 0, 3, int32, 0);
                if (this.mhRS == -1)
                {
                    throw new Rs232.CIOChannelException("Unable to open COM" + this.miPort.ToString());
                }
                int lpErrors = 0;
                int num      = Rs232.ClearCommError(this.mhRS, lpErrors, 0);
                num = Rs232.PurgeComm(this.mhRS, 12);
                Rs232.DCB lpDCB = new DCB();
                num = Rs232.GetCommState(this.mhRS, ref lpDCB);
                num = Rs232.BuildCommDCB(string.Format("baud={0} parity={1} data={2} stop={3}", (object)this.miBaudRate, (object)"NOEM".Substring((int)this.meParity, 1), (object)this.miDataBit, (object)this.meStopBit), ref lpDCB);
                if (Rs232.SetCommState(this.mhRS, ref lpDCB) == 0)
                {
                    throw new Rs232.CIOChannelException("Unable to set COM state0" + this.pErr2Text(Rs232.GetLastError()));
                }
                num = Rs232.SetupComm(this.mhRS, this.miBufferSize, this.miBufferSize);
                this.pSetTimeout();
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception InnerException = ex;
                throw new Rs232.CIOChannelException(InnerException.Message, InnerException);
            }
        }
 public void ClearInputBuffer()
 {
     if (this.mhRS == -1)
     {
         return;
     }
     Rs232.PurgeComm(this.mhRS, 8);
 }
 public void Write(byte[] Buffer)
 {
     if (this.mhRS == -1)
     {
         throw new ApplicationException("Please initialize and open port before using this method");
     }
     try
     {
         if (this.meMode == Rs232.Mode.Overlapped)
         {
             if (this.pHandleOverlappedWrite(Buffer))
             {
                 throw new ApplicationException("Error in overllapped write");
             }
         }
         else
         {
             Rs232.PurgeComm(this.mhRS, 12);
             int    mhRs    = this.mhRS;
             byte[] Buffer1 = Buffer;
             int    length  = Buffer.Length;
             int    num     = 0;
             // ISSUE: explicit reference operation
             // ISSUE: variable of a reference type
             int lpNumberOfBytesWritten  = @num;
             Rs232.OVERLAPPED overlapped = (Rs232.OVERLAPPED)((object)null ?? Activator.CreateInstance(typeof(Rs232.OVERLAPPED)));
             // ISSUE: explicit reference operation
             // ISSUE: variable of a reference type
             Rs232.OVERLAPPED lpOverlapped = @overlapped;
             if (Rs232.WriteFile(mhRs, Buffer1, length, ref lpNumberOfBytesWritten, ref lpOverlapped) == 0)
             {
                 throw new ApplicationException("Write Error - Bytes Written " + num.ToString() + " of " + Buffer.Length.ToString());
             }
         }
     }
     catch (Exception ex)
     {
         ProjectData.SetProjectError(ex);
         throw;
     }
 }
        private bool pHandleOverlappedWrite(byte[] Buffer)
        {
            this.muOverlappedW.hEvent = Rs232.CreateEvent(0, 1, 0, (string)null);
            if (this.muOverlappedW.hEvent == 0)
            {
                throw new ApplicationException("Error creating event for overlapped write.");
            }
            Rs232.PurgeComm(this.mhRS, 12);
            this.mbWaitOnRead = true;
            int  num  = 0;
            bool flag = false;

            if (Rs232.WriteFile(this.mhRS, Buffer, Buffer.Length, ref num, ref this.muOverlappedW) == 0)
            {
                int lastError = Rs232.GetLastError();
                if (lastError != 997)
                {
                    throw new ArgumentException("Overlapped Read Error: " + this.pErr2Text(lastError));
                }
                if (Rs232.WaitForSingleObject(this.muOverlappedW.hEvent, -1) == 0)
                {
                    if (Rs232.GetOverlappedResult(this.mhRS, ref this.muOverlappedW, ref num, 0) == 0)
                    {
                        flag = true;
                    }
                    else
                    {
                        this.mbWaitOnRead = false;
                        //if (this.TxCompletedEvent != null)
                        //    this.TxCompletedEvent(this);
                    }
                }
            }
            else
            {
                flag = false;
            }
            Rs232.CloseHandle(this.muOverlappedW.hEvent);
            return(flag);
        }