Beispiel #1
0
        public bool Open()
        {
            if (this.isOpen)
            {
                return(false);
            }
            bool   fullFramework = CommAPI.FullFramework;
            string fileName      = string.Format(@"\\.\{0}", this.portName);

            this.hPort = this.m_CommAPI.CreateFile(fileName);
            if (this.hPort == ((IntPtr)(-1)))
            {
                int num = Marshal.GetLastWin32Error();
                throw new CommPortException(string.Format("Error open port: {0}", (APIErrors)num));
            }
            this.m_CommAPI.PurgeComm(this.hPort, 0);
            this.m_CommAPI.GetCommState(this.hPort, this.dcb);
            this.dcb.BaudRate     = (uint)this.portSettings.BasicSettings.BaudRate;
            this.dcb.ByteSize     = this.portSettings.BasicSettings.ByteSize;
            this.dcb.fOutxCtsFlow = this.portSettings.OutCTS;
            this.dcb.fParity      = true;
            this.dcb.fBinary      = true;
            this.dcb.fRtsControl  = (byte)this.portSettings.RTSControl;
            this.dcb.Parity       = (byte)this.portSettings.BasicSettings.Parity;
            this.dcb.StopBits     = (byte)this.portSettings.BasicSettings.StopBits;
            this.brk = 0;
            this.dtr = (this.dcb.fDtrControl == 1) ? 1 : 0;
            this.rts = (this.dcb.fRtsControl == 1) ? 1 : 0;
            CommTimeouts timeouts = new CommTimeouts();

            timeouts.ReadIntervalTimeout         = uint.MaxValue;
            timeouts.ReadTotalTimeoutMultiplier  = 0;
            timeouts.ReadTotalTimeoutConstant    = 0x3e8;
            timeouts.WriteTotalTimeoutConstant   = 0;
            timeouts.WriteTotalTimeoutMultiplier = 0x4b00 / this.dcb.BaudRate;
            this.m_CommAPI.SetCommState(this.hPort, this.dcb);
            this.m_CommAPI.SetCommTimeouts(this.hPort, timeouts);
            this.m_CommAPI.SetupComm(this.hPort, 0x1000, 0x1000);
            this.m_CommAPI.SetupComm(this.hPort, this.rxBufferSize, this.txBufferSize);
            this.GetPortProperties();
            Thread.Sleep(100);
            this.isOpen      = true;
            this.eventThread = new Thread(new ThreadStart(this.CommEventThread));
            this.eventThread.IsBackground = true;
            this.eventThread.Priority     = ThreadPriority.Highest;
            this.eventThread.Start();
            this.threadStarted.WaitOne();
            return(true);
        }
Beispiel #2
0
 private static extern int CESetCommTimeouts(IntPtr hFile, CommTimeouts timeouts);
Beispiel #3
0
 internal override bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts)
 {
     return(Convert.ToBoolean(CESetCommTimeouts(hPort, timeouts)));
 }
