Esempio n. 1
0
        public void Open()
        {
            if (m_hCommPort != IntPtr.Zero)
            {
                return;
            }
            SerialPort comm = new SerialPort();

            comm.BaudRate = 128000;
            m_hCommPort   = GWin32.CreateFile("COM3",
                                              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 == IntPtr.Zero)
            {
                int    err          = Marshal.GetLastWin32Error();
                string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                throwWinErr("Open com failed ");
            }

            const uint EV_RXCHAR = 1, EV_TXEMPTY = 4;

            if (!GWin32.SetCommMask(m_hCommPort, EV_RXCHAR | EV_TXEMPTY))
            {
                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))
            {
                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))
            {
                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);
        }
Esempio n. 2
0
 protected void SetTimeout(uint tm = GWin32.INFINITE)
 {
     GWin32.COMMTIMEOUTS commTimeouts = new GWin32.COMMTIMEOUTS();
     commTimeouts.ReadIntervalTimeout = tm;
     GWin32.SetCommTimeouts(m_hCommPort, ref commTimeouts);
 }