/// <summary> /// Старт сервера /// </summary> public bool StartServer() { bool result = false; if ((this.TypeInterfaceModbus & 0x01) != 0) { try { if (this.LogEnable) { this.Log.AppendLogIO("Параметры сервера Modbus RTU:" + Environment.NewLine + this.SettingsRTU.GetParams() + Environment.NewLine); } this.mbS_rtu = new ModbusS_RTU(this.SettingsRTU); this.mbS_rtu.SlaveFcn = this.modbusFcn; this.mbS_rtu.AdressDevice = (byte)this.AddressDevice; this.mbS_rtu.onDataCame += new EventHandler<EventModbusArgs>(delegate(object x, EventModbusArgs y) { StringBuilder sb = new StringBuilder(); foreach (var v in y.Request) sb.Append(string.Format("[{1}{0:X}] ", v, (v < 16 ? "0" : ""))); this.Log.AppendMBInput(sb.ToString() + Environment.NewLine); sb.Clear(); foreach (var v in y.Response) sb.Append(string.Format("[{1}{0:X}] ", v, (v < 16 ? "0" : ""))); this.Log.AppendMBOutput(sb.ToString() + Environment.NewLine); }); this.mbS_rtu.AutoResponse = true; this.mbS_rtu.ModbusStart(); if (this.LogEnable) { this.Log.AppendLogIO("Сервер Modbus RTU запущен" + Environment.NewLine); } result = true; } catch(Exception ex) { if (this.LogEnable) { this.Log.AppendLogIO("Ошибка при запуске сервера Modbus RTU:" + Environment.NewLine + ex.Message + Environment.NewLine); } } } else { if (this.LogEnable) { this.Log.AppendLogIO("Сервер Modbus RTU не задействован" + Environment.NewLine); } } if ((this.TypeInterfaceModbus & 0x02) != 0) { try { if (this.LogEnable) { this.Log.AppendLogIO("Параметры сервера Modbus TCP:" + Environment.NewLine + this.SettingsTCP.GetParams() + Environment.NewLine); } this.mbS_tcp = new ModbusS_TCP(this.SettingsTCP); this.mbS_tcp.SlaveFcn = this.modbusFcn; this.mbS_tcp.AdressDevice = (byte)this.AddressDevice; this.mbS_tcp.onConnected += new EventHandler<EventModbusArgs>(delegate(object x, EventModbusArgs y) { this.Log.AppendLogIO(string.Format("Клиент подключен ({0}){1}", y.CountClients, Environment.NewLine)); }); this.mbS_tcp.onDisconnected += new EventHandler<EventModbusArgs>(delegate(object x, EventModbusArgs y) { this.Log.AppendLogIO(string.Format("Клиент отключен ({0}){1}", y.CountClients, Environment.NewLine)); }); this.mbS_tcp.onDataCame += new EventHandler<EventModbusArgs>(delegate(object x, EventModbusArgs y) { StringBuilder sb = new StringBuilder(); foreach (var v in y.Request) sb.Append(string.Format("[{1}{0:X}] ", v, (v < 16 ? "0" : ""))); this.Log.AppendMBInput(sb.ToString() + Environment.NewLine); sb.Clear(); foreach (var v in y.Response) sb.Append(string.Format("[{1}{0:X}] ", v, (v < 16 ? "0" : ""))); this.Log.AppendMBOutput(sb.ToString() + Environment.NewLine); }); this.mbS_tcp.AutoResponse = true; this.mbS_tcp.ModbusStart(); if (this.LogEnable) { this.Log.AppendLogIO("Сервер Modbus TCP запущен" + Environment.NewLine); } result = true; } catch (Exception ex) { if (this.LogEnable) { this.Log.AppendLogIO("Ошибка при запуске сервера Modbus TCP:" + Environment.NewLine + ex.Message + Environment.NewLine); } } } else { if (this.LogEnable) { this.Log.AppendLogIO("Сервер Modbus TCP не задействован" + Environment.NewLine); } } return result; }
/// <summary> /// Инициализация Модбас /// </summary> public void InitModbus() { if ((this.TypeInterfaceModbus & 0x01) != 0) { //this.mbS_rtu = new ModbusS_RTU() this.mbM_rtu = new ModbusM_RTU(this.SettingsRTU); } if ((this.TypeInterfaceModbus & 0x02) != 0) { this.mbS_tcp = new ModbusS_TCP(this.SettingsTCP); //this.mbM_tcp = new ModbusM_TCP(this.SettingsTCP); } }