Example #1
0
        public bool SetSlaveMode()
        {
            DetectedSlaveAddress = 0; //redundant,only for notification

            _logger.Log("SetSlaveMode...");

            if (IsSlaveModeActive())
            {
                _logger.Log("Already in slave mode");
                return(true);
            }

            _logger.Log("RX: " + Convertor.ConvertByteArrayToHexString(_recievedPacket));

            if (!Crc16.CheckCrc(_recievedPacket))
            {
                return(false);
            }

            byte[] packetToSend = { _recievedPacket[0], (byte)(_recievedPacket[1] + 0x80), (byte)0x05 };
            Crc16.AddCrc(ref packetToSend);
            if (!SendPacket(packetToSend))
            {
                return(false);
            }

            _logger.Log("TX: " + Convertor.ConvertByteArrayToHexString(packetToSend));

            try
            {
                Thread.Sleep(2000);
                _logger.Log("TX: <ReadHoldingRegisters>");
                _modbusSerial.ReadHoldingRegisters(_recievedPacket[0], RegisterMappingOffsets.PasswordRegisterOffset, 1);
            }
            catch (Exception)
            {
                return(false);
            }
            _logger.Log("SetSlaveMode - OK");
            DetectedSlaveAddress = _recievedPacket[0];
            return(true);
        }