Beispiel #1
0
        /// <summary>
        /// Create & opens the com port and configures it with the required settings.
        /// </summary>
        /// <param name="cfg">Reference to port user config.</param>
        /// <returns>True if port opened successfully.</returns>
        protected bool Create(SerialCnfg cfg)
        {
            // If port already open, return.
            if(portOpen)
            {
                this.CommError("Com Port Already Open.");
                return false;
            }

            // Init members.
            this.fault = "";
            this.rxThread = null;
            this.writeEvent = new ManualResetEvent(false);
            this.recvrEvent = new ManualResetEvent(false);

            // Copy config to DCB.
            this.dcb = new Win32DCB(cfg);

            // Create handle to comm port.
            this.port = new Win32Com();
            if((this.portOpen = this.port.Open(cfg.PortName, true)) == false)
            {
                this.CommError(this.port.Fault);
                return false;
            }
            this.portName = cfg.PortName;

            // Instantiate support classes.
            this.stats  = new Win32Status();
            this.tmout  = new Win32Tmout();
            this.props  = new Win32Props();
            this.escape = new Win32Escape(port.Handle);

            // Set read/write timeouts.
            this.tmout.WriteConstant = cfg.TxTmoConst;
            this.tmout.WriteMultiplier = cfg.TxTmoMulti;
            if(this.tmout.Set(this.port.Handle) == false)
            {
                this.CommError(this.tmout.Fault);
                this.Destroy();
                return false;
            }

            // Overide OS default queue sizes.
            if((cfg.RxQueLen != 0) || (cfg.TxQueLen != 0))
            {
                if(this.props.Set(this.port.Handle, (uint)cfg.RxQueLen, (uint)cfg.TxQueLen) == false)
                {
                    this.Destroy();
                    this.CommError(this.props.Fault);
                    return false;
                }
            }

            // Get the current properties.
            if(this.props.Get(this.port.Handle) == false)
            {
                this.Destroy();
                this.CommError(this.props.Fault);
                return false;
            }

            // Set flow control limits.
            this.dcb.Limits(cfg, this.props.RxCurSize);

            // Update the port settings.
            if(this.dcb.Set(this.port.Handle) == false)
            {
                this.Destroy();
                this.CommError(this.dcb.Fault);
                return false;
            }

            // XON/OFF extended functionality.
            this.escape.XOFFavailable = true;

            // RTS extended functionality.
            if(cfg.RtsControl == PinState.Disable)
                this.escape.RTS = false;
            else if(cfg.RtsControl == PinState.Enable)
                this.escape.RTS = true;
            else if(cfg.RtsControl == PinState.Handshake)
                this.escape.RTSavailable = false;

            // DTR extended functionality.
            if(cfg.DtrControl == PinState.Disable)
                this.escape.DTR = false;
            else if(cfg.DtrControl == PinState.Enable)
                this.escape.DTR = true;
            else if(cfg.DtrControl == PinState.Toggle)
                this.escape.DTR = false;
            else if(cfg.DtrControl == PinState.Handshake)
                this.escape.DTRavailable = false;

            // Create TX overlap memory pointer.
            this.txOvr = new Win32Ovrlap(this.port.Handle, this.writeEvent.Handle);

            // Set the receiver mode.
            this.immediate = cfg.ReceiveMode;

            // Start the receiver thread.
            this.rxThread = new Thread(new ThreadStart(ReceiveThread));
            this.rxThread.Name = "COMReceiver";
            this.rxThread.Priority = ThreadPriority.AboveNormal;
            this.rxThread.Start();

            // Wait for receive thread to start.
            this.recvrEvent.WaitOne(500, false);

            // Port opened OK.
            return true;
        }
Beispiel #2
0
        /// <summary>
        /// Set the Xon/Xoff limits in the DCB.	NOTE: be very careful when
        /// overriding the default limits. Buffer overflow may result.
        /// </summary>
        /// <param name="cfg">Reference to user defined port config.</param>
        /// <param name="rxQueLen">Receiver queue length.</param>
        /// <returns></returns>
        internal void Limits(SerialCnfg cfg, uint rxQueLen)
        {
            // If the RX queue length is known (>0), default to 10% cushion.
            // If the queue size is unknown, set very low defaults for safety.
            if(cfg.XonLimit == 0)
                this.dcb.xonLim  = (short) ((rxQueLen > 0) ? (rxQueLen/10) : 8);
            else
                this.dcb.xonLim = cfg.XonLimit;

            if(cfg.XoffLimit == 0)
                this.dcb.xoffLim = (short) ((rxQueLen > 0) ? (rxQueLen/10) : 8);
            else
                this.dcb.xoffLim = cfg.XoffLimit;
            return;
        }
Beispiel #3
0
        /// <summary>
        /// Create the device control block (DCB) for the associated comm port.
        /// Sets the DCB fields to match the passed configuration.
        /// </summary>
        /// <param name="cfg">Reference to user defined port config.</param>
        internal Win32DCB(SerialCnfg cfg)
        {
            this.dcb = new DCB();

            this.dcb.dcbLength = Marshal.SizeOf(this.dcb);
            this.dcb.baudRate = (int) cfg.BaudRate;
            this.dcb.bitfield = 0x8001;

            if((cfg.Parity == Parity.Odd) ||
                (cfg.Parity == Parity.Even))
                this.dcb.bitfield |= 0x0002;
            if (cfg.TxFlowCTS)
                this.dcb.bitfield |= 0x0004;
            if (cfg.TxFlowDSR)
                this.dcb.bitfield |= 0x0008;
            this.dcb.bitfield |= ( ((int) cfg.DtrControl  &  0x0003) << 4);

            if (cfg.RxDSRsense)
                this.dcb.bitfield |= 0x0040;
            if (cfg.TxContinue)
                this.dcb.bitfield |= 0x0080;

            if (cfg.TxFlowXoff)
                this.dcb.bitfield |= 0x0100;
            if (cfg.RxFlowXoff)
                this.dcb.bitfield |= 0x0200;
            this.dcb.bitfield |= ( ((int) cfg.RtsControl  &  0x0003) << 12);

            this.dcb.byteSize = (byte) cfg.DataBits;
            this.dcb.prtyByte = (byte) cfg.Parity;
            this.dcb.stopBits = (byte) cfg.StopBits;
            this.dcb.xoffChar = (byte) cfg.XoffChar;
            this.dcb.xonChar  = (byte) cfg.XonChar;
            return;
        }
Beispiel #4
0
 /// <summary>
 /// Default constructor. Create port and setup delegates.
 /// </summary>
 /// <param name="ev">Event handler delegates.</param>
 public SerialPort(WithEvents ev)
 {
     this.ev = ev;
     this.index = 1;
     this.path = "";
     this.file = "Port";
     this.extn = ".cfg";
     this.SetName();
     this.cnfg = new SerialCnfg(this.index, this.name);
     return;
 }