예제 #1
0
        public void GivenConnected_AndString_WhenFakeReceiveData_ThenCallbackInvoked()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            var expectedDataReceived = "Hello World!";
            var actualDataReceived   = string.Empty;
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
                actualDataReceived = data;
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);
            var port = (TestableSerialPort)sut.GetSerialPort(portRef);

            // Act
            port.FakeReceiveData(expectedDataReceived);

            // Assert
            Assert.Equal(expectedDataReceived, actualDataReceived);
        }
예제 #2
0
        public void Connection()
        {
            var stopwatch = Stopwatch.StartNew();

            try
            {
                IsConnected = SerialAdaper.Connect();

                stopwatch.Stop();
            }
            catch (TimeoutException ex)
            {
                stopwatch.Stop();

                EventscadaException?.Invoke(this.GetType().Name,
                                            $"Could Not Connect to Server : {ex.Message}Time{stopwatch.ElapsedTicks}");
            }
        }
예제 #3
0
        public void GivenConnectedPortRef_AndBytes_AndOffset_AndCount_WhenWrite_ThenBytesWrittenToPort_AndTrueReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);
            var port           = (TestableSerialPort)sut.GetSerialPort(portRef);
            var expectedData   = Encoding.ASCII.GetBytes("Hello World");
            int expectedOffset = 0;
            int expectedCount  = expectedData.Length;
            var actualData     = default(byte[]);
            var actualOffset   = 0;
            var actualCount    = 0;

            port.WriteCallback = delegate(byte[] data, int offset, int count)
            {
                actualData   = data;
                actualOffset = offset;
                actualCount  = count;
            };

            // Act
            var result = sut.Write(
                portRef,
                expectedData,
                expectedOffset,
                expectedCount);

            // Assert
            Assert.True(result);
            Assert.Equal(expectedData, actualData);
            Assert.Equal(expectedOffset, actualOffset);
            Assert.Equal(expectedCount, actualCount);
        }
예제 #4
0
        public void Connection()
        {
            var stopwatch = Stopwatch.StartNew();

            try
            {
                IsConnected = SerialAdaper.Connect();


                stopwatch.Stop();
            }
            catch (TimeoutException ex)
            {
                stopwatch.Stop();

                EventscadaException?.Invoke(this.GetType().Name, string.Format("Could Not Connect to Server : {0}Time{1}", this.GetType().Name, ex.Message,
                                                                               stopwatch.ElapsedTicks));

                IsConnected = false;
            }
        }
예제 #5
0
        public void GivenConnected_AndPortRef_WhenGetSerialPort_ThenCorrectSerialPortReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);

            // Act
            var port = sut.GetSerialPort(portRef);

            // Assert
            Assert.Equal(config.Port, port.PortName);
            Assert.Equal(config.BaudRate, port.BaudRate);
        }
예제 #6
0
        public void GivenConfig_AndCallback_WhenConnect_ThenPortCreated_AndPortOpened_AndRefReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut = new SerialPortAdapter <TestableSerialPort>();

            // Act
            var result = sut.Connect(
                config,
                callback);

            // Assert
            Assert.Equal(config, result.Config);
            Assert.Equal(callback, result.DataReceivedCallback);
            Assert.True(sut.GetSerialPort(result).IsOpen);
        }
예제 #7
0
        public void GivenConnectedPortRef_WhenDisconnect_ThenPortClosed_AndTrueReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);
            var port = sut.GetSerialPort(portRef);

            // Act
            var result = sut.Disconnect(portRef);

            // Assert
            Assert.True(result);
            Assert.False(port.IsOpen);
        }
예제 #8
0
        public void GivenConnected_WhenDispose_ThenAllPortsDisposed()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);
            var port = (TestableSerialPort)sut.GetSerialPort(portRef);

            // Act
            sut.Dispose();

            // Assert
            Assert.Empty(sut.PortRefs);
            Assert.True(port.Disposed);
        }