Beispiel #4
0
        /// <summary>
        /// Open the current port
        /// </summary>
        /// <returns>true if successful, false if it fails</returns>
        public bool Open()
        {
            if (isOpen)
            {
                return(false);
            }

            if (CommAPI.FullFramework)
            {
                // set up the overlapped tx IO
                //				AutoResetEvent are = new AutoResetEvent(false);
                OVERLAPPED o = new OVERLAPPED();
                txOverlapped = LocalAlloc(0x40, Marshal.SizeOf(o));
                o.Offset     = 0;
                o.OffsetHigh = 0;
                o.hEvent     = IntPtr.Zero;
                Marshal.StructureToPtr(o, txOverlapped, true);
            }

            hPort = m_CommAPI.CreateFile(portName);

            if (hPort == (IntPtr)CommAPI.INVALID_HANDLE_VALUE)
            {
                int e = Marshal.GetLastWin32Error();

                if (e == (int)APIErrors.ERROR_ACCESS_DENIED)
                {
                    // port is unavailable
                    return(false);
                }

                // ClearCommError failed!
                string error = String.Format("CreateFile Failed: {0}", e);
                throw new CommPortException(error);
            }


            isOpen = true;

            // set queue sizes
            m_CommAPI.SetupComm(hPort, rxBufferSize, txBufferSize);

            // transfer the port settings to a DCB structure
            dcb.BaudRate          = (uint)portSettings.BasicSettings.BaudRate;
            dcb.ByteSize          = portSettings.BasicSettings.ByteSize;
            dcb.EofChar           = (sbyte)portSettings.EOFChar;
            dcb.ErrorChar         = (sbyte)portSettings.ErrorChar;
            dcb.EvtChar           = (sbyte)portSettings.EVTChar;
            dcb.fAbortOnError     = portSettings.AbortOnError;
            dcb.fBinary           = true;
            dcb.fDsrSensitivity   = portSettings.DSRSensitive;
            dcb.fDtrControl       = (DCB.DtrControlFlags)portSettings.DTRControl;
            dcb.fErrorChar        = portSettings.ReplaceErrorChar;
            dcb.fInX              = portSettings.InX;
            dcb.fNull             = portSettings.DiscardNulls;
            dcb.fOutX             = portSettings.OutX;
            dcb.fOutxCtsFlow      = portSettings.OutCTS;
            dcb.fOutxDsrFlow      = portSettings.OutDSR;
            dcb.fParity           = (portSettings.BasicSettings.Parity == Parity.none) ? false : true;
            dcb.fRtsControl       = (DCB.RtsControlFlags)portSettings.RTSControl;
            dcb.fTXContinueOnXoff = portSettings.TxContinueOnXOff;
            dcb.Parity            = (byte)portSettings.BasicSettings.Parity;

            dcb.StopBits = (byte)portSettings.BasicSettings.StopBits;
            dcb.XoffChar = (sbyte)portSettings.XoffChar;
            dcb.XonChar  = (sbyte)portSettings.XonChar;

            dcb.XonLim = dcb.XoffLim = (ushort)(rxBufferSize / 10);

            m_CommAPI.SetCommState(hPort, dcb);

            // store some state values
            brk = 0;
            dtr = dcb.fDtrControl == DCB.DtrControlFlags.Enable ? 1 : 0;
            rts = dcb.fRtsControl == DCB.RtsControlFlags.Enable ? 1 : 0;

            // set the Comm timeouts
            CommTimeouts ct = new CommTimeouts();

            // reading we'll return immediately
            // this doesn't seem to work as documented
            ct.ReadIntervalTimeout        = uint.MaxValue;      // this = 0xffffffff
            ct.ReadTotalTimeoutConstant   = 0;
            ct.ReadTotalTimeoutMultiplier = 0;

            // writing we'll give 5 seconds
            ct.WriteTotalTimeoutConstant   = 5000;
            ct.WriteTotalTimeoutMultiplier = 0;

            m_CommAPI.SetCommTimeouts(hPort, ct);

            // read the ports capabilities
            bool status = GetPortProperties();

            // start the receive thread
            eventThread          = new Thread(new ThreadStart(CommEventThread));
            eventThread.Priority = ThreadPriority.Highest;             //*** mod from release
            eventThread.Start();

            // wait for the thread to actually get spun up
            threadStarted.WaitOne();

            return(true);
        }
Beispiel #5
0
		private static extern int WinSetCommTimeouts(IntPtr hFile, CommTimeouts timeouts);
Beispiel #6
0
		override internal bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts) 
		{
			return Convert.ToBoolean(WinSetCommTimeouts(hPort, timeouts));
		}
Beispiel #7
0
		internal virtual bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts) {return false;}
Beispiel #8
0
 internal virtual bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts)
 {
     return(false);
 }
