public OMSSCADACommon.Responses.Response WriteSingleDigital(string id, CommandTypes command) { Console.WriteLine("WriteSingleDigital!"); Digital digital = null; OMSSCADACommon.Responses.Response response = new OMSSCADACommon.Responses.Response(); //while (!Database.IsConfigurationRunning) // Thread.Sleep(100); // getting PV from db ProcessVariable pv; if (dbContext.GetProcessVariableByName(id, out pv)) { digital = (Digital)pv; } // does this ID exist in the database if (digital == null) { response.ResultMessage = ResultMessage.INVALID_ID; return(response); } // is this a valid command for this digital device if (!CommandValidator.ValidateDigitalCommand(digital, command)) { response.ResultMessage = ResultMessage.INVALID_DIG_COMM; return(response); } RTU rtu; if ((rtu = dbContext.GetRTUByName(digital.ProcContrName)) != null) { IORequestBlock iorb = new IORequestBlock() { RequestType = RequestType.SEND, ProcessControllerName = digital.ProcContrName }; iorb.ReqAddress = (ushort)rtu.GetCommandAddress(digital); switch (rtu.Protocol) { case IndustryProtocols.ModbusTCP: ModbusHandler mdbHandler = new ModbusHandler { Header = new ModbusApplicationHeader() { TransactionId = 0, Length = 5, ProtocolId = (ushort)IndustryProtocols.ModbusTCP, DeviceAddress = rtu.Address }, Request = new WriteRequest() { FunCode = FunctionCodes.WriteSingleCoil, StartAddr = (ushort)rtu.GetCommandAddress(digital), Value = (ushort)command } }; iorb.SendBuff = mdbHandler.PackData(); iorb.SendMsgLength = iorb.SendBuff.Length; break; } IORequests.EnqueueRequest(iorb); Console.WriteLine("enqued {0}", BitConverter.ToString(iorb.SendBuff, 0, 12)); digital.Command = command; response.ResultMessage = ResultMessage.OK; } else { // rtu does not exist } return(response); }
/// <summary> /// Send Commands to simulator, to make its state consistent with RTDB /// </summary> public void InitializeSimulator() { List <ProcessVariable> pvs = dbContext.GetAllProcessVariables(); if (pvs.Count != 0) { foreach (ProcessVariable pv in pvs) { IORequestBlock iorb = new IORequestBlock() { RequestType = RequestType.SEND, ProcessControllerName = pv.ProcContrName }; RTU rtu; if ((rtu = dbContext.GetRTUByName(pv.ProcContrName)) != null) { iorb.ReqAddress = (ushort)rtu.GetCommandAddress(pv); bool shouldCommand = false; switch (rtu.Protocol) { case IndustryProtocols.ModbusTCP: ModbusHandler mdbHandler = new ModbusHandler(); mdbHandler.Header = new ModbusApplicationHeader() { TransactionId = 0, Length = 5, ProtocolId = (ushort)IndustryProtocols.ModbusTCP, DeviceAddress = rtu.Address }; mdbHandler.Request = new WriteRequest() { StartAddr = (ushort)rtu.GetCommandAddress(pv) }; switch (pv.Type) { // initialy, on simulator all digitals are set to 0 -> closed state case VariableTypes.DIGITAL: Digital digital = (Digital)pv; CommandTypes comm; if (shouldCommand = CommandValidator.InitialCommandingForDigital(digital, out comm)) { mdbHandler.Request.FunCode = FunctionCodes.WriteSingleCoil; ((WriteRequest)mdbHandler.Request).Value = (ushort)comm; } break; case VariableTypes.ANALOG: Analog analog = (Analog)pv; if (shouldCommand = AnalogProcessor.InitialWorkPointAnalog(analog)) { mdbHandler.Request.FunCode = FunctionCodes.WriteSingleRegister; ((WriteRequest)mdbHandler.Request).Value = (ushort)analog.RawCommValue; } analog.IsInit = true; break; case VariableTypes.COUNTER: Counter counter = (Counter)pv; break; } if (shouldCommand) { iorb.SendBuff = mdbHandler.PackData(); iorb.SendMsgLength = iorb.SendBuff.Length; if (pv.Name == "MEAS_AN_1") { Console.WriteLine(BitConverter.ToString(iorb.SendBuff, 0, 12)); } } break; } if (shouldCommand) { IORequests.EnqueueRequest(iorb); } } else { // izbrisati omdah te procesne varijable sa rtu-om tog imena? Console.WriteLine("Invalid config: ProcContrName = {0} does not exists.", pv.ProcContrName); continue; } } } }