WPF进行IO点位监控-读取西门子S7-200 PLC

Xaml程序

  <Grid>
      <Grid.RowDefinitions>
          <RowDefinition Height="30" />
          <RowDefinition Height="10"/>
          <RowDefinition Height="30" />
          <RowDefinition Height="10"/>
          <RowDefinition Height="30" />
          <RowDefinition Height="10"/>
          <RowDefinition Height="30" />
          <RowDefinition Height="10"/>
          <RowDefinition Height="30" />
          <RowDefinition Height="10"/>
          <RowDefinition Height="30" />
          <RowDefinition Height="10"/>
          <RowDefinition Height="30" />
          <RowDefinition Height="10"/>
          <RowDefinition Height="30" />
          <RowDefinition Height="10"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
          <ColumnDefinition Width="10"/>
          <ColumnDefinition Width="80"/>
          <ColumnDefinition Width="10"/>
          <ColumnDefinition Width="50"/>
          <ColumnDefinition Width="30"/>
          <ColumnDefinition Width="50"/>
          <ColumnDefinition Width="10"/>
          <ColumnDefinition Width="50"/>
      </Grid.ColumnDefinitions>
      <TextBlock Grid.Row="2" Grid.Column="1" Text="V400.0" HorizontalAlignment="Right" VerticalAlignment="Center"/>
      <Rectangle Grid.Row="2" Grid.Column="3"   x:Name="A0" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#808080"/>

      <TextBlock Grid.Row="4" Grid.Column="1" Text="V400.1" HorizontalAlignment="Right" VerticalAlignment="Center"/>
      <Rectangle Grid.Row="4" Grid.Column="3"   x:Name="A1" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#808080"/>

      <TextBlock Grid.Row="6" Grid.Column="1" Text="V400.2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
      <Rectangle Grid.Row="6" Grid.Column="3"   x:Name="A2" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#808080"/>

      <Button Grid.Row="6" Grid.Column="5" Content="取反" Click="A2Button_Click"/>

  </Grid>

Cs程序

 private DispatcherTimer _timer;
 private bool _isRed = true;
 public uint V400, V401 = 0;
 
 public Plc my200 = new Plc(CpuType.S71200, "192.168.1.12", 0, 1);
 public Window3()
 {
     InitializeComponent();
     //初始化定时器
    _timer = new DispatcherTimer();
     _timer.Interval = TimeSpan.FromSeconds(0.1); 
     _timer.Tick += Timer_Tick; // 设置Tick事件处理程序  
     _timer.Start(); // 启动定时器 
     my200.Open();


 }

 private void A2Button_Click(object sender, RoutedEventArgs e)
 {
     if ((bool)my200.Read("DB1.DBX500.1"))
     {
         my200.Write("DB1.DBX500.1", 0);
     }
     else
     {
         my200.Write("DB1.DBX500.1", 1);
     }
 }

 private void Timer_Tick(object sender, EventArgs e)
 {
     try
     {
         V400 = (uint)my200.Read("DB1.DBD400");
         //Console.WriteLine("V400为{0}", V400); 
         //string binaryString = Convert.ToString(V400, 2).PadLeft(32, '0');
         //bool isHighestBitSet = (V400 & (1U << 31)) != 0; //31为最高位
          打印结果  
         //if (isHighestBitSet)
         //{
         //    Console.WriteLine("The highest bit is set (1).");
         //}
         //else
         //{
         //    Console.WriteLine("The highest bit is not set (0).");
         //}

          打印二进制字符串  
         //Console.WriteLine($"The binary representation of {V400} is: {binaryString}");
     

         if (((V400 & (1U << 24)) != 0)){
             A0.Fill = new SolidColorBrush(Colors.Green);
         }
         else{
             A0.Fill = new SolidColorBrush(Colors.Red);
         }

         if (((V400 & (1U << 25)) != 0)){
             A1.Fill = new SolidColorBrush(Colors.Green);
         }
         else{
             A1.Fill = new SolidColorBrush(Colors.Red);
         }
         if (((V400 & (1U << 26)) != 0)){
             A2.Fill = new SolidColorBrush(Colors.Green);
         }
         else{
             A2.Fill = new SolidColorBrush(Colors.Red);
         }
     }  
     catch (Exception)
     {
         Console.WriteLine($"连接到PLC设备失败:IsConnect = {my200.IsConnected}");
         return;
     }


 }

 //确保在窗口关闭时停止定时器
 //protected override void OnClosed(EventArgs e)
 //{
 //    //_timer.Stop();
 //    //base.OnClosed(e);
 //}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值