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); }
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}"); } }
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); }
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; } }
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); }
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); }
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); }
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); }