/// <summary> /// 写入数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param>; private void btnWriteDataItem_Click(object sender, RoutedEventArgs e) { string message = null; if (!opcClienthelper.IsConnected) { message = DateTime.Now.ToString(dateString) + "请先连接服务器" + System.Environment.NewLine; asyncUpMessage(message); return; } OpcDataItem opcDataItem = null; OpcResult opcResult = OpcResult.Unknow; if (Equals(null, opcClienthelper.OpcDataItems) || opcClienthelper.OpcDataItems.Count < 1) { message = DateTime.Now.ToString(dateString) + "没有数据点" + System.Environment.NewLine; } else { opcDataItem = opcClienthelper.OpcDataItems.FirstOrDefault().Clone() as OpcDataItem; //bool newValue = (DateTime.Now.Millisecond % 2) == 0 ? true : false; //bool newValue = !tmpValue; //tmpValue = newValue; //System.Diagnostics.Debug.Print(tmpValue.ToString()); opcResult = opcClienthelper.Write(opcDataItem, 1); message = DateTime.Now.ToString(dateString) + "写入完成 " + opcResult + " " + (opcDataItem == null ? " " : opcDataItem.ToString()) + System.Environment.NewLine; } asyncUpMessage(message); }
private void btnUpdateDataItems_Click(object sender, RoutedEventArgs e) { string message; if (!opcClienthelper.IsConnected) { message = DateTime.Now.ToString(dateString) + "请先连接服务器" + System.Environment.NewLine; asyncUpMessage(message); return; } var strList = txtOpcDataItems.Text.Split('\r', '\n'); List <OpcDataItem> opcDataItems = new List <OpcDataItem>(strList.Count()); //txtOpcDataItems .Text.Split (System.Environment.NewLine ): foreach (var strOpcDataItem in strList) { var strOpcDataItemTmp = strOpcDataItem.Split(';'); if (strOpcDataItemTmp.Count() < 2) { continue; } OpcDataItem opcDataItem = new OpcDataItem(strOpcDataItemTmp[0], int.Parse(strOpcDataItemTmp[1]), strOpcDataItemTmp[2], strOpcDataItemTmp[3], (OpcResult)Enum.Parse(typeof(OpcResult), strOpcDataItemTmp[4])); opcDataItems.Add(opcDataItem); } opcClienthelper.RegisterOpcDataItemsAsync(opcDataItems); dataGridDataSource = new ObservableCollection <OpcDataItem>(opcDataItems); gvOpcDataItems.ItemsSource = dataGridDataSource; this.txtb.Text = "(" + dataGridDataSource.Count(a => a.Quality == OpcResult.S_OK) + "/" + dataGridDataSource.Count() + ")"; }
private void upOpcDataItem(OpcDataItem opcDataItem) { //this.txtb.Text = "(" + opcDataItem.Count(a => a.Quality == OpcResult.S_OK) + "/" + opcDataItem.Count() + ")"; //System.IO.File.AppendAllText("log.log", message); //gvOpcDataItems.ItemsSource = null; //gvOpcDataItems.ItemsSource = opcDataItem; //this.txtb.Text = "(" + opcDataItem.Count(a => a.Quality == OpcResult.S_OK) + "/" + opcDataItem.Count() + ")"; //this.Dispatcher.BeginInvoke ); this.Dispatcher.Invoke(new Action(() => { //dataGridDataSource //var v = dataGridDataSource.First(a => a.Name == opcDataItem.Name) ; // v = opcDataItem; dataGridDataSource.Add(opcDataItem); dataGridDataSource.Remove(opcDataItem); //gvOpcDataItems.ItemsSource = null; //gvOpcDataItems.ItemsSource = dataGridDataSource; this.txtb.Text = "(" + dataGridDataSource.Count(a => a.Quality == OpcResult.S_OK) + "/" + dataGridDataSource.Count() + ")"; })); }