/// <summary> /// Creates an instance of <see cref="Serial" /> for the given socket. /// </summary> /// <remarks>This automatically checks that the socket supports Type U, and reserves the pins. /// An exception will be thrown if there is a problem with these checks.</remarks> /// <param name="baudRate">The baud rate for the serial port.</param> /// <param name="parity">A value from the <see cref="SerialParity"/> enumeration that specifies /// the parity for the port.</param> /// <param name="stopBits">A value from the <see cref="SerialStopBits"/> enumeration that specifies /// the stop bits for the port.</param> /// <param name="dataBits">The number of data bits.</param> /// <param name="socket">The socket for this serial interface.</param> /// <param name="hardwareFlowControlRequirement">Specifies whether the module must use hardware flow control, will use hardware flow control if available, or does not use hardware flow control.</param> /// <param name="module">The module using this interface (which can be null if unspecified).</param> /// <returns>An instance of <see cref="Serial" /> for the given socket.</returns> public static Serial Create(Socket socket, int baudRate, SerialParity parity, SerialStopBits stopBits, int dataBits, HardwareFlowControl hardwareFlowControlRequirement, Module module) { bool hwFlowSupported = false; if (hardwareFlowControlRequirement == HardwareFlowControl.Required) socket.EnsureTypeIsSupported('K', module); else { hwFlowSupported = socket.SupportsType('K'); if (!hwFlowSupported) socket.EnsureTypeIsSupported('U', module); } socket.ReservePin(Socket.Pin.Four, module); socket.ReservePin(Socket.Pin.Five, module); if (hardwareFlowControlRequirement != HardwareFlowControl.NotRequired) { // must reserve hardware flow control pins even if not using them, since they are electrically connected. socket.ReservePin(Socket.Pin.Six, module); socket.ReservePin(Socket.Pin.Seven, module); } string portName = socket.SerialPortName; Serial instance; if ((portName == null || portName == "") && socket.SerialIndirector != null) instance = socket.SerialIndirector(socket, baudRate, (SerialParity)parity, (SerialStopBits)stopBits, dataBits, (HardwareFlowControl)hardwareFlowControlRequirement, module); else instance = new NativeSerial(socket, baudRate, (SerialParity)parity, (SerialStopBits)stopBits, dataBits, (HardwareFlowControl)hardwareFlowControlRequirement, module, portName, hwFlowSupported); instance.NewLine = "\n"; instance.ReadTimeout = System.Threading.Timeout.Infinite; instance.WriteTimeout = System.Threading.Timeout.Infinite; return instance; }
public NativeSerial(Socket socket, int baudRate, SerialParity parity, SerialStopBits stopBits, int dataBits, HardwareFlowControl hwFlowRequirement, Module module, string portName, bool hwFlowSupported) { if (portName == null || portName == "") { // this is a mainboard error that should not happen (we already check for it in SocketInterfaces.RegisterSocket) but just in case... throw Socket.InvalidSocketException.FunctionalityException(socket, "Serial"); } _port = new Hardware.SerialPort(portName, baudRate, (Hardware.Parity)parity, dataBits, (Hardware.StopBits)stopBits); if ((hwFlowRequirement != SocketInterfaces.HardwareFlowControl.NotRequired) && hwFlowSupported) { _port.Handshake = Hardware.Handshake.RequestToSend; _hardwareFlowControl = true; } try { this.ReadTimeout = System.Threading.Timeout.Infinite; this.WriteTimeout = System.Threading.Timeout.Infinite; } catch { } }
// Note: A constructor summary is auto-generated by the doc builder. /// <summary></summary> /// <remarks>This automatically checks that the socket supports Type U, and reserves the pins. /// An exception will be thrown if there is a problem with these checks.</remarks> /// <param name="baudRate">The baud rate for the serial port.</param> /// <param name="parity">A value from the <see cref="SerialParity"/> enumeration that specifies /// the parity for the port.</param> /// <param name="stopBits">A value from the <see cref="SerialStopBits"/> enumeration that specifies /// the stop bits for the port.</param> /// <param name="dataBits">The number of data bits.</param> /// <param name="socket">The socket for this serial interface.</param> /// <param name="hardwareFlowControlRequirement">Specifies whether the module must use hardware flow control, will use hardware flow control if available, or does not use hardware flow control.</param> /// <param name="module">The module using this interface (which can be null if unspecified).</param> public Serial(Socket socket, int baudRate, SerialParity parity, SerialStopBits stopBits, int dataBits, HardwareFlowControl hardwareFlowControlRequirement, Module module) { if (!socket.SupportsType('U')) { if (module != null) { throw new Socket.InvalidSocketException("Module " + module + " cannot use socket " + socket + " because it requires a socket supporting type 'K'" + (hardwareFlowControlRequirement == HardwareFlowControl.Required ? "" : " or type 'U'.")); } else { throw new Socket.InvalidSocketException("Cannot use socket " + socket + " because it does not support socket type 'K'" + (hardwareFlowControlRequirement == HardwareFlowControl.Required ? "" : " or type 'U'.")); } } bool socketSupportsHardwareFlowControl = socket.SupportsType('K'); if (hardwareFlowControlRequirement == HardwareFlowControl.Required && !socketSupportsHardwareFlowControl) { if (module != null) { throw new Socket.InvalidSocketException("Module " + module + " cannot use socket " + socket + " because it requires a socket supporting type 'K'."); } else { throw new Socket.InvalidSocketException("Cannot use socket " + socket + " because it does not support socket type 'K' and hardware flow control is required. Please relax the requirement for hardware flow control or use a socket supporting type 'K'."); } } string portName = socket.SerialPortName; if (portName == null || portName == "") { // this is a mainboard error that should not happen (we already check for it in SocketInterfaces.RegisterSocket) but just in case... throw new Socket.InvalidSocketException("Socket " + socket + " has an error with its Serial functionality. Please try a different socket."); } socket.ReservePin(Socket.Pin.Four, module); socket.ReservePin(Socket.Pin.Five, module); if (hardwareFlowControlRequirement != HardwareFlowControl.NotRequired) { // must reserve hardware flow control pins even if not using them, since they are electrically connected. socket.ReservePin(Socket.Pin.Six, module); socket.ReservePin(Socket.Pin.Seven, module); } this.LineReceivedEventDelimiter = "\n"; this.Encoding = System.Text.Encoding.UTF8; this._serialPort = new SerialPort(portName, baudRate, (System.IO.Ports.Parity)parity, dataBits, (System.IO.Ports.StopBits)stopBits); if ((hardwareFlowControlRequirement != HardwareFlowControl.NotRequired) && socketSupportsHardwareFlowControl) { this._serialPort.Handshake = Handshake.RequestToSend; this._hardwareFlowControl = true; } else { this._hardwareFlowControl = false; } this._serialPort.DataReceived += new SerialDataReceivedEventHandler(this._serialPort_DataReceived); this.ReadTimeout = InfiniteTimeout; this.WriteTimeout = InfiniteTimeout; }
// Note: A constructor summary is auto-generated by the doc builder. /// <summary></summary> /// <remarks>This automatically checks that the socket supports Type U, and reserves the pins. /// An exception will be thrown if there is a problem with these checks.</remarks> /// <param name="baudRate">The baud rate for the serial port.</param> /// <param name="parity">A value from the <see cref="SerialParity"/> enumeration that specifies /// the parity for the port.</param> /// <param name="stopBits">A value from the <see cref="SerialStopBits"/> enumeration that specifies /// the stop bits for the port.</param> /// <param name="dataBits">The number of data bits.</param> /// <param name="socket">The socket for this serial interface.</param> /// <param name="hardwareFlowControlRequirement">Specifies whether the module must use hardware flow control, will use hardware flow control if available, or does not use hardware flow control.</param> /// <param name="module">The module using this interface (which can be null if unspecified).</param> public Serial(Socket socket, int baudRate, SerialParity parity, SerialStopBits stopBits, int dataBits, HardwareFlowControl hardwareFlowControlRequirement, Module module) { if (!socket.SupportsType('U')) { if (module != null) { throw new Socket.InvalidSocketException("Module " + module + " cannot use socket " + socket + " because it requires a socket supporting type 'K'" + (hardwareFlowControlRequirement == HardwareFlowControl.Required ? "" : " or type 'U'.")); } else { throw new Socket.InvalidSocketException("Cannot use socket " + socket + " because it does not support socket type 'K'" + (hardwareFlowControlRequirement == HardwareFlowControl.Required ? "" : " or type 'U'.")); } } bool socketSupportsHardwareFlowControl = socket.SupportsType('K'); if (hardwareFlowControlRequirement == HardwareFlowControl.Required && !socketSupportsHardwareFlowControl) { if (module != null) { throw new Socket.InvalidSocketException("Module " + module + " cannot use socket " + socket + " because it requires a socket supporting type 'K'."); } else { throw new Socket.InvalidSocketException("Cannot use socket " + socket + " because it does not support socket type 'K' and hardware flow control is required. Please relax the requirement for hardware flow control or use a socket supporting type 'K'."); } } string portName = socket.SerialPortName; if (portName == null || portName == "") { // this is a mainboard error that should not happen (we already check for it in SocketInterfaces.RegisterSocket) but just in case... throw new Socket.InvalidSocketException("Socket " + socket + " has an error with its Serial functionality. Please try a different socket."); } socket.ReservePin(Socket.Pin.Four, module); socket.ReservePin(Socket.Pin.Five, module); if (hardwareFlowControlRequirement != HardwareFlowControl.NotRequired) { // must reserve hardware flow control pins even if not using them, since they are electrically connected. socket.ReservePin(Socket.Pin.Six, module); socket.ReservePin(Socket.Pin.Seven, module); } this.LineReceivedEventDelimiter = "\n"; this.Encoding = System.Text.Encoding.UTF8; this._serialPort = new SerialPort(portName, baudRate, (System.IO.Ports.Parity)parity, dataBits, (System.IO.Ports.StopBits)stopBits); if ((hardwareFlowControlRequirement != HardwareFlowControl.NotRequired) && socketSupportsHardwareFlowControl) { this._serialPort.Handshake = Handshake.RequestToSend; this._hardwareFlowControl = true; } else { this._hardwareFlowControl = false; } this._serialPort.DataReceived += new SerialDataReceivedEventHandler(this._serialPort_DataReceived); this.ReadTimeout = InfiniteTimeout; this.WriteTimeout = InfiniteTimeout; }
/// <summary> /// Creates an instance of <see cref="Serial" /> for the given socket. /// </summary> /// <remarks>This automatically checks that the socket supports Type U, and reserves the pins. /// An exception will be thrown if there is a problem with these checks.</remarks> /// <param name="baudRate">The baud rate for the serial port.</param> /// <param name="parity">A value from the <see cref="SerialParity"/> enumeration that specifies /// the parity for the port.</param> /// <param name="stopBits">A value from the <see cref="SerialStopBits"/> enumeration that specifies /// the stop bits for the port.</param> /// <param name="dataBits">The number of data bits.</param> /// <param name="socket">The socket for this serial interface.</param> /// <param name="hardwareFlowControlRequirement">Specifies whether the module must use hardware flow control, will use hardware flow control if available, or does not use hardware flow control.</param> /// <param name="module">The module using this interface (which can be null if unspecified).</param> /// <returns>An instance of <see cref="Serial" /> for the given socket.</returns> public static Serial Create(Socket socket, int baudRate, SerialParity parity, SerialStopBits stopBits, int dataBits, HardwareFlowControl hardwareFlowControlRequirement, Module module) { bool hwFlowSupported = false; if (hardwareFlowControlRequirement == HardwareFlowControl.Required) { socket.EnsureTypeIsSupported('K', module); } else { hwFlowSupported = socket.SupportsType('K'); if (!hwFlowSupported) { socket.EnsureTypeIsSupported('U', module); } } socket.ReservePin(Socket.Pin.Four, module); socket.ReservePin(Socket.Pin.Five, module); if (hardwareFlowControlRequirement != HardwareFlowControl.NotRequired) { // must reserve hardware flow control pins even if not using them, since they are electrically connected. socket.ReservePin(Socket.Pin.Six, module); socket.ReservePin(Socket.Pin.Seven, module); } string portName = socket.SerialPortName; Serial instance; if ((portName == null || portName == "") && socket.SerialIndirector != null) { instance = socket.SerialIndirector(socket, baudRate, (SerialParity)parity, (SerialStopBits)stopBits, dataBits, (HardwareFlowControl)hardwareFlowControlRequirement, module); } else { instance = new NativeSerial(socket, baudRate, (SerialParity)parity, (SerialStopBits)stopBits, dataBits, (HardwareFlowControl)hardwareFlowControlRequirement, module, portName, hwFlowSupported); } instance.NewLine = "\n"; instance.ReadTimeout = System.Threading.Timeout.Infinite; instance.WriteTimeout = System.Threading.Timeout.Infinite; return(instance); }
internal static extern ErrorCode SetFlowControl( IntPtr handle, SoftwareFlowControl softwareFlowControl, HardwareFlowControl hardwareFlowControl);