public byte[] ReadCoilStatus(byte slaveAddress, string startAddress, ushort nuMBErOfPoints)
        {
            var frame = ReadCoilStatusMessage(slaveAddress, startAddress, nuMBErOfPoints);

            SerialAdaper.Write(frame, 0, frame.Length);
            Thread.Sleep(DELAY);
            var buffReceiver = SerialAdaper.Read();

            if (buffReceiver.Length == 5)
            {
                ModbusExcetion(buffReceiver);
            }
            var data = new byte[buffReceiver.Length - 5];

            Array.Copy(buffReceiver, 3, data, 0, data.Length);
            return(Bit.ToByteArray(Bit.ToArray(data)));
        }
Exemple #2
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);
        }
Exemple #3
0
        public void GivenUnknownPortRef_AndBytes_AndOffset_AndCount_WhenWrite_ThenFalseReturned()
        {
            // 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.Write(
                new SerialPortAdapterRef(null, null),
                null,
                0,
                0);

            // Assert
            Assert.False(result);
        }