// Заполняем DataModel данными public static void LoadConfigSystemFromFile(DataModel dm, SyncForParameterValue syncForParameterValue) { //const string RS_485 = "RS485"; //const string TCP_IP = "TCPIP"; const string PN_PANEL = "PN"; const string PW_PANEL = "PW"; string slaveId = ""; string deviceId = ""; string TimeOut = ""; string interval = ""; string period = ""; string Ncom = ""; string speed = ""; string bit = ""; string parity = ""; string stopBit = ""; string typeConn = ""; string IP = ""; string portTcp = ""; string typeD = ""; string conn = ""; string nameConn = ""; ////Открываем локальную папку //StorageFolder localFolder = ApplicationData.Current.LocalFolder; //// получаем файл //StorageFile configFile = await localFolder.GetFileAsync("config.ini"); //// читаем файл //string text = await FileIO.ReadTextAsync(configFile); string text = ReadConfigFile(); //Десериализуем Json формат dynamic xxx = Newtonsoft.Json.JsonConvert.DeserializeObject(text); int count = 0; foreach (object d in xxx.Connections) { typeConn = xxx.Connections[count].typeConn; Ncom = $"{xxx.Connections[count].Ncom}"; speed = $"{xxx.Connections[count].speed}"; bit = $"{xxx.Connections[count].bit}"; parity = $"{xxx.Connections[count].parity}"; stopBit = $"{xxx.Connections[count].stopBit}"; IP = xxx.Connections[count].IP; portTcp = xxx.Connections[count].portTcp; nameConn = xxx.Connections[count].name; count++; if (typeConn == "COM") { // Создаём точку подключения через COM по шине RS-485 Connection connection = new Connection(); connection.m_Name = nameConn; // Создаём экземпляр класса InterfaceRS485 для работы с COM connection.m_Interface = new InterfaceRS485(); connection.m_Interface.InterfaceParams.Add(Convert.ToInt32(Ncom)); // номер COM-порта connection.m_Interface.InterfaceParams.Add(Convert.ToInt32(speed)); // скорость connection.m_Interface.InterfaceParams.Add(Convert.ToInt32(bit)); // бит данных if (parity == "Odd") { connection.m_Interface.InterfaceParams.Add(1); // паритет } else if (parity == "Even") { connection.m_Interface.InterfaceParams.Add(2); // паритет } else { connection.m_Interface.InterfaceParams.Add(0); // паритет } connection.m_Interface.InterfaceParams.Add(Convert.ToInt32(stopBit)); // стоп бит dm.AddConnection(connection); } else { int port = Convert.ToInt32(portTcp); int[] ipAsInt = new int[4]; bool isValidIP = false; IPAddress _ip; if (IPAddress.TryParse(IP, out _ip) == true) { byte[] bytes = _ip.GetAddressBytes(); for (int i = 0; i < ipAsInt.Length; i++) { ipAsInt[i] = bytes[i]; } isValidIP = true; } if (port > 0 && isValidIP) { // Создаём точку подключения через TCP/IP по шине RS-485 Connection connection = new Connection(); connection.m_Name = nameConn; // Создаём экземпляр класса InterfaceTcpIP connection.m_Interface = new InterfaceTcpIP(); connection.m_Interface.InterfaceParams.Add(ipAsInt[0]); // IP:0 connection.m_Interface.InterfaceParams.Add(ipAsInt[1]); // IP:1 connection.m_Interface.InterfaceParams.Add(ipAsInt[2]); // IP:2 connection.m_Interface.InterfaceParams.Add(ipAsInt[3]); // IP:3 connection.m_Interface.InterfaceParams.Add(port); // TCP-порт dm.AddConnection(connection); } } } count = 0; //Выбор Девайсов из config foreach (object d in xxx.Device) { slaveId = $"{xxx.Device[count].slaveId}"; deviceId = $"{xxx.Device[count].deviceId}"; TimeOut = $"{xxx.Device[count].timeOut}"; interval = $"{xxx.Device[count].interval}"; period = $"{xxx.Device[count].period}"; typeD = $"{xxx.Device[count].typeD}"; conn = $"{xxx.Device[count].conn}"; count++; IDevice device_ = null; if (typeD == PW_PANEL) { // Создаём экземпляр класса DevicePW для панели PowerWizard device_ = new DevicePW(syncForParameterValue); } else if (typeD == PN_PANEL) { // Создаём экземпляр класса DevicePW для панели PowerWizard device_ = new DevicePN(syncForParameterValue); } if (device_ != null) { device_.Number = Convert.ToInt32(deviceId); // Порядковый номер устройства device_.Address = Convert.ToInt32(slaveId); // Адрес устройства device_.ParamsForInterface.Clear(); device_.ParamsForInterface.Add(Convert.ToInt32(TimeOut)); // Таймаут ответа устройства device_.ParamsForInterface.Add(Convert.ToInt32(interval)); // Таймаут между запросами к устройству device_.ParamsForInterface.Add(Convert.ToInt32(period)); // Таймаут между 2 байтами ответа устройства foreach (Connection connection in dm.Connections) { if (connection.m_Name == conn) { if ((connection.m_Interface as InterfaceRS485) != null) { ((ModbusDevice)device_).Protocol = new ProtocolModbusRTU(); } else if ((connection.m_Interface as InterfaceTcpIP) != null) { ((ModbusDevice)device_).Protocol = new ProtocolModbusTCP(); } if (((ModbusDevice)device_).Protocol != null) { connection.m_Devices.Add(device_); } break; } } } } }