public static bool SaveDevice(string filename, int index) { if (Path.GetExtension(filename) == ".ini") { return(true); } else { DevData data = new DevData(); data.devices = new List <device_t>(); data.devices.Add(devices[index]); return(Files.ExportXml(filename, data)); } }
public static bool SaveDevices(string filename) { if (Path.GetExtension(filename) == ".ini") { /*IniFile ini; * ini = new IniFile(filename); * int devCount = global.devices.Count; * int regsCount = global.registers.Count; * * ini.Write("Dev", "Count", devCount.ToString()); * ini.Write("Regs", "Count", regsCount.ToString()); * * for (int i = 0; i < devCount; i++) * { * ini.Write("dev" + i.ToString(), "Name", global.devices[i].name); * ini.Write("dev" + i.ToString(), "Baud", global.devices[i].baudrate.ToString()); * ini.Write("dev" + i.ToString(), "Bits", global.devices[i].bits.ToString()); * ini.Write("dev" + i.ToString(), "Parity", global.devices[i].parity); * ini.Write("dev" + i.ToString(), "Port", global.devices[i].port); * ini.Write("dev" + i.ToString(), "StopBits", global.devices[i].stopbits); * ini.Write("dev" + i.ToString(), "Address", global.devices[i].address.ToString()); * } * * for (int i = 0; i < regsCount; i++) * { * ini.Write("reg" + i.ToString(), "Name", global.registers[i].name); * ini.Write("reg" + i.ToString(), "Type", global.registers[i].type.ToString()); * ini.Write("reg" + i.ToString(), "Reg", global.registers[i].reg.ToString()); * ini.Write("reg" + i.ToString(), "Device", global.registers[i].device.ToString()); * ini.Write("reg" + i.ToString(), "Address", global.registers[i].address.ToString()); * ini.Write("reg" + i.ToString(), "Length", global.registers[i].len.ToString()); * ini.Write("reg" + i.ToString(), "GUID", global.registers[i].GUID.ToString()); * }*/ return(true); } else { DevData data = new DevData(); data.devices = devices; return(Files.ExportXml(filename, data)); } }
public static bool LoadDevices(string filename, bool append = false) { string x = Path.GetExtension(filename); if (Path.GetExtension(filename) == ".ini") { /*IniFile ini; * ini = new IniFile(filename); * int devCount = ini.ReadInt("Dev", "Count", 0); * int regsCount = ini.ReadInt("Regs", "Count", 0); * * global.devices = new List<device_t>(); * * for (int i = 0; i < devCount; i++) * { * device_t devItem = new device_t(); * devItem.name = ini.Read("dev" + i.ToString(), "Name", "dev" + i.ToString()); * devItem.baudrate = ini.ReadInt("dev" + i.ToString(), "Baud", 9600); * devItem.bits = ini.ReadInt("dev" + i.ToString(), "Bits", 8); * devItem.parity = ini.Read("dev" + i.ToString(), "Parity", "Even"); * devItem.port = ini.Read("dev" + i.ToString(), "Port", "COM1"); * devItem.stopbits = ini.Read("dev" + i.ToString(), "StopBits", "One"); * devItem.address = ini.ReadByte("dev" + i.ToString(), "Address", 1); * * global.devices.Add(devItem); * } * * for (int i = 0; i < regsCount; i++) * { * reg_t regItem = new reg_t(); * regItem.name = ini.Read("reg" + i.ToString(), "Name", "reg" + i.ToString()); * regItem.type = ini.ReadInt("reg" + i.ToString(), "Type", 0); * regItem.reg = ini.ReadInt("reg" + i.ToString(), "Reg", 0); * regItem.device = ini.Read("reg" + i.ToString(), "Device", ""); * regItem.address = (ushort)ini.ReadInt("reg" + i.ToString(), "Address", 0); * regItem.len = (ushort)ini.ReadInt("reg" + i.ToString(), "Length", 1); * regItem.value = ""; * regItem.GUID = ini.ReadGUID("reg" + i.ToString(), "GUID", Guid.NewGuid()); * global.registers.Add(regItem); * }*/ return(true); } else { DevData data = new DevData(); try { data = (DevData)Files.ImportXml(filename, data); // ----- Clear last values & generate new Guid ----- for (int j = 0; j < data.devices.Count; j++) { if (data.devices[j].packet != null) { for (int k = 0; k < data.devices[j].packet.Count; k++) { packet_t pItem = data.devices[j].packet[k]; pItem.GUID = Guid.NewGuid(); data.devices[j].packet.RemoveAt(k); data.devices[j].packet.Insert(k, pItem); if (data.devices[j].packet[k].regs != null) { for (int i = 0; i < data.devices[j].packet[k].regs.Count; i++) { reg_t item = data.devices[j].packet[k].regs[i]; item.value = ""; item.GUID = Guid.NewGuid(); data.devices[j].packet[k].regs.RemoveAt(i); data.devices[j].packet[k].regs.Insert(i, item); } } } } } }catch (Exception) { return(false); } if (append) { foreach (var devItem in data.devices) { devices.Add(devItem); } } else { devices = data.devices; } RefreshGroupList(); return(true); } }