示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerialPort"/> class.
        /// </summary>
        /// <param name="connectionContext">
        /// The connection context.
        /// </param>
        /// <exception cref="ArgumentException">
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// </exception>
        public SerialPort(ExternalDataConfigurationConnectionContext connectionContext)
        {
            if (connectionContext.StopBits == StopBits.None)
                throw new ArgumentException("stopBits cannot be StopBits.None", "stopBits");

            if (connectionContext.DataBits < 5 || connectionContext.DataBits > 8)
                throw new ArgumentOutOfRangeException("The number of data bits must be 5 to 8 bits.", "byteSize");

            if ((connectionContext.DataBits == 5 && connectionContext.StopBits == StopBits.Two) || (connectionContext.StopBits == StopBits.OnePointFive && connectionContext.DataBits > 5))
                throw new ArgumentException("The use of 5 data bits with 2 stop bits is an invalid combination, " +
                    "as is 6, 7, or 8 data bits with 1.5 stop bits.");


            this.sPortName = connectionContext.PortName.Trim();
            this.iBaudRate = Convert.ToInt32(connectionContext.BaudRate);
            this.byteSize = Convert.ToByte(connectionContext.DataBits);

            this.stopBits = connectionContext.StopBits;
            this.parity = connectionContext.Parity;
        }
        public void ManagerPortListnerTest()
        {
            var dataVariableContextBool = new DataVariableContext("variableName", "VariableSystemName", new Guid(), DataVariablesType.Boolean);
            var dataVariableContextByte = new DataVariableContext("variableName", "VariableSystemName", new Guid(), DataVariablesType.Byte);
            var dataVariableContextByteArray = new DataVariableContext("variableName", "VariableSystemName", new Guid(), DataVariablesType.ByteArray);

            var dataVariableList = new List<DataVariableContext>
                                   {
                                       dataVariableContextBool,
                                       dataVariableContextByte,
                                       dataVariableContextByteArray
                                   };

            var computerConnectionContextCom1 = new ComputerConnectionContext(
                "192.168.1.100",
                ConnectionType.SerialPort,
                "Com1");

            var computerConnectionContextCom2 = new ComputerConnectionContext(
                "192.168.1.100",
                ConnectionType.SerialPort,
                "Com2");

            var computerConnectionList = new List<ComputerConnectionContext>() { computerConnectionContextCom1, computerConnectionContextCom2 };

            var ExternalDataConfigurationConnectionContext_1 = new ExternalDataConfigurationConnectionContext(
                "Name1",
                "systemName",
                "portName",
                "9600",
                SerialPortParity.None,
                true,
                75,
                false,
                FlowControl.HardWare,
                true,
                8,
                ConnectionType.SerialPort,
                StopBits.One,
                computerConnectionList,
                "1111",
                "2222",
                "23423452345",
                "");


            var ExternalDataConfigurationConnectionContext_2 = new ExternalDataConfigurationConnectionContext(
               "Name2",
               "systemName2",
               "portName2",
               "9600",
               SerialPortParity.None,
               true,
               75,
               false,
               FlowControl.HardWare,
               true,
               8,
               ConnectionType.SerialPort,
               StopBits.One,
               computerConnectionList,
               "1111",
               "2222",
               "23423452345",
               "");


            var externalDataConfigurationConnectionContextList = new List<ExternalDataConfigurationConnectionContext>()
                                                                 {
                                                                     ExternalDataConfigurationConnectionContext_1,
                                                                     ExternalDataConfigurationConnectionContext_2
                                                                 };

            var infoList = new List<ExternalDataDefinition>()
                           {
                                new ExternalDataDefinition("externalDataConnectionName", dataVariableList, externalDataConfigurationConnectionContextList,"")
                           };



            // Arrange
            Mock.SetupStatic(typeof(Utils));
            Mock.Arrange(() => Utils.CurrentUserClientIP).Returns("192.168.1.100");


            var logger = Mock.Create<ILogger>(Behavior.Loose);
            var logWasCalled = false;
            Mock.Arrange(() => logger.Log(LogSeverity.Error, typeof(FieldFileViewModel).FullName, Arg.IsAny<Exception>())).DoInstead(() => logWasCalled = true);
            Logger = logger;



            var port = Mock.Create<SerialPort>(Constructor.Mocked, Behavior.Loose);
            Mock.Arrange(() => port.Open()).IgnoreInstance().Returns(true);

            this.ManagerPortListner(infoList.ToArray());

            var inst = new PrivateAccessor(this);
            var serialPortList = inst.GetField("SerialPortList") as Dictionary<string, SerialPort>;
            
            Assert.IsNotNull(serialPortList);
            Assert.IsTrue(serialPortList != null && serialPortList.Count == 2);
        }