/// <summary> /// Default constructor. Create port and setup delegates. /// </summary> /// <param name="ev">Event handler delegates.</param> public SerialPort(WithEvents ev, int idx = 1) { this.ev = ev; this.path = ""; this.file = "Port"; this.extn = ".cfg"; this.index = idx; this.SetName(); this.cnfg = new SerialCnfg(this.index, this.name); return; }
public SerialPort(WithEvents ev, string port) { this.ev = ev; this.path = ""; this.file = "Port"; this.extn = ".cfg"; this.index = Convert.ToInt32(port.Substring(3, port.Length - 4), 10); this.SetName(); this.cnfg = new SerialCnfg(this.index, this.name); return; }
/// <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; }
/// <summary> /// Default constructor. Initializes the settings form. /// </summary> /// <param name="fileName">Name of file the receives changes.</param> /// <param name="cfg">Reference to current configuration.</param> public Settings(string fileName, SerialCnfg cfg) { // // Required for Windows Form Designer support // InitializeComponent(); // Set selected port. this.PortBox.Items.Add(cfg.PortName); this.PortBox.SelectedIndex = 0; // Load basic settings. this.EnumLoad(typeof(LineSpeed), this.BaudBox, cfg.BaudRate); this.EnumLoad(typeof(ByteSize), this.DataBox, cfg.DataBits); this.EnumLoad(typeof(StopBits), this.StopBox, cfg.StopBits); this.EnumLoad(typeof(Parity), this.ParityBox, cfg.Parity); this.EnumLoad(typeof(Handshake), this.HshakeBox, cfg.FlowCtrl); // Load ctrl/char boxes. this.LoadCtrl(this.XonBox, cfg.XonChar); this.LoadCtrl(this.XoffBox, cfg.XoffChar); this.LoadChar(this.EofBox, cfg.EofChar); this.LoadChar(this.EventBox, cfg.EvtChar); this.LoadChar(this.ErrorBox, cfg.ErrChar); // Set check boxes. this.ChkEvent.Checked = cfg.ReceiveMode ? true : false; this.ChkParity.Checked = cfg.ErrReplace ? true : false; this.ChkDiscard.Checked = cfg.NullDiscard ? true : false; Application.DoEvents(); this.Focus(); // Set local references. this.config = cfg; this.fileName = fileName; this.changed = false; }
/// <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; }
/// <summary> /// Default constructor. Create port and setup delegates. /// </summary> /// <param name="ev">Event handler delegates.</param> public SerialPort(WithEvents ev, int idx=1) { this.ev = ev; this.path = ""; this.file = "Port"; this.extn = ".cfg"; this.index = idx; this.SetName(); this.cnfg = new SerialCnfg(this.index, this.name); return; }
/// <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.Destroy(); this.CommError(this.tmout.Fault); 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; }
/// <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.Destroy(); this.CommError(this.tmout.Fault); 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); }
/// <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; }
/// <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; }