/// <summary> /// 设置数据 /// </summary> /// <param name="dataName">请求点集合名称</param> /// <param name="dataList">点数据集合</param> /// <returns></returns> public override int SetData(string dataName, Hashtable dataList) { int result = 1; pb_data_sensor_list data_sensor_list = new pb_data_sensor_list(); try { foreach (var data in dataList.Values) { data_sensor_list.PbDataSensors.Add((pb_data_sensor)data); } MemoryStream mStream = new MemoryStream(); data_sensor_list.WriteTo(mStream); result = Rtds.rtds_set_data(mStream.ToArray(), mStream.ToArray().Length); if (result != 1) { log.Debug(string.Format("SetData Error:calling rtds_set_data method ,method returns:{0}", result)); result = (int)eErrorCode.SETDATA_ERR; } } catch (Exception ex) { result = (int)eErrorCode.SETDATA_ERR; log.Debug(string.Format("SetData Error:set data failed,because {0}", ex.Message.ToString())); } return(result); }
/// <summary> /// 构造函数 /// </summary> public RDB() { try { // 读取标签点配置文件及开辟空间 返回 0 成功 int result = 1; result = Rtds.rtds_init(); if (result != 1) { log.Debug(string.Format("rtds init failed,calling rtds_init method back result: {0}", result)); } else { log.Info("rtds init success"); } } catch (Exception ex) { log.Debug(string.Format("rtds init failed,because {0}", ex.Message.ToString())); } }
/// <summary> /// 开始读取数据并更新内存AllDataDict /// </summary> /// <param name="configObj">配置项名称</param> private void Work(object configObj) { string configName = configObj.ToString(); // 获取数据更新周期 int cycle = 0; foreach (KeyValuePair <string, PbConfig> kvp in AllConfigDict) { if (kvp.Key == configName) { cycle = ((pb_config_rtds)kvp.Value).UpdateCycle; break; } } while (GetThreadStt(configName)) { var now = DateTime.Now; try { pb_data_sensor_list pbDataList = new pb_data_sensor_list(); foreach (KeyValuePair <string, List <string> > kvp in TagAndConfigDict) { if (kvp.Value.Contains(configName)) { pb_data_sensor dataObject = new pb_data_sensor(); dataObject.Name = kvp.Key; pbDataList.PbDataSensors.Add(dataObject); } } MemoryStream mStream = new MemoryStream(); pbDataList.WriteTo(mStream); // 定义这个c#中用来接收c++返回数据的指针类型 IntPtr unmanaged_ptr = IntPtr.Zero; int length = Rtds.rtds_get_cur_data(mStream.ToArray(), mStream.ToArray().Length, out unmanaged_ptr); if (length > 0) { byte[] managed_data = new byte[length]; // 将非托管内存拷贝成托管内存,才能在c#里面使用 Marshal.Copy(unmanaged_ptr, managed_data, 0, length); // 遍历返回标签点数据 并更新到内存中 pb_data_sensor_list data_sensor_list = pb_data_sensor_list.Parser.ParseFrom(managed_data); foreach (pb_data_sensor pb_data_sensor in data_sensor_list.PbDataSensors) { if (AllDataDict.ContainsKey(pb_data_sensor.Name)) { ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Type = pb_data_sensor.Type; ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Size = pb_data_sensor.Size; ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Value = pb_data_sensor.Value; ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Time = pb_data_sensor.Time; ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Quality = pb_data_sensor.Quality; ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Status = pb_data_sensor.Status; ((pb_data_sensor)AllDataDict[pb_data_sensor.Name]).Unit = pb_data_sensor.Unit; } } // 清理C++内存空间 Rtds.rtds_release_mem(out unmanaged_ptr); } else { log.Debug(string.Format("GetData Error:calling rtds_get_cur_data method,method returns data length:{0}", length)); } // 实时更新状态 为已运行 3 SetRtdsStatus("3"); } catch (Exception ex) { // 实时更新状态 为异常 4 SetRtdsStatus("4"); log.Debug(string.Format("GetData Error:update cache data failed,because {0}", ex.Message.ToString())); } // 如果数据更新周期为0 则只取一次数据 if (cycle == 0) { break; } else { var cur = DateTime.Now; TimeSpan ts = cur - now; if (ts.TotalMilliseconds < cycle * 1000) { Thread.Sleep(cycle * 1000 - (int)ts.TotalMilliseconds); } } } }