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);
//}