public static string Display(this SerialStopBits sb)
 {
     return(sb switch {
         SerialStopBits.One => "1",
         SerialStopBits.OnePointFive => "1.5",
         SerialStopBits.Two => "2",
         _ => "1",
     });
示例#2
0
        public static SerialStopBitCount Convert(this SerialStopBits sb)
        {
            switch (sb)
            {
            case SerialStopBits.One: return(SerialStopBitCount.One);

            case SerialStopBits.OnePointFive: return(SerialStopBitCount.OnePointFive);

            case SerialStopBits.Two: return(SerialStopBitCount.Two);

            default: return(SerialStopBitCount.One);
            }
        }
示例#3
0
        // 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)
        {
            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;

            if ((portName == null || portName == "") && socket.SerialIndirector != null)
            {
                Interface = socket.SerialIndirector(socket, baudRate, (Socket.SocketInterfaces.SerialParity)parity, (Socket.SocketInterfaces.SerialStopBits)stopBits, dataBits, (Socket.SocketInterfaces.HardwareFlowControl)hardwareFlowControlRequirement, module);
            }

            else
            {
                Interface = new NativeSerial(socket, baudRate, (Socket.SocketInterfaces.SerialParity)parity, (Socket.SocketInterfaces.SerialStopBits)stopBits, dataBits, (Socket.SocketInterfaces.HardwareFlowControl)hardwareFlowControlRequirement, module, portName, hwFlowSupported);
            }

            Interface.NewLine      = "\n";
            Interface.Encoding     = System.Text.Encoding.UTF8;
            Interface.ReadTimeout  = InfiniteTimeout;
            Interface.WriteTimeout = InfiniteTimeout;
        }
        public ApplicationSettings(string configFileName)
        {
            string applicationPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            string configFile      = Path.Combine(applicationPath, configFileName);

            // Use defaults if configuration file is not readable
            if (!File.Exists(configFile))
            {
                return;
            }

            // Read configuration file settings
            XElement xmlConfig = XElement.Load(configFile);

            // Get serial port settings
            foreach (XElement serialPort in xmlConfig.Elements("SerialPort"))
            {
                SerialPortName = (string)serialPort.Element("PortName") ?? SerialPortName;
                SerialBaudRate = (int?)serialPort.Element("BaudRate") ?? SerialBaudRate;
                SerialDataBits = (int?)serialPort.Element("DataBits") ?? SerialDataBits;

                Enum.TryParse((string)serialPort.Element("Parity") ?? SerialParity.ToString(), true, out Parity parseParity);
                SerialParity = parseParity;

                Enum.TryParse((string)serialPort.Element("StopBits") ?? SerialStopBits.ToString(), true, out StopBits parseStopBits);
                SerialStopBits = parseStopBits;

                Enum.TryParse((string)serialPort.Element("FlowControl") ?? SerialFlowControl.ToString(), true, out Handshake parseFlowControl);
                SerialFlowControl = parseFlowControl;
            }

            // Get GPIB adapter settings
            foreach (XElement gpibAdapter in xmlConfig.Elements("GPIBAdapter"))
            {
                GPIBAdapterTimeout       = (int?)gpibAdapter.Element("Timeout") ?? GPIBAdapterTimeout;
                GPIBAdapterVersionString = (string)gpibAdapter.Element("VersionString") ?? GPIBAdapterVersionString;
            }
        }
示例#5
0
        /// <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;
        }
示例#6
0
        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 { }
        }
示例#7
0
        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 { }
        }
示例#8
0
        public SerialPort(SerialCommPort port, SerialDataWidth dataWidth, SerialStopBits stopBits,
		                  SerialParityBits parityBits, SerialHardwareFlowControl hwFlowControl,
		                  SerialSoftwareFlowControl swFlowControl, bool enableDTROnOpen, 
		                  bool enableRTSOnOpen)
            : this(port,dataWidth,stopBits,parityBits,hwFlowControl,swFlowControl)
        {
            m_EnableDTROnOpen = enableDTROnOpen;
            m_EnableRTSOnOpen = enableRTSOnOpen;
        }
示例#9
0
        public SerialPort(SerialCommPort port, SerialDataWidth dataWidth, SerialStopBits stopBits, 
		                  SerialParityBits parityBits, SerialHardwareFlowControl hwFlowControl, 
		                  SerialSoftwareFlowControl swFlowControl)
            : this(port,dataWidth,stopBits,parityBits)
        {
            m_HwFlowControl = hwFlowControl;
            m_SwFlowControl = swFlowControl;
        }
