| | |
| | | private string serverIP; |
| | | private GroupProperty groupProperty; |
| | | |
| | | public ListenEventCreater listenEventCreater; |
| | | //public ListenEventCreater listenEventCreater; |
| | | public event Action<Item, int> OnReadItemValueChanged; |
| | | public class Item |
| | | { |
| | | public string name; |
| | |
| | | |
| | | public Dictionary<string, Item> itemReadDict; |
| | | public Dictionary<string, Item> itemWriteDict; |
| | | public Dictionary<int, Item> itemReadHandleDict; |
| | | |
| | | public OPC(opcConfig opcConfig) |
| | | { |
| | |
| | | SetGroupProperty(opcReadGroup); |
| | | SetGroupProperty(opcWriteGroup); |
| | | |
| | | listenEventCreater = new ListenEventCreater(); |
| | | //listenEventCreater = new ListenEventCreater(); |
| | | opcReadGroup.DataChange+=new DIOPCGroupEvent_DataChangeEventHandler(opcReadGroup_DataChange); |
| | | |
| | | SetHandles(opcReadGroup, itemReadDict); |
| | | SetHandles(opcWriteGroup, itemWriteDict); |
| | | SetHandles(opcReadGroup, itemReadDict, out itemReadHandleDict); |
| | | SetHandles(opcWriteGroup, itemWriteDict, out _); |
| | | CommonUtil.WriteLog(LogType.Inf, "OPC Server 组列表创建完成"); |
| | | return true; |
| | | } |
| | |
| | | /// <param name="TimeStamps"></param> |
| | | private void opcReadGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) |
| | | { |
| | | //for (int i = 1; i <= NumItems; i++) |
| | | //{ |
| | | // foreach (Item item in itemReadDict.Values) |
| | | // { |
| | | // if (item.clientHandle == Convert.ToInt32(ClientHandles.GetValue(i))) |
| | | // { |
| | | // itemReadDict[item.name].value = Convert.ToInt32(ItemValues.GetValue(i)); |
| | | // string state = item.name; // 改变的item的名称 |
| | | // item.value = ItemValues.GetValue(i); |
| | | // listenEventCreater.ChangeValue(new EventData(item)); |
| | | // } |
| | | // } |
| | | //} |
| | | |
| | | for (int i = 1; i <= NumItems; i++) |
| | | { |
| | | foreach (Item item in itemReadDict.Values) |
| | | int clientHandle = Convert.ToInt32(ClientHandles.GetValue(i)); |
| | | if (itemReadHandleDict.ContainsKey(clientHandle)) |
| | | { |
| | | if (item.clientHandle == Convert.ToInt32(ClientHandles.GetValue(i))) |
| | | { |
| | | itemReadDict[item.name].value = Convert.ToInt32(ItemValues.GetValue(i)); |
| | | string state = item.name; // 改变的item的名称 |
| | | item.value = ItemValues.GetValue(i); |
| | | listenEventCreater.ChangeValue(new EventData(item)); |
| | | } |
| | | var item = itemReadHandleDict[clientHandle]; |
| | | int itemValue = Convert.ToInt32(ItemValues.GetValue(i)); |
| | | item.value = itemValue; |
| | | |
| | | CommonUtil.WriteLog($"Receive PLC singal:Name:{item.name},value:{itemValue}"); |
| | | OnReadItemValueChanged?.Invoke(item, itemValue); |
| | | } |
| | | } |
| | | } |
| | |
| | | /// <param name="opcGroup"></param> |
| | | /// <param name="itemDict"></param> |
| | | /// <returns></returns> |
| | | private bool SetHandles(OPCGroup opcGroup, Dictionary<string, Item> itemDict) |
| | | private bool SetHandles(OPCGroup opcGroup, Dictionary<string, Item> itemDict, out Dictionary<int, Item> itemHandleDict) |
| | | { |
| | | itemHandleDict = new Dictionary<int, Item>(); |
| | | OPCItems opcItems; |
| | | OPCItem opcItem; |
| | | opcItems = opcGroup.OPCItems; |
| | | foreach (var item in itemDict.Values) |
| | | { |
| | | item.clientHandle = itmHandleClient; |
| | | itmHandleClient++; |
| | | opcItem = opcItems.AddItem(item.name, item.clientHandle); |
| | | item.serverHandle = opcItem.ServerHandle; |
| | | |
| | | itemHandleDict[itmHandleClient] = item; |
| | | itmHandleClient++; |
| | | } |
| | | return true; |
| | | } |