/// <summary> /// 添加一个ComLine对象,当前对象替换同名老对象 /// </summary> public static void AddComLine(ComLine cl) { if (_dictComLines.ContainsKey(cl.Name) == false) { _dictComLines.Add(cl.Name, cl); } else { if (Log.IsDebugEnabled) { Log.DebugFormat("[{0}]已经存在,替换添加", cl.Name); } _dictComLines[cl.Name].Clear(); _dictComLines.Remove(cl.Name); _dictComLines.Add(cl.Name, cl); } if (Log.IsDebugEnabled) { Log.DebugFormat("[{0}]添加成功", cl.Name); } }
private void device_init() { try { ComHelper.InitComLinesandDevices(); ComLine cl = ComHelper.GetComLines()[0]; stalabMBPortName.Text = cl.COM.PortName; stalabMBBaudRate.Text = cl.COM.BaudRate.ToString(); CKM55LC_M ck = (CKM55LC_M)cl.Devices[0]; ck.SetResult += ShowSetResult; cl.AddTimer(ComHelper.GetComConfig()[0].RefreshPeriod, true, ck.FactoryRefresh); cl.AddTimer(ComHelper.GetComConfig()[0].RefreshPeriod, true, fill_datatable); ck.Online += new BaseDevice.DeviceEventHandler(show_device_online); ck.Offline += new BaseDevice.DeviceEventHandler(show_device_offline); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 初始化所有ComLines以及其挂接的设备 /// </summary> public static void InitComLinesandDevices() { string msg = ""; List <ComConfig> coms = GetComConfig(); foreach (ComConfig com in coms) { bool err = false; try { com.ComPort.Open(); } catch (System.Exception ex) { string str = string.Format("初始化[{0}]失败:", com.ComPort.PortName) + ex.Message; Log.Error(str); msg += str + "\n"; err = true; } ComLine cl = new ComLine(com.ComPort); cl.ReceiveTimeout = com.Timeout; cl.SendTimeout = com.Timeout; cl.InterFrameGap = com.InterFrameGap; cl.CheckConnect(true, (uint)com.ConnectCheckPeriod); foreach (byte id in com.Device.Keys) { try { #if CKK65 if (com.Device[id] == "CKK65") { CKK65 dev = new CKK65(id, cl); cl.AddDevice(dev); //cl.AddTimer(com.RefreshPeriod, true, dev.PeriodRefresh); } else if (com.Device[id] == "CKM55LC-M") #else if (com.Device[id] == "CKM55LC-M") #endif { CKM55LC_M dev = new CKM55LC_M(id, cl); cl.AddDevice(dev); } else { string str = string.Format("初始化[{0}][{1}][{2}]失败:不支持此设备类型,此设备将被忽略", com.ComPort.PortName, id, com.Device[id]); Log.Warn(str); msg += str + "\n"; } } catch (System.Exception ex) { string str = string.Format("初始化[{0}][{1}][{2}]失败:", com.ComPort.PortName, id, com.Device[id]) + ex.Message; Log.Error(str); msg += str + "\n"; err = true; } } AddComLine(cl); string info = "挂接设备"; foreach (BaseDevice bdev in cl.Devices) { info += string.Format("[{0}-{1}],", bdev.Index, bdev.Type); } if (err == true) { Log.ErrorFormat("初始化[{0}]失败,{1}", cl.Name, info); } else { Log.InfoFormat("初始化[{0}]成功,{1}", cl.Name, info); } } if (msg != "") { Log.Error(msg); throw new Exception(msg); } }
public Publisher(ComLine line, CancellationTokenSource tokenSource) { _line = line; _tokenSource = tokenSource; }
public Publisher(ComLine line, CancellationToken token) : this(line, CancellationTokenSource.CreateLinkedTokenSource(token)) { }
public Publisher(ComLine line) : this(line, new CancellationTokenSource()) { }
public CKM55LC_M(byte index, BaseLine line) : base(index, "CKM55LC_M") { Line = line; _lock = new object(); string str = Line.GetType().ToString(); if (str == "Lines.Com.ComLine") { ComLine cl = (ComLine)line; _modbus = new Modbus(index); _modbus.InterFrameGap = cl.InterFrameGap; _modbus.CaptureLineArgs += new CaptureLineArgsEventHandler(cl.CaptureLine); _modbus.ReleaseLineArgs += new ReleaseLineArgsEventHandler(cl.ReleaseLine); _modbus.DiscardSendBuffer += new CommonEventHandler(cl.DiscardSendBuffer); _modbus.DiscardReceiveBuffer += new CommonEventHandler(cl.DiscardReceiveBuffer); _modbus.Send += new IOEventHandler(cl.Send); _modbus.Receive += new IOEventHandler(cl.Receive); if (Log.IsDebugEnabled) { Log.DebugFormat("初始化CKM55LC_M[{0}]绑定ComLine[{1}]", index, cl.Name); } } else if (str == "Lines.Ethernet.NetLine") { } else { Log.Error("unsupported Line type:" + str); throw new Exception("unsupported Line type:" + str); } //异步操作Handler对象初始化 _SetRegisterHandle = new GetSetRegisterHandler(SetRegister); _GetRegisterHandle = new GetSetRegisterHandler(GetRegister); //各类寄存器对象初始化 _RunValues = new RunValue(); _RunValues.GetRegister += new GetSetRegisterHandler(GetRegister); _RunValues.GetRegisterAsync += new GetSetRegisterAsyncHandler(GetRegisterAsync); _MonitorValues = new MonitorValue(); _MonitorValues.GetRegister += new GetSetRegisterHandler(GetRegister); _MonitorValues.GetRegisterAsync += new GetSetRegisterAsyncHandler(GetRegisterAsync); _ControlSets = new ControlSet(); _ControlSets.SetRegisterAsync += new GetSetRegisterAsyncHandler(SetRegisterAsync); _DiagnosisValues = new DiagnosisValue(); _DiagnosisValues.GetRegister += new GetSetRegisterHandler(GetRegister); _DiagnosisValues.GetRegisterAsync += new GetSetRegisterAsyncHandler(GetRegisterAsync); _ProtectSets = new ProtectSet(); _ProtectSets.GetRegister += new GetSetRegisterHandler(GetRegister); _ProtectSets.GetRegisterAsync += new GetSetRegisterAsyncHandler(GetRegisterAsync); _ProtectSets.SetRegisterAsync += new GetSetRegisterAsyncHandler(SetRegisterAsync); _SystemSets = new SystemSet(); _SystemSets.GetRegister += new GetSetRegisterHandler(GetRegister); _SystemSets.GetRegisterAsync += new GetSetRegisterAsyncHandler(GetRegisterAsync); _SystemSets.SetRegisterAsync += new GetSetRegisterAsyncHandler(SetRegisterAsync); _IdentityValues = new IdentityValue(); _IdentityValues.GetRegister += new GetSetRegisterHandler(GetRegister); _IdentityValues.GetRegisterAsync += new GetSetRegisterAsyncHandler(GetRegisterAsync); _IdentityValues.SetRegister += new GetSetRegisterHandler(SetRegister); _CalibrationValues = new CalibrationValue(); _CalibrationValues.GetRegister += new GetSetRegisterHandler(GetRegister); _CalibrationValues.GetRegisterAsync += new GetSetRegisterAsyncHandler(GetRegisterAsync); _CalibrationValues.SetRegisterAsync += new GetSetRegisterAsyncHandler(SetRegisterAsync); _CalibrationValues.SetRegister += new GetSetRegisterHandler(SetRegister); GetAllRegistersAsync(); }