Explorar o código

调节PLC的值

user_lt hai 1 ano
pai
achega
88dab7e4a0

+ 39 - 20
BlankApp1/BlankApp1/ViewModels/MonitorManageViewModel/PLCWriteViewModel.cs

@@ -34,33 +34,60 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
         #region 私有方法
 
         private void WriteToPlc(object obj)
-        {
-            foreach (var item in PLCItemList)
+        {//没有连接 plc,直接退出
+            if (!PLCCom.GetInstance().IsConnectPLC())
+            {
+                MessageBox.Show("PLC未连接!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
+                return;
+            }
+          
+            long id = Convert.ToInt64(obj);
+            var findPlcItem= PLCItemList.FirstOrDefault(l => l.Id == id);
+            if(findPlcItem!=null)
             {
-                //没有连接 plc,直接退出
-                if (!PLCCom.GetInstance().IsConnectPLC())
+                string plcAddr = findPlcItem.PlcAddress;
+                string plcAddType = findPlcItem.PlcAddType;
+                string plcValue = findPlcItem.PlcValue;
+                if (string.IsNullOrEmpty(plcValue))
                 {
-                    break;
+                    MessageBox.Show("请填写数据!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
+                    return;
                 }
-                string plcAddr = item.PlcAddress;
-                string plcAddType = item.PlcAddType;
                 switch (plcAddType)
                 {
                     case "bool":
-                        bool isResult=PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Bit, WritePLCValue);
+                        bool isResult = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Bit, plcValue);
+                        if(isResult)
+                        {
+                            MessageBox.Show("写入成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+                        }
                         break;
                     case "word":
-                        bool isResult2 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Word, WritePLCValue);
+                        bool isResult2 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Word, plcValue);
+                        if (isResult2)
+                        {
+                            MessageBox.Show("写入成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+                        }
                         break;
                     case "dword":
-                        bool isResult3 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.DWord, WritePLCValue);
+                        bool isResult3 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.DWord, plcValue);
+                        if (isResult3)
+                        {
+                            MessageBox.Show("写入成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+                        }
                         break;
                     case "real":
-                        bool isResult4 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Real, WritePLCValue);
+                       
+                        bool isResult4 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Real, plcValue);
+                        if (isResult4)
+                        {
+                            MessageBox.Show("写入成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+                        }
                         break;
                 }
 
             }
+        
         }
         /// <summary>
         /// 获取PLC配置
@@ -85,15 +112,7 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
             set { plcItemList = value; RaisePropertyChanged(); }
         }
 
-        /// <summary>
-        /// 写入值
-        /// </summary>
-        private string writePLCValue;
-        public string WritePLCValue
-        {
-            get { return writePLCValue; }
-            set { writePLCValue = value; RaisePropertyChanged(); }
-        }
+    
         #endregion
 
         #region 命令绑定

+ 1 - 1
BlankApp1/BlankApp1/Views/BusinessManageView/QueryView.xaml

@@ -62,7 +62,7 @@
 
                 <Button  Content="查询" Width="80"  Margin="5,0"  Command="{Binding QueryCommand}" Style="{StaticResource NormalButtonStyle}" />
                 <Button  Content="重置" Width="80"  Margin="5,0"  Command="{Binding ResetCommand}" Style="{StaticResource NormalButtonStyle}" />
-                <Button  Content="导出Excel" Width="80"  Margin="5,0"  Command="{Binding ResetCommand}" Style="{StaticResource NormalButtonStyle}" />
+                <Button  Content="导出Excel" Width="80"  Margin="5,0"  Command="{Binding ExportCommand}" Style="{StaticResource NormalButtonStyle}" />
             </StackPanel>
         </Grid>
         <DataGrid  Grid.Row="2"  ColumnWidth="*" AutoGenerateColumns="False" HeadersVisibility="All" CanUserAddRows="False"  SelectionUnit="FullRow" SelectionMode="Single"   RowHeaderWidth="0"

+ 2 - 2
BlankApp1/BlankApp1/Views/MonitorManageView/PLCWriteView.xaml

@@ -21,11 +21,11 @@
                     <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
-                                <TextBox  Width="60" Text="{Binding WritePLCValue}"  HorizontalContentAlignment ="Left" Foreground="Black" Height="5" 
+                                <TextBox  Width="60" Text="{Binding PlcValue, Mode=OneWayToSource, UpdateSourceTrigger=LostFocus}"  HorizontalContentAlignment ="Left" Foreground="Black" Height="20" 
                                         Cursor="Hand" Margin="0,0,2,0" >
                                 </TextBox>
                                 <Button  Width="auto" Background="Transparent" HorizontalContentAlignment ="Left" Foreground="Black" Height="25" BorderThickness="0"
-                                    Command="{Binding DataContext.WriteCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}"  Cursor="Hand" >
+                                    Command="{Binding DataContext.WriteCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding Id}"  Cursor="Hand" >
                                     <StackPanel Orientation="Horizontal">
                                         <TextBlock  Text="写入" VerticalAlignment="Center" Foreground="Blue"/>
                                     </StackPanel>