private async void uiButton1_Click(object sender, EventArgs e) { await GasMonitorManager.Close(); await GasMonitorManager.Start(); }
public static void InitMonitorManager(MainForm mainForm) { try { MainForm = mainForm; //实时数据缓存池 ReceiveCachec = new ReceiveCachec(); ReceiveCachec.OnGeted += ReceiveCachec_OnGeted; ReceiveCachec.OnPushed += ReceiveCachec_OnPushed; ReceiveCachec.MaxNumber = 1000; //报警缓存池 ReceiveAlarmCachec = new ReceiveCachec(); ReceiveAlarmCachec.OnGeted += ReceiveAlarmCachec_OnGeted; ReceiveAlarmCachec.OnPushed += ReceiveAlarmCachec_OnPushed; ReceiveAlarmCachec.MaxNumber = 1000; ///报警状态 ReceiveAlarmStatusCachec = new ReceiveCachec(); ReceiveAlarmStatusCachec.OnGeted += ReceiveAlarmStatusCachec_OnGeted; ReceiveAlarmStatusCachec.OnPushed += ReceiveAlarmStatusCachec_OnPushed; ReceiveAlarmStatusCachec.MaxNumber = 1000; Config = new GasMonitorConfig(); GasMonitorManager.Chanels = Config.Channels; //加载系统配置表单信息 systenSetFrm.InitForms(); realDataFrm.InitForms(); realSeriesFrm.InitForms(); alarmSetFrm.InitForms(); //初始化主界面左边树结构 MainForm.InitForms(); SerialConfig serialConfig = new SerialConfig() { BaudRate = int.Parse(Config.SerialPort.BaudRate), CollectFaultsInternal = int.Parse(Config.SerialPort.CollectFaultsInternal), CollectFaultsNumber = int.Parse(Config.SerialPort.CollectFaultsNumber), ContinueCollect = string.IsNullOrEmpty(Config.SerialPort.ContinueCollect) || Config.SerialPort.ContinueCollect.Trim() == "0" ? false : true, DataBits = int.Parse(Config.SerialPort.DataBits), ModbusType = "RTU", OffsetInterval = int.Parse(Config.SerialPort.OffsetInterval), PackMaxSize = int.Parse(Config.SerialPort.PackMaxSize), ReadTimeout = int.Parse(Config.SerialPort.ReadTimeout), WriteTimeout = int.Parse(Config.SerialPort.WriteTimeout), SerialCheck = (Parity)Enum.Parse(typeof(Parity), Config.SerialPort.CheckBit), SerialPort = Config.SerialPort.Port, UpdateCycle = Config.SerialPort.UpdateCycle, RTSEnable = string.IsNullOrEmpty(Config.SerialPort.RTSEnable) || Config.SerialPort.RTSEnable.Trim() == "0" ? false : true, RSTSendPreKeeyTime = int.Parse(Config.SerialPort.RSTSendPreKeeyTime), RTSSendAfterKeeyTime = int.Parse(Config.SerialPort.RSTSendPreKeeyTime), Chanels = Chanels, StopBits = StopBits.One, ModbusParas = Config.ModbusPara }; switch (Config.SerialPort.StopBit.Trim()) { case "0": serialConfig.StopBits = StopBits.None; break; case "1": serialConfig.StopBits = StopBits.One; break; case "2": serialConfig.StopBits = StopBits.Two; break; case "1.5": serialConfig.StopBits = StopBits.OnePointFive; break; default: serialConfig.StopBits = StopBits.One; break; } ModbusSlave = new Modbus_Serial(serialConfig); ModbusSlave.LogOutput = new Action <string>((s) => { if (logFrm != null && !logFrm.IsDisposed) { logFrm.AddLog(s); } }); //接收到数据,在此处进行解析 ModbusSlave.ReadDataSuccessed = new Action <List <RealData> >((s) => { try { if (s == null || s.Count <= 0) { return; } string datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "," + DateTime.Now.Millisecond.ToString(); string str = ""; foreach (RealData d in s) { if (d.ReadSendByte == null || d.ReadSendByte.Length <= 0) { continue; } str += ModbusConvert.ByteToHexStr(d.ReadSendByte) + " ,"; try { ReceiveItem realData = new ReceiveItem(); ReceiveItem alarmData = new ReceiveItem(); ReceiveItem alarmstatus = new ReceiveItem(); realData.Id = d.SlaveId; realData.Name = d.ParaName; realData.Address = d.Address; realData.DateTime = datetime; realData.Value = "-9999"; alarmData.Id = d.SlaveId; alarmData.Name = d.ParaName; alarmData.Address = d.Address; alarmData.DateTime = datetime; alarmData.Value = "0.00"; alarmstatus.Id = d.SlaveId; alarmstatus.Name = d.ParaName; alarmstatus.Address = d.Address; alarmstatus.DateTime = datetime; alarmstatus.Value = "0.00"; //获取放大倍数 byte[] blxs = new byte[2]; Array.Copy(d.ReadSendByte, 12, blxs, 0, 2); float blxsvalue = ModbusConvert.GetUShort(ModbusConvert.Bytes2Ushorts(blxs), 0); blxsvalue = Convert.ToSingle(Math.Pow(10, blxsvalue)); //获取单位 byte[] unitb = new byte[2]; Array.Copy(d.ReadSendByte, 14, unitb, 0, 2); realData.Unit = (new GasUnit(ModbusConvert.GetUShort(ModbusConvert.Bytes2Ushorts(unitb), 0))).Unit; //获取气体值 realData.Address = d.Address; realData.DateTime = datetime; byte[] vub = new byte[4]; Array.Copy(d.ReadSendByte, 0, vub, 0, 4); realData.Value = (ModbusConvert.GetUInt(ModbusConvert.Bytes2Ushorts(vub), 0) / blxsvalue).ToString("0.00"); //获取报警状态 alarmstatus.Address = d.Address; alarmstatus.DateTime = datetime; byte[] alarmstatusb = new byte[2]; Array.Copy(d.ReadSendByte, 4, alarmstatusb, 0, 2); alarmstatus.Value = ModbusConvert.GetUShort(ModbusConvert.Bytes2Ushorts(alarmstatusb), 0).ToString(); //获取报警值 if (alarmstatus.Value == "0")//无报警 { alarmData.Address = d.Address; alarmData.DateTime = datetime; alarmData.Value = "0"; } else if (alarmstatus.Value == "1")//低报警 { alarmData.Address = d.Address; alarmData.DateTime = datetime; byte[] alarmb = new byte[4]; Array.Copy(d.ReadSendByte, 16, alarmb, 0, 4); alarmData.Value = (ModbusConvert.GetUInt(ModbusConvert.Bytes2Ushorts(alarmb), 0) / blxsvalue).ToString("0.00"); } else if (alarmstatus.Value == "2")//高报警 { alarmData.Address = d.Address; alarmData.DateTime = datetime; byte[] alarmb = new byte[4]; Array.Copy(d.ReadSendByte, 20, alarmb, 0, 4); alarmData.Value = (ModbusConvert.GetUInt(ModbusConvert.Bytes2Ushorts(alarmb), 0) / blxsvalue).ToString("0.00"); } alarmData.Id = realData.Id; alarmData.Unit = realData.Unit; alarmData.Name = d.ParaName; alarmData.Address = d.Address; alarmData.DateTime = datetime; if (realData != null && realData.Value != "-9999") { realDataFrm.SetChannelStatus(); realDataFrm.SetReadData(realData, alarmData, alarmstatus); realSeriesFrm.SetReadSeries(realData, alarmData, alarmstatus); GasMonitorManager.ReceiveCachec.PushItem(realData); GasMonitorManager.ReceiveAlarmCachec.PushItem(alarmData); GasMonitorManager.ReceiveAlarmStatusCachec.PushItem(alarmstatus); } } catch { continue; } } logFrm.AddLog(str); } catch (Exception emx) { logFrm.AddLog(emx.Message); } }); GasMonitorManager.Start(); } catch (Exception emx) { logFrm.AddLog(emx.Message + " ERROR=01"); } }