/// <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); } }
/// <summary> /// Соединине клиента /// </summary> public bool ConnectClient() { bool result = false; if ((this.TypeInterfaceModbus & 0x01) != 0) { try { if (this.LogEnable) { this.Log.AppendLogIO("Настройки подключения клиента Modbus RTU:" + Environment.NewLine); } this.mbM_rtu = new ModbusM_RTU(this.SettingsRTU); this.mbM_rtu.AutoRequest.DeviceRequest = (byte)this.AddressDevice; this.mbM_rtu.AutoRequest.Protocol = this._protocol; this.mbM_rtu.onConnected += new EventHandler<EventModbusArgs>(delegate(object x, EventModbusArgs y) { this.Log.AppendLogIO(string.Format("Соединение с сервером установлено")); }); this.mbM_rtu.onDisconnected += new EventHandler<EventModbusArgs>(delegate(object x, EventModbusArgs y) { this.Log.AppendLogIO(string.Format("Соединение с сервером разорвано")); }); this.mbM_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.mbM_rtu.ModbusStart(); this.mbM_rtu.AutoRequestEnable = true; 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); } } } if ((this.TypeInterfaceModbus & 0x02) != 0) { try { if (this.LogEnable) { this.Log.AppendLogIO("Настройки подключения клиента Modbus TCP:" + Environment.NewLine); } this.mbM_tcp = new ModbusM_TCP(this.SettingsTCP); this.mbM_tcp.AutoRequest.DeviceRequest = (byte)this.AddressDevice; this.mbM_tcp.AutoRequest.Protocol = this._protocol; this.mbM_tcp.onConnected += new EventHandler<EventModbusArgs>(delegate(object x, EventModbusArgs y) { this.Log.AppendLogIO(string.Format("Соединение с сервером установлено")); }); this.mbM_tcp.onDisconnected += new EventHandler<EventModbusArgs>(delegate(object x, EventModbusArgs y) { this.Log.AppendLogIO(string.Format("Соединение с сервером разорвано")); }); this.mbM_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.mbM_tcp.ModbusStart(); this.mbM_tcp.AutoRequestEnable = true; 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); } } } return result; }