第一步:添加wpf窗口;
第二步:在主窗体image的MouseLeftButtonUp事件中调用调试窗口;
StatusViewWindow svWindow = new StatusViewWindow();
svWindow.Show();
复制
第三步:在主窗体中开个线程通过udp接收光电广开的数据:
Thread t3 = new Thread(StartSwitchDataRevThread);//四个广电开关数据接受线程
t3.Name = "StartSwitchDataRevThread";
t3.Start();
t3.IsBackground = true;
复制
private void StartSwitchDataRevThread()
{
try
{
Stopwatch elapsetime = new Stopwatch();
UdpClient client = new UdpClient(8012);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.1.202"), 8008);
client.Client.ReceiveBufferSize = 1024 * 1024;//默认值是8192
while (true)
{
Byte[] recv;
recv = client.Receive(ref endpoint);
for (int i = 0; i < 4; i++)
{
switchSignal[i] = recv[i + 2];
}
switchList.Add(switchSignal);
elapsetime.Restart();//计时开始
if (switchDataWrite == true)
{
String switchDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "switchData");
string stringData = "0x" + BitConverter.ToString(recv).Replace("-", " 0x").ToLower();
stringData = DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss:fff") + " " + stringData;
StrWrite.strWrite(stringData, switchDataPath, "switchDataFile.txt");
}
elapsetime.Stop();//计时结束
Console.WriteLine("接收数据耗时:" + elapsetime.ElapsedMilliseconds.ToString("0000"));
}
}
catch (Exception ex)
{
LogWrite.logWrite(ex.Message, ex.StackTrace);
}
}
复制
第四部:在StatusViewWindow窗口定义一个10毫秒的定时器,每隔十毫秒从主窗体获取一次光电开关的数据并显示到listBox1中;
<Window x:Class="thzSoftware.StatusViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:thzSoftware"
mc:Ignorable="d"
Title="StatusViewWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid ShowGridLines="False" Background="LightCyan" >
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox Name="listBox1" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Background="LightGray"/>
<Button Name="btnCtrl1" Content="开始采集" FontSize="30" Grid.Row="0" Grid.Column="0" Background="LightGray" Click="BtnCtrl1_Click"/>
<Button Name="btnCtrl2" Content="停止采集" FontSize="30" Grid.Row="1" Grid.Column="0" Background="LightGray" Click="BtnCtrl2_Click"/>
</Grid>
</Window>
复制
using System;
using System.Windows;
using System.Windows.Threading;
namespace thzSoftware
{
/// <summary>
/// StatusViewWindow.xaml 的交互逻辑
/// </summary>
public partial class StatusViewWindow : Window
{
public StatusViewWindow()
{
InitializeComponent();
}
Byte[] switchSignal = new byte[4];
DispatcherTimer StatusViewTimer;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
StatusViewTimer = new DispatcherTimer();
StatusViewTimer.Interval = TimeSpan.FromMilliseconds(10);
StatusViewTimer.Tick += StatusViewTimer_Tick;//加载事件,敲tab键事件框架可以自己出来
}
private void StatusViewTimer_Tick(object sender, EventArgs e)
{
switchSignal = thzModel.DataProcess.switchSignal;
string stringData = "0x" + BitConverter.ToString(switchSignal).Replace("-", " 0x").ToLower();
stringData = DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss:fff") + " " + stringData;
listBox1.Items.Add(stringData);
}
private void BtnCtrl1_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
StatusViewTimer.Start();
}
private void BtnCtrl2_Click(object sender, RoutedEventArgs e)
{
StatusViewTimer.Stop();
}
}
}
复制
运行结果如下;
欢迎关注公众号: dotnet编程大全
技术群: 需要进技术群的添加小编微信mm1552923,备注:加群;
文章转载自DotNet工控上位机编程,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年4月中国数据库流行度排行榜:OB高分复登顶,崖山稳驭撼十强
墨天轮编辑部
2262次阅读
2025-04-09 15:33:27
数据库国产化替代深化:DBA的机遇与挑战
代晓磊
1045次阅读
2025-04-27 16:53:22
2025年3月国产数据库大事记
墨天轮编辑部
958次阅读
2025-04-03 15:21:16
2025年3月国产数据库中标情况一览:TDSQL大单622万、GaussDB大单581万……
通讯员
657次阅读
2025-04-10 15:35:48
数据库,没有关税却有壁垒
多明戈教你玩狼人杀
524次阅读
2025-04-11 09:38:42
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
507次阅读
2025-04-14 09:40:20
最近我为什么不写评论国产数据库的文章了
白鳝的洞穴
464次阅读
2025-04-07 09:44:54
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
419次阅读
2025-04-17 17:02:24
天津市政府数据库框采结果公布,7家数据库产品入选!
通讯员
396次阅读
2025-04-10 12:32:35
2025年4月国产数据库中标情况一览:4个千万元级项目,GaussDB与OceanBase大放异彩!
通讯员
389次阅读
2025-04-30 15:24:06