private static bool SendCommand(ComReader comReader, 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); }
private IcpDeviceModel GetFullInfo(int addr) { IcpDeviceModel icpDeviceModel = new IcpDeviceModel { Address = addr }; SendCommand(icpDeviceModel, IcpCommand.GetName, addr); SendCommand(icpDeviceModel, IcpCommand.GetFirmware, addr); SendCommand(icpDeviceModel, IcpCommand.GetInputState, addr); return(icpDeviceModel); }
public static async Task <List <IcpDeviceModel> > ScanIcpDevicesAsync(ComReader comReader) { await TaskEx.Run(() => { List <IcpDeviceModel> icpDevices = new List <IcpDeviceModel>(); for (int i = 1; i < 10; i++) { if (!IcpDcom.PingDevice(i, comReader)) { continue; } IcpDeviceModel devInfo = GetFullInfo(comReader, i); icpDevices.Add(devInfo); } return(icpDevices); }); return(null); }
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); } }
public async Task <List <IcpDeviceModel> > ScanIcpDevicesAsync() { IcpDeviceDict.Clear(); List <IcpDeviceModel> icpDevices = new List <IcpDeviceModel>(); await TaskEx.Run(() => { for (int i = 1; i < 10; i++) { if (!PingIcpDevice(i)) { continue; } IcpDeviceModel devInfo = GetFullInfo(i); IcpDeviceDict.Add(i, devInfo); icpDevices.Add(devInfo); } }); return(icpDevices); }