public static string CreateIcpDcomRequest(IcpCommand command, int addr, string name = "", int outputs = 0) { switch (command) { case IcpCommand.GetName: return($"${AddrToStr(addr)}M"); case IcpCommand.GetFirmware: return($"${AddrToStr(addr)}F"); case IcpCommand.SetName: return($"~{AddrToStr(addr)}O{name}"); case IcpCommand.SetOutputs: return($"@{AddrToStr(addr)}{outputs.ToString("X4")}"); case IcpCommand.GetInputState: return($"@{AddrToStr(addr)}"); case IcpCommand.GetModuleStatus: return($"~{AddrToStr(addr)}0"); default: return(string.Empty); } }
private bool SendCommand(IcpDeviceModel icpDevice, IcpCommand command, int addr) { string request = IcpDcom.CreateIcpDcomRequest(command, addr); string response = ComReader.SendCommand(request); bool result = IcpDcom.ParseIcpResponse(command, response, icpDevice); if (!result) { logger.Error($"Cant send {command} to {icpDevice.Name}, addr: {addr}, com: {ComReader.Port}"); } return(result); }
public static bool ParseIcpResponse(IcpCommand command, string response, IcpDeviceModel icpDevice, int outputs = 0) { if (string.IsNullOrEmpty(response)) { return(false); } if (response[0] == '?') { return(false); } switch (command) { case IcpCommand.GetName: icpDevice.Name = response.Substring(3); return(true); case IcpCommand.GetFirmware: icpDevice.Firmware = response.Substring(3); return(true); case IcpCommand.SetName: icpDevice.Name = response.Substring(3); return(true); case IcpCommand.SetOutputs: icpDevice.Outputs = outputs; return(true); case IcpCommand.GetInputState: return(ParseInputsState(response.Substring(1), icpDevice.Inputs)); case IcpCommand.GetModuleStatus: return(true); default: return(false); } }