예제 #1
0
        /// <summary>
        /// Sends the telecontrol command.
        /// </summary>
        public override void SendCommand(TeleCommand cmd)
        {
            base.SendCommand(cmd);

            if ((deviceModel.GetCmd(cmd.CmdCode) ?? deviceModel.GetCmd(cmd.CmdNum)) is ModbusCmd modbusCmd)
            {
                // prepare Modbus command
                if (modbusCmd.Multiple || modbusCmd.DataBlock == DataBlock.Custom)
                {
                    modbusCmd.Value = 0;

                    if (cmd.CmdData == null)
                    {
                        modbusCmd.SetCmdData(cmd.CmdVal);
                    }
                    else
                    {
                        modbusCmd.Data = cmd.CmdData;
                    }
                }
                else
                {
                    modbusCmd.Value = modbusCmd.DataBlock == DataBlock.HoldingRegisters
                        ? (ushort)cmd.CmdVal
                        : (ushort)(cmd.CmdVal > 0 ? 1 : 0);
                    modbusCmd.SetCmdData(cmd.CmdVal);
                }

                modbusCmd.InitReqPDU();
                modbusCmd.InitReqADU(deviceModel.Addr, transMode);

                // send command to device
                LastRequestOK = false;
                int tryNum = 0;

                while (RequestNeeded(ref tryNum))
                {
                    if (modbusPoll.DoRequest(modbusCmd))
                    {
                        LastRequestOK = true;
                    }

                    FinishRequest();
                    tryNum++;
                }
            }
            else
            {
                LastRequestOK = false;
                Log.WriteLine(CommPhrases.InvalidCommand);
            }

            FinishCommand();
        }