Beispiel #9
0
        /// <summary>
        /// Open the current port
        /// </summary>
        /// <returns>true if successful, false if it fails</returns>
        public bool Open()
        {
            if(isOpen) return false;

            if(CommAPI.FullFramework)
            {
                // set up the overlapped tx IO
                //				AutoResetEvent are = new AutoResetEvent(false);
                OVERLAPPED o = new OVERLAPPED();
                txOverlapped = LocalAlloc(0x40, Marshal.SizeOf(o));
                o.Offset = 0;
                o.OffsetHigh = 0;
                o.hEvent = IntPtr.Zero;
                Marshal.StructureToPtr(o, txOverlapped, true);
            }

            hPort = m_CommAPI.CreateFile(portName);

            if(hPort == (IntPtr)CommAPI.INVALID_HANDLE_VALUE)
            {
                int e = Marshal.GetLastWin32Error();

                if(e == (int)APIErrors.ERROR_ACCESS_DENIED)
                {
                    // port is unavailable
                    return false;
                }

                // ClearCommError failed!
                string error = String.Format("CreateFile Failed: {0}", e);
                throw new CommPortException(error);
            }

            isOpen = true;

            // set queue sizes
            m_CommAPI.SetupComm(hPort, rxBufferSize, txBufferSize);

            // transfer the port settings to a DCB structure
            dcb.BaudRate = (uint)portSettings.BasicSettings.BaudRate;
            dcb.ByteSize = portSettings.BasicSettings.ByteSize;
            dcb.EofChar = (sbyte)portSettings.EOFChar;
            dcb.ErrorChar = (sbyte)portSettings.ErrorChar;
            dcb.EvtChar = (sbyte)portSettings.EVTChar;
            dcb.fAbortOnError = portSettings.AbortOnError;
            dcb.fBinary = true;
            dcb.fDsrSensitivity = portSettings.DSRSensitive;
            dcb.fDtrControl = (DCB.DtrControlFlags)portSettings.DTRControl;
            dcb.fErrorChar = portSettings.ReplaceErrorChar;
            dcb.fInX = portSettings.InX;
            dcb.fNull = portSettings.DiscardNulls;
            dcb.fOutX = portSettings.OutX;
            dcb.fOutxCtsFlow = portSettings.OutCTS;
            dcb.fOutxDsrFlow = portSettings.OutDSR;
            dcb.fParity = (portSettings.BasicSettings.Parity == Parity.none) ? false : true;
            dcb.fRtsControl = (DCB.RtsControlFlags)portSettings.RTSControl;
            dcb.fTXContinueOnXoff = portSettings.TxContinueOnXOff;
            dcb.Parity = (byte)portSettings.BasicSettings.Parity;

            dcb.StopBits = (byte)portSettings.BasicSettings.StopBits;
            dcb.XoffChar = (sbyte)portSettings.XoffChar;
            dcb.XonChar = (sbyte)portSettings.XonChar;

            dcb.XonLim = dcb.XoffLim = (ushort)(rxBufferSize / 10);

            m_CommAPI.SetCommState(hPort, dcb);

            // store some state values
            brk = 0;
            dtr = dcb.fDtrControl == DCB.DtrControlFlags.Enable ? 1 : 0;
            rts = dcb.fRtsControl == DCB.RtsControlFlags.Enable ? 1 : 0;

            // set the Comm timeouts
            CommTimeouts ct = new CommTimeouts();

            // reading we'll return immediately
            // this doesn't seem to work as documented
            ct.ReadIntervalTimeout = uint.MaxValue; // this = 0xffffffff
            ct.ReadTotalTimeoutConstant = 0;
            ct.ReadTotalTimeoutMultiplier = 0;

            // writing we'll give 5 seconds
            ct.WriteTotalTimeoutConstant = 5000;
            ct.WriteTotalTimeoutMultiplier = 0;

            m_CommAPI.SetCommTimeouts(hPort, ct);

            // read the ports capabilities
            bool status=GetPortProperties();

            // start the receive thread
            eventThread = new Thread(new ThreadStart(CommEventThread));
            eventThread.Priority = ThreadPriority.Highest;
            eventThread.Start();

            // wait for the thread to actually get spun up
            threadStarted.WaitOne();

            return true;
        }
		private void UpdateTimeouts(){
			// set the Comm timeouts
			CommTimeouts ct = new CommTimeouts();

			// reading we'll return immediately
			// this doesn't seem to work as documented
			ct.ReadIntervalTimeout = 0xffffffff;
			if (this.mReadTimeout > 0){
				ct.ReadTotalTimeoutConstant = (uint)this.mReadTimeout;
				ct.ReadTotalTimeoutMultiplier = 0;
			}else{
				ct.ReadTotalTimeoutConstant = 0;
				ct.ReadTotalTimeoutMultiplier = 0;
			}

			uint writeTimeoutOrZero; 
			if (mWriteTimeout == InfiniteTimeout){
				writeTimeoutOrZero = 0;
			}else{
				writeTimeoutOrZero = (uint)mWriteTimeout; 
			}
			ct.WriteTotalTimeoutConstant = writeTimeoutOrZero;
			ct.WriteTotalTimeoutMultiplier = 0;

			m_CommAPI.SetCommTimeouts(hPort, ct);
		}