示例#10
0
        public SerialPort(SerialCommPort port, SerialDataWidth dataWidth, SerialStopBits stopBits, 
		                  SerialParityBits parityBits)
            : this(port)
        {
            m_DataWidth = dataWidth;
            m_StopBits = stopBits;
            m_ParityBits = parityBits;
        }
示例#11
0
        public Config()
        {
            switch (ConfigurationManager.AppSettings["BaudRate"])
            {
                case "115200": baudRate = SerialBaudRate.br115200;
                    break;
                case "9600": baudRate = SerialBaudRate.br009600;
                    break;
                case "14400": baudRate = SerialBaudRate.br014400;
                    break;
                case "19200": baudRate = SerialBaudRate.br019200;
                    break;
                case "38400": baudRate = SerialBaudRate.br038400;
                    break;
                case "57600": baudRate = SerialBaudRate.br057600;
                    break;
                case "128000": baudRate = SerialBaudRate.br128000;
                    break;
                case "7200": baudRate = SerialBaudRate.br007200;
                    break;
                case "4800": baudRate = SerialBaudRate.br004800;
                    break;
                case "2400": baudRate = SerialBaudRate.br002400;
                    break;
                case "1800": baudRate = SerialBaudRate.br001800;
                    break;
                case "1200": baudRate = SerialBaudRate.br001200;
                    break;
                case "600": baudRate = SerialBaudRate.br000600;
                    break;
                default: baudRate = SerialBaudRate.br000300;
                    break;
            }

            switch (Int32.Parse(ConfigurationManager.AppSettings["DataWidth"]))
            {
                case 8: dataWidth = SerialDataWidth.dw8Bits;
                    break;
                case 7: dataWidth = SerialDataWidth.dw7Bits;
                    break;
                case 6: dataWidth = SerialDataWidth.dw6Bits;
                    break;
                default:
                    dataWidth = SerialDataWidth.dw5Bits;
                    break;
            }

            switch (Int32.Parse(ConfigurationManager.AppSettings["StopBits"]))
            {
                case 1: stopBits = SerialStopBits.sb1Bit;
                    break;
                case 2: stopBits = SerialStopBits.sb2Bits;
                    break;
                default:
                    stopBits = SerialStopBits.sb1_5Bits;
                    break;

            }

            switch (ConfigurationManager.AppSettings["ParityBits"].ToUpper())
            {
                case "NONE": parityBits = SerialParityBits.pbNone;
                    break;
                case "EVEN": parityBits = SerialParityBits.pbEven;
                    break;
                case "ODD": parityBits = SerialParityBits.pbOdd;
                    break;
                case "SPACE": parityBits = SerialParityBits.pbSpace;
                    break;
                default: parityBits = SerialParityBits.pbMark;
                    break;
            }

            port = SerialPort.StringToSerialCommPort(ConfigurationManager.AppSettings["Port"].ToUpper());
        }
示例#12
0
        // 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;
        }
 public StopBitDisplayClass(SerialStopBits sb)
 {
     this.Display  = sb.Display();
     this.StopBits = sb;
 }
        private SerialDeviceInfo RetrieveAndValidate(
            IIndexItem <SerialIndexExtraInfo> index, string display, uint baud, ushort dataBits, SerialStopBits sbits, SerialParityType parity, SerialFlowControlHandshake hs)
        {
            SerialDeviceInfo p = this.RetrieveData(index);

            Assert.AreEqual(display, p.Display, "Param Data");
            Assert.AreEqual(baud, p.Baud, "Baud");
            Assert.AreEqual(dataBits, p.DataBits, "Data bits");
            Assert.AreEqual(sbits, p.StopBits, "Stop bits");
            Assert.AreEqual(parity, p.Parity, "Parity");
            Assert.AreEqual(hs, p.FlowHandshake, "Flow");
            // Extra info
            Assert.AreEqual(p.PortName, index.ExtraInfoObj.PortName, "Extra Info Port");
            Assert.AreEqual(p.USB_ProductIdDisplay, index.ExtraInfoObj.USBProduct, "Extra Info Product");
            Assert.AreEqual(p.USB_ProductId, index.ExtraInfoObj.USBProductId, "Extra Info Product id");
            Assert.AreEqual(p.USB_VendorIdDisplay, index.ExtraInfoObj.USBVendor, "Extra Info vendor");
            Assert.AreEqual(p.USB_VendorId, index.ExtraInfoObj.USBVendorId, "Extra Info vendor id");
            return(p);
        }