예제 #1
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);
     }
 }
예제 #2
0
 extern internal static bool SetCommTimeouts(IntPtr hFile, [In] ref Win32Com.COMMTIMEOUTS lpCommTimeouts);
예제 #3
0
 extern internal static bool GetCommTimeouts(IntPtr hFile, out Win32Com.COMMTIMEOUTS lpCommTimeouts);
예제 #4
0
 extern internal static bool BuildCommDCBAndTimeouts(string lpDef, ref Win32Com.DCB lpDCB, ref Win32Com.COMMTIMEOUTS lpCommTimeouts);
예제 #5
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;
     }
 }