Ejemplo n.º 1
0
 private void InitializeDcb()
 {
     GWin32.DCB dcb = new GWin32.DCB();
     GetCommStateNative(ref dcb);
     //dcb.Binary[DcbFlagAbortOnError] = &= ~(1u << DcbFlagAbortOnError);
     dcb.AbortOnError = false;
     SetCommStateNative(ref dcb);
 }
Ejemplo n.º 2
0
        private void SetCommStateNative(ref GWin32.DCB lpDcb)
        {
            int commErrors = 0;

            GWin32.Comstat comStat = new GWin32.Comstat();

            for (int i = 0; i < CommStateRetries; i++)
            {
                if (!GWin32.ClearCommError(m_Handle, ref commErrors, ref comStat))
                {
                    WinIoError();
                }
                if (GWin32.SetCommState(m_Handle, ref lpDcb))
                {
                    break;
                }
                if (i == CommStateRetries - 1)
                {
                    WinIoError();
                }
            }
        }
Ejemplo n.º 3
0
        public void Open(string comPortName = "COM3", int baudRate = 128000)
        {
            if (m_hCommPort != null)
            {
                return;
            }
            SerialPort comm = new SerialPort();

            comm.BaudRate = baudRate;
            m_hCommPort   = GWin32.CreateFile(comPortName,
                                              FileAccess.Read | FileAccess.Write, //GENERIC_READ | GENERIC_WRITE,//access ( read and write)
                                              FileShare.None,                     //0,    //(share) 0:cannot share the COM port
                                              IntPtr.Zero,                        //0,    //security  (None)
                                              FileMode.Open,                      //OPEN_EXISTING,// creation : open_existing
                                              0x20000000 | 0x40000000,            //FILE_FLAG_OVERLAPPED,// we want overlapped operation
                                              IntPtr.Zero                         //0// no templates file for COM port...
                                              );

            if (m_hCommPort == null || m_hCommPort.IsInvalid)
            {
                m_hCommPort = null;
                int    err          = Marshal.GetLastWin32Error();
                string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                if (onErr != null)
                {
                    onErr.OnError("Open com failed " + errorMessage, true);
                }
                throwWinErr("Open com failed ");
            }

            const uint EV_RXCHAR = 1, EV_TXEMPTY = 4;

            if (!GWin32.SetCommMask(m_hCommPort, EV_RXCHAR | EV_TXEMPTY))
            {
                if (onErr != null)
                {
                    onErr.OnError("Failed to Set Comm Mask", true);
                }
                throwWinErr("Failed to Set Comm Mask");
            }

            GWin32.DCB dcb = new GWin32.DCB();
            dcb.DCBLength = (uint)Marshal.SizeOf(dcb);
            if (!GWin32.GetCommState(m_hCommPort, ref dcb))
            {
                if (onErr != null)
                {
                    onErr.OnError("CSerialCommHelper : Failed to Get Comm State", true);
                }
                throwWinErr("CSerialCommHelper : Failed to Get Comm State");
            }

            dcb.BaudRate       = (uint)comm.BaudRate;
            dcb.ByteSize       = (byte)comm.DataBits;
            dcb.Parity         = comm.Parity;
            dcb.StopBits       = comm.StopBits;
            dcb.DsrSensitivity = false;
            dcb.DtrControl     = GWin32.DtrControl.Enable;
            dcb.OutxDsrFlow    = false;
            dcb.OutxCtsFlow    = false;
            dcb.InX            = false;
            dcb.RtsControl     = GWin32.RtsControl.Disable;

            dcb.Binary = true;
            if (!GWin32.SetCommState(m_hCommPort, ref dcb))
            {
                if (onErr != null)
                {
                    onErr.OnError("CSerialCommHelper : Failed to Get Comm State", true);
                }
                throwWinErr("CSerialCommHelper : Failed to Set Comm State");
            }


            GWin32.COMMTIMEOUTS commTimeouts = new GWin32.COMMTIMEOUTS();
            commTimeouts.ReadIntervalTimeout         = 0;  // Never timeout, always wait for data.
            commTimeouts.ReadTotalTimeoutMultiplier  = 0;  // Do not allow big read timeout when big read buffer used
            commTimeouts.ReadTotalTimeoutConstant    = 0;  // Total read timeout (period of read loop)
            commTimeouts.WriteTotalTimeoutConstant   = 0;  // Const part of write timeout
            commTimeouts.WriteTotalTimeoutMultiplier = 0;  // Variable part of write timeout (per byte)
            GWin32.SetCommTimeouts(m_hCommPort, ref commTimeouts);
        }