/// <summary> /// ControlBlock模式读取数据块 /// </summary> private void ControlBlockModeRead() { byte[] buffer; for (int i = 0; i < ControlBlock.Count; i++) { PLCDBBlock block = ControlBlock[i]; buffer = new byte[block.BufferLength]; int resNo = cycleReadConnection.ReadBlock( DBType, DBNumber, block.Start_Offset, block.BufferLength, ref buffer, out string errText); if (resNo != 0) { _log.Error( $"设备[{Name}]读取[{DBType}][{DBNumber}]" + $"Offset[{block.Start_Offset}]Length[{block.BufferLength}]" + $"失败,失败信息:[Code:{resNo},Message:{errText}]"); } else { DoSomething(ControlBlock.GetKey(i), buffer); } } }
/// <summary> /// 根据关键字处理指定TagGroup所对应的数据块数据 /// </summary> private void DealPartBlockBuffer(string key, byte[] buffer) { PLCDBBlock block = ControlBlock[key]; if (block != null && key != "COMM") { _log.Debug( $"[{key}.Offset={block.Start_Offset}, " + $"{key}.Length={block.BufferLength}]"); _log.Debug($"[{key}]|[Data:{Tools.BytesToBCD(buffer)}]"); } // 根据key解析出TagGroup和SubTagGroup string[] keys = key.Split('.'); string keyTagGroup = keys[0]; string keySubTagGroup = ""; if (keys.Length >= 2) { keySubTagGroup = keys[1]; } SiemensTagGroup group = _groups[keyTagGroup] as SiemensTagGroup; if (group == null) { _log.Error($"设备[{Name}]未找到[{keyTagGroup}]Tag组"); return; } if (keySubTagGroup == "") { SetControlTagValueInGroup(group, buffer, block.Start_Offset); } else { SiemensSubTagGroup subGroup = group.SubGroups[keySubTagGroup] as SiemensSubTagGroup; if (subGroup == null) { _log.Error($"设备[{Name}]未找到[{keyTagGroup}.{keySubTagGroup}]Tag组"); return; } else { SetControlTagValueInGroup(subGroup, buffer, block.Start_Offset); } } }
/// <summary> /// 在集合中新增一个PLCDBReadBlock对象 /// </summary> /// <param name="key">关键字</param> /// <param name="item">PLCDBReadBlock对象</param> public void Add(string key, PLCDBBlock item) { if (key == "") { throw new Exception("关键字参数不能空白"); } if (item == null) { throw new Exception("PLCDBReadBlock对象不能是null"); } if (_items.ContainsKey(key)) { throw new Exception($"集合中已经存在关键字[{key}]的PLCDBReadBlock对象"); } _items.Add(key, item); }
/// <summary> /// Tag对象注册事件 /// </summary> /// <param name="group">TagGroup对象</param> /// <param name="tag">Tag对象</param> private void OnTagRegister(CustomGroup group, CustomTag tag) { _log.Trace($"Device[{Name}]:Tag[{tag.Name}],Offset[{tag.DB_Offset}]"); if (!(tag is SiemensTag)) { throw new Exception($"tag对象:{tag.Name}不是SiemensTag对象"); } SiemensTag siemensTag = tag as SiemensTag; switch (CycleReadMode) { case SiemensCycleReadMode.FullBlock: FullBlock.Add(siemensTag.DB_Offset, siemensTag.Length); break; case SiemensCycleReadMode.ControlBlock: if (siemensTag.Type == TagType.C) { string key = ""; if (group is SiemensTagGroup) { key = (group as SiemensTagGroup).Name; } else if (group is SiemensSubTagGroup) { SiemensSubTagGroup subGroup = group as SiemensSubTagGroup; key = $"{subGroup.Parent.Name}.{subGroup.Prefix}"; } PLCDBBlock block = ControlBlock[key]; if (block == null) { block = new PLCDBBlock(); ControlBlock.Add(key, block); } block.Add(siemensTag.DB_Offset, siemensTag.Length); } break; } }