Beispiel #11
0
 public bool Open()
 {
     if (this.isOpen)
     {
         return false;
     }
     bool fullFramework = CommAPI.FullFramework;
     string fileName = string.Format(@"\\.\{0}", this.portName);
     this.hPort = this.m_CommAPI.CreateFile(fileName);
     if (this.hPort == ((IntPtr) (-1)))
     {
         int num = Marshal.GetLastWin32Error();
         throw new CommPortException(string.Format("Error open port: {0}", (APIErrors) num));
     }
     this.m_CommAPI.PurgeComm(this.hPort, 0);
     this.m_CommAPI.GetCommState(this.hPort, this.dcb);
     this.dcb.BaudRate = (uint) this.portSettings.BasicSettings.BaudRate;
     this.dcb.ByteSize = this.portSettings.BasicSettings.ByteSize;
     this.dcb.fOutxCtsFlow = this.portSettings.OutCTS;
     this.dcb.fParity = true;
     this.dcb.fBinary = true;
     this.dcb.fRtsControl = (byte) this.portSettings.RTSControl;
     this.dcb.Parity = (byte) this.portSettings.BasicSettings.Parity;
     this.dcb.StopBits = (byte) this.portSettings.BasicSettings.StopBits;
     this.brk = 0;
     this.dtr = (this.dcb.fDtrControl == 1) ? 1 : 0;
     this.rts = (this.dcb.fRtsControl == 1) ? 1 : 0;
     CommTimeouts timeouts = new CommTimeouts();
     timeouts.ReadIntervalTimeout = uint.MaxValue;
     timeouts.ReadTotalTimeoutMultiplier = 0;
     timeouts.ReadTotalTimeoutConstant = 0x3e8;
     timeouts.WriteTotalTimeoutConstant = 0;
     timeouts.WriteTotalTimeoutMultiplier = 0x4b00 / this.dcb.BaudRate;
     this.m_CommAPI.SetCommState(this.hPort, this.dcb);
     this.m_CommAPI.SetCommTimeouts(this.hPort, timeouts);
     this.m_CommAPI.SetupComm(this.hPort, 0x1000, 0x1000);
     this.m_CommAPI.SetupComm(this.hPort, this.rxBufferSize, this.txBufferSize);
     this.GetPortProperties();
     Thread.Sleep(100);
     this.isOpen = true;
     this.eventThread = new Thread(new ThreadStart(this.CommEventThread));
     this.eventThread.IsBackground = true;
     this.eventThread.Priority = ThreadPriority.Highest;
     this.eventThread.Start();
     this.threadStarted.WaitOne();
     return true;
 }
Beispiel #12
0
        /// <summary>
        /// Open the current port 
        /// </summary>
        /// <param name="isThreaded">Set to true to use threading. Otherwise poll</param>
        /// <returns></returns>
        public bool OpenThreaded(bool isThreaded)
        {
            this.isThreaded = isThreaded;

            if(isOpen) return false;

            if(CommAPI.FullFramework)
            {
                // set up the overlapped tx IO
                //				AutoResetEvent are = new AutoResetEvent(false);
                OVERLAPPED o = new OVERLAPPED();
                txOverlapped = LocalAlloc(0x40, Marshal.SizeOf(o));
                o.Offset = 0;
                o.OffsetHigh = 0;
                o.hEvent = IntPtr.Zero;
                Marshal.StructureToPtr(o, txOverlapped, true);
            }

            hPort = m_CommAPI.CreateFile(portName);

            if(hPort == (IntPtr)CommAPI.INVALID_HANDLE_VALUE)
            {
                int e = Marshal.GetLastWin32Error();

                if(e == (int)APIErrors.ERROR_ACCESS_DENIED)
                {
                    // port is unavailable
                    return false;
                }

                // ClearCommError failed!
                string error = String.Format("CreateFile Failed: {0}", e);
                throw new CommPortException(error);
            }

            isOpen = true;

            // set queue sizes
            m_CommAPI.SetupComm(hPort, rxBufferSize, txBufferSize);

            // transfer the port settings to a DCB structure
            dcb.BaudRate = (uint)portSettings.BasicSettings.BaudRate;
            dcb.ByteSize = portSettings.BasicSettings.ByteSize;
            dcb.EofChar = (sbyte)portSettings.EOFChar;
            dcb.ErrorChar = (sbyte)portSettings.ErrorChar;
            dcb.EvtChar = (sbyte)portSettings.EVTChar;
            dcb.fAbortOnError = portSettings.AbortOnError;
            dcb.fBinary = true;
            dcb.fDsrSensitivity = portSettings.DSRSensitive;
            dcb.fDtrControl = (DCB.DtrControlFlags)portSettings.DTRControl;
            dcb.fErrorChar = portSettings.ReplaceErrorChar;
            dcb.fInX = portSettings.InX;
            dcb.fNull = portSettings.DiscardNulls;
            dcb.fOutX = portSettings.OutX;
            dcb.fOutxCtsFlow = portSettings.OutCTS;
            dcb.fOutxDsrFlow = portSettings.OutDSR;
            dcb.fParity = (portSettings.BasicSettings.Parity == Parity.none) ? false : true;
            dcb.fRtsControl = (DCB.RtsControlFlags)portSettings.RTSControl;
            dcb.fTXContinueOnXoff = portSettings.TxContinueOnXOff;
            dcb.Parity = (byte)portSettings.BasicSettings.Parity;

            dcb.StopBits = (byte)portSettings.BasicSettings.StopBits;
            dcb.XoffChar = (sbyte)portSettings.XoffChar;
            dcb.XonChar = (sbyte)portSettings.XonChar;

            dcb.XonLim = dcb.XoffLim = (ushort)(rxBufferSize / 10);

            m_CommAPI.SetCommState(hPort, dcb);

            // store some state values
            brk = 0;
            dtr = dcb.fDtrControl == DCB.DtrControlFlags.Enable ? 1 : 0;
            rts = dcb.fRtsControl == DCB.RtsControlFlags.Enable ? 1 : 0;

            // set the Comm timeouts
            CommTimeouts ct = new CommTimeouts();

            /*
            If an application sets ReadIntervalTimeout and ReadTotalTimeoutMultiplier to MAXDWORD and sets ReadTotalTimeoutConstant to a value greater than zero and less than MAXDWORD, one of the following occurs when the ReadFile function is called:

              a.. If there are any characters in the input buffer, ReadFile returns immediately with the characters in the buffer.
              b.. If there are no characters in the input buffer, ReadFile waits until a character arrives and then returns immediately.
              c.. If no character arrives within the time specified by ReadTotalTimeoutConstant, ReadFile times out.
            */

            // reading we'll return immediately
            // this doesn't seem to work as documented
            #if PPC
            ct.ReadIntervalTimeout = uint.MaxValue; // On the Smartphone, MaxValue blocks, so set to 1 instead but this doesn't work either
            ct.ReadTotalTimeoutConstant = 0;
            ct.ReadTotalTimeoutMultiplier = 0;
            #else
            ct.ReadIntervalTimeout = uint.MaxValue; // On the PC definitely needs to be this, doesn't block. On PPC and SP, this blocks
            ct.ReadTotalTimeoutConstant = 0;
            ct.ReadTotalTimeoutMultiplier = 0;
            #endif

            // writing we'll give 5 seconds
            ct.WriteTotalTimeoutConstant = 5000;
            ct.WriteTotalTimeoutMultiplier = 0;

            m_CommAPI.SetCommTimeouts(hPort, ct);

            // read the ports capabilities
            bool status=GetPortProperties();

            if (isThreaded)
            {
                // start the receive thread
                eventThread = new Thread(new ThreadStart(CommEventThread));
                eventThread.Priority = ThreadPriority.AboveNormal; //SSI changed from highest
                eventThread.Start();

                // wait for the thread to actually get spun up
                threadStarted.WaitOne();
            }
            else
            {
                GrabDataSetup();
            }
            return true;
        }