页面调试
This commit is contained in:
@@ -21,7 +21,7 @@ namespace 头罩视野
|
|||||||
|
|
||||||
public partial class MainWindow : Window
|
public partial class MainWindow : Window
|
||||||
{
|
{
|
||||||
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
Background="#F5F7FA"
|
Background="#F5F7FA"
|
||||||
d:DesignHeight="768" d:DesignWidth="1024"
|
d:DesignHeight="768" d:DesignWidth="1024"
|
||||||
Title="PageTest" Loaded="Page_Loaded" Unloaded="Page_Unloaded">
|
Title="PageTest" Loaded="Page_Loaded" Unloaded="Page_Unloaded" >
|
||||||
<Page.Resources>
|
<Page.Resources>
|
||||||
<!-- 标题样式 -->
|
<!-- 标题样式 -->
|
||||||
<Style x:Key="MainTitleStyle" TargetType="TextBlock">
|
<Style x:Key="MainTitleStyle" TargetType="TextBlock">
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ namespace 头罩视野.Views
|
|||||||
public PageTest()
|
public PageTest()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
System.Diagnostics.Debug.WriteLine("页面加载了!111111111");
|
||||||
_timer = InitDispatcherTimer();
|
_timer = InitDispatcherTimer();
|
||||||
// 2. 初始化定时器:500毫秒 执行一次
|
// 2. 初始化定时器:500毫秒 执行一次
|
||||||
|
|
||||||
@@ -80,7 +80,8 @@ namespace 头罩视野.Views
|
|||||||
{
|
{
|
||||||
((Button)sender).CaptureMouse();
|
((Button)sender).CaptureMouse();
|
||||||
_isPressing = true;
|
_isPressing = true;
|
||||||
await Task.Run(() => {
|
await Task.Run(() =>
|
||||||
|
{
|
||||||
//重新占位写入
|
//重新占位写入
|
||||||
_modbusMaster.WriteSingleCoilAsync(1, 10, true);
|
_modbusMaster.WriteSingleCoilAsync(1, 10, true);
|
||||||
Task.Delay(100);
|
Task.Delay(100);
|
||||||
@@ -102,7 +103,8 @@ namespace 头罩视野.Views
|
|||||||
((Button)sender).CaptureMouse();
|
((Button)sender).CaptureMouse();
|
||||||
_isPressing1 = true;
|
_isPressing1 = true;
|
||||||
|
|
||||||
await Task.Run(() => {
|
await Task.Run(() =>
|
||||||
|
{
|
||||||
//重新占位写入
|
//重新占位写入
|
||||||
_modbusMaster.WriteSingleCoilAsync(1, 11, true);
|
_modbusMaster.WriteSingleCoilAsync(1, 11, true);
|
||||||
System.Diagnostics.Debug.WriteLine("正传start");
|
System.Diagnostics.Debug.WriteLine("正传start");
|
||||||
@@ -123,7 +125,7 @@ namespace 头罩视野.Views
|
|||||||
private void Button_Click_Test(object sender, RoutedEventArgs e)
|
private void Button_Click_Test(object sender, RoutedEventArgs e)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
testTimer.Start();
|
testTimer.Start();
|
||||||
ma.BtnClickFunction(Function.ButtonType.复归型, 101);
|
ma.BtnClickFunction(Function.ButtonType.复归型, 101);
|
||||||
}
|
}
|
||||||
@@ -164,6 +166,8 @@ namespace 头罩视野.Views
|
|||||||
private void Page_Unloaded(object sender, RoutedEventArgs e)
|
private void Page_Unloaded(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
testTimer?.Stop();
|
testTimer?.Stop();
|
||||||
|
_timer?.Stop();
|
||||||
|
|
||||||
}
|
}
|
||||||
//停止btn
|
//停止btn
|
||||||
private void Button_Click_Stop(object sender, RoutedEventArgs e)
|
private void Button_Click_Stop(object sender, RoutedEventArgs e)
|
||||||
@@ -205,7 +209,7 @@ namespace 头罩视野.Views
|
|||||||
{
|
{
|
||||||
var timer = new DispatcherTimer
|
var timer = new DispatcherTimer
|
||||||
{
|
{
|
||||||
Interval = TimeSpan.FromMilliseconds(100)
|
Interval = TimeSpan.FromMilliseconds(500)
|
||||||
};
|
};
|
||||||
timer.Tick += async (s, e) =>
|
timer.Tick += async (s, e) =>
|
||||||
{
|
{
|
||||||
@@ -228,16 +232,15 @@ namespace 头罩视野.Views
|
|||||||
// 创建任务列表
|
// 创建任务列表
|
||||||
var tasks = new List<Task>
|
var tasks = new List<Task>
|
||||||
{
|
{
|
||||||
ReadAndUpdateFloatAsync(200, 2, fbspeed, "F2", "°"),
|
//ReadAndUpdateFloatAsync(200, 2, fbspeed, "F2", "°"),
|
||||||
ReadAndUpdateFloatAsync(202, 2, dqangle, "F2", "°"),
|
//ReadAndUpdateFloatAsync(202, 2, dqangle, "F2", "°"),
|
||||||
ReadAndUpdateFloatAsync(310, 2, zdangle, "F2", "°/S"),
|
//ReadAndUpdateFloatAsync(204, 2, zmsyarea, "F2", "cm²"),
|
||||||
|
//ReadAndUpdateFloatAsync(206 ,2, xfsyarea, "F2", " "),
|
||||||
ReadAndUpdateFloatAsync(204, 2, zmsyarea, "F2", "cm²"),
|
//ReadAndUpdateFloatAsync(208, 2, smsyarea, "F2", "cm²"),
|
||||||
ReadAndUpdateFloatAsync(208, 2, smsyarea, "F2", "cm²"),
|
//ReadAndUpdateFloatAsync(210 ,2, ymsyarea, "F2", " "),
|
||||||
|
ReadAndUpdateFloatRangeAsync(200, 12, "F2", "°"),
|
||||||
ReadAndUpdateFloatAsync(424 ,2, kbsyarea, "F2", "cm²"),
|
ReadAndUpdateFloatAsync(424 ,2, kbsyarea, "F2", "cm²"),
|
||||||
|
ReadAndUpdateFloatAsync(310, 2, zdangle, "F2", "°/S"),
|
||||||
ReadAndUpdateFloatAsync(210 ,2, ymsyarea, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(206 ,2, xfsyarea, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(430 ,2, sybhl, "F2", " "),
|
ReadAndUpdateFloatAsync(430 ,2, sybhl, "F2", " "),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -274,6 +277,53 @@ namespace 头罩视野.Views
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async Task ReadAndUpdateFloatRangeAsync(int address, int length, string format, string unit)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ushort[] registers = await Task.Run(async () =>
|
||||||
|
await _modbusMaster?.ReadHoldingRegistersAsync(1, (ushort)address, (ushort)length)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (registers != null && registers.Length >= 2)
|
||||||
|
{
|
||||||
|
float value = c.UshortToFloat(registers[1], registers[0]);
|
||||||
|
float value2 = c.UshortToFloat(registers[3], registers[2]);
|
||||||
|
float value3 = c.UshortToFloat(registers[5], registers[4]);
|
||||||
|
float value4 = c.UshortToFloat(registers[7], registers[6]);
|
||||||
|
float value5 = c.UshortToFloat(registers[9], registers[8]);
|
||||||
|
float value6 = c.UshortToFloat(registers[11], registers[10]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//ReadAndUpdateFloatAsync(200, 2, fbspeed, "F2", "°"),
|
||||||
|
//ReadAndUpdateFloatAsync(202, 2, dqangle, "F2", "°"),
|
||||||
|
//ReadAndUpdateFloatAsync(204, 2, zmsyarea, "F2", "cm²"),
|
||||||
|
//ReadAndUpdateFloatAsync(206 ,2, xfsyarea, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(208, 2, smsyarea, "F2", "cm²"),
|
||||||
|
//ReadAndUpdateFloatAsync(210 ,2, ymsyarea, "F2", " "),
|
||||||
|
|
||||||
|
Dispatcher.Invoke(() =>
|
||||||
|
|
||||||
|
{
|
||||||
|
fbspeed.Text = value.ToString(format) + unit;
|
||||||
|
dqangle.Text = value2.ToString(format) + unit;
|
||||||
|
zmsyarea.Text = value3.ToString(format) + unit;
|
||||||
|
xfsyarea.Text = value4.ToString(format) + unit;
|
||||||
|
smsyarea.Text = value5.ToString(format) + unit;
|
||||||
|
ymsyarea.Text = value6.ToString(format) + unit;
|
||||||
|
//control.Text = value.ToString(format) + unit);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"读取地址{address}失败:{ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task ReadAndUpdateFloatAsync(int address, int length, System.Windows.Controls.TextBlock control, string format, string unit)
|
private async Task ReadAndUpdateFloatAsync(int address, int length, System.Windows.Controls.TextBlock control, string format, string unit)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -349,15 +399,21 @@ namespace 头罩视野.Views
|
|||||||
//错误信息提示
|
//错误信息提示
|
||||||
private void ShowError(string msg) => MessageBox.Show(msg, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
private void ShowError(string msg) => MessageBox.Show(msg, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||||
private void GoHome(object s, RoutedEventArgs e) => NavigationService.Content = null;
|
private void GoHome(object s, RoutedEventArgs e) => NavigationService.Content = null;
|
||||||
private void GoTest(object s, RoutedEventArgs e) => NavigationService.Content = new Views.PageTest();
|
private void GoTest(object s, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
_timer.Stop();
|
||||||
|
NavigationService.Content = new Views.PageTest();
|
||||||
|
}
|
||||||
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
|
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
|
||||||
private void GoView(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordPage();
|
private void GoView(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordPage();
|
||||||
|
|
||||||
private void Page_Loaded(object sender, RoutedEventArgs e)
|
private void Page_Loaded(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("页面加载了!112222222");
|
||||||
_timer.Start();
|
_timer.Start();
|
||||||
ma = new Function(_modbusMaster);
|
ma = new Function(_modbusMaster);
|
||||||
c = new DataChange();
|
c = new DataChange();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,13 +63,8 @@ namespace 头罩视野.Views
|
|||||||
ChEight = 55,
|
ChEight = 55,
|
||||||
});
|
});
|
||||||
|
|
||||||
// 验证:输出列表数据条数(运行后看控制台,确认有2条)
|
|
||||||
Console.WriteLine($"数据条数:{list.Count}");
|
//System.Diagnostics.Debug.WriteLine("页面加载了!112222222");
|
||||||
Console.WriteLine($"数据条数:1111");
|
|
||||||
|
|
||||||
// ########### 加这行调试输出 ###########
|
|
||||||
Console.WriteLine("Hello, World!");
|
|
||||||
System.Diagnostics.Debug.WriteLine("页面加载了!112222222");
|
|
||||||
|
|
||||||
// ####################################
|
// ####################################
|
||||||
// 绑定到 DataGrid 显示
|
// 绑定到 DataGrid 显示
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
d:DesignHeight="769" d:DesignWidth="1024"
|
d:DesignHeight="769" d:DesignWidth="1024"
|
||||||
Background="#F5F7FA"
|
Background="#F5F7FA"
|
||||||
Title="VisiData" Loaded="Page_Loaded" >
|
Title="VisiData" Loaded="Page_Loaded" Unloaded="Page_Unloaded" >
|
||||||
<Page.Resources>
|
<Page.Resources>
|
||||||
<!-- 标题样式 -->
|
<!-- 标题样式 -->
|
||||||
<Style x:Key="MainTitleStyle" TargetType="TextBlock">
|
<Style x:Key="MainTitleStyle" TargetType="TextBlock">
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ namespace 头罩视野.Views
|
|||||||
{
|
{
|
||||||
var timer = new DispatcherTimer
|
var timer = new DispatcherTimer
|
||||||
{
|
{
|
||||||
Interval = TimeSpan.FromMilliseconds(100)
|
Interval = TimeSpan.FromMilliseconds(500)
|
||||||
};
|
};
|
||||||
timer.Tick += async (s, e) =>
|
timer.Tick += async (s, e) =>
|
||||||
{
|
{
|
||||||
@@ -144,30 +144,28 @@ namespace 头罩视野.Views
|
|||||||
// 创建任务列表
|
// 创建任务列表
|
||||||
var tasks = new List<Task>
|
var tasks = new List<Task>
|
||||||
{
|
{
|
||||||
ReadAndUpdateFloatAsync(350, 1, sdtsj1, "F2", ""),
|
//ReadAndUpdateFloatAsync(350, 1, c, "F2", ""),
|
||||||
ReadAndUpdateFloatAsync(351, 1, sdtsj2, "F2", ""),
|
//ReadAndUpdateFloatAsync(351, 1, sdtsj2, "F2", ""),
|
||||||
ReadAndUpdateFloatAsync(352, 1, sdtsj3, "F2", ""),
|
//ReadAndUpdateFloatAsync(352, 1, sdtsj3, "F2", ""),
|
||||||
ReadAndUpdateFloatAsync(353, 1, sdtsj4, "F2", ""),
|
//ReadAndUpdateFloatAsync(353, 1, sdtsj4, "F2", ""),
|
||||||
ReadAndUpdateFloatAsync(354, 1, sdtsj5, "F2", ""),
|
//ReadAndUpdateFloatAsync(354, 1, sdtsj5, "F2", ""),
|
||||||
|
//ReadAndUpdateFloatAsync(355, 1, xdtsj1, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(356, 1, xdtsj2, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(357, 1, xdtsj3, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(358, 1, xdtsj4, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(359, 1, xdtsj5, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(360, 1, zdtsj1, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(361, 1, zdtsj2, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(362, 1, zdtsj3, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(363, 1, zdtsj4, "F2", " "),
|
||||||
|
//ReadAndUpdateFloatAsync(364, 1, zdtsj5, "F2", " "),
|
||||||
|
|
||||||
ReadAndUpdateFloatAsync(212, 2, zymjxs, "F2", ""),
|
ReadAndUpdateFloatAsync(212, 2, zymjxs, "F2", ""),
|
||||||
ReadAndUpdateFloatAsync(214, 2, yymjxs, "F2", ""),
|
ReadAndUpdateFloatAsync(214, 2, yymjxs, "F2", ""),
|
||||||
ReadAndUpdateFloatAsync(428, 2, bcjzxs, "F2", ""),
|
ReadAndUpdateFloatAsync(428, 2, bcjzxs, "F2", ""),
|
||||||
|
ReadAndUpdateFloatAsync(340,2, fwbc, "F2", " "),
|
||||||
ReadAndUpdateFloatAsync(355, 1, xdtsj1, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(356, 1, xdtsj2, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(357, 1, xdtsj3, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(358, 1, xdtsj4, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(359, 1, xdtsj5, "F2", " "),
|
|
||||||
|
|
||||||
ReadAndUpdateFloatAsync(360, 1, zdtsj1, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(361, 1, zdtsj2, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(362, 1, zdtsj3, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(363, 1, zdtsj4, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(364, 1, zdtsj5, "F2", " "),
|
|
||||||
|
|
||||||
ReadAndUpdateFloatAsync(340,2, fwbc, "F2", " "),
|
|
||||||
ReadAndUpdateFloatAsync(330,2, djxw, "F2", " "),
|
ReadAndUpdateFloatAsync(330,2, djxw, "F2", " "),
|
||||||
|
ReadAndUpdateFloatRangeAsync(350, 15, "F2", ""),
|
||||||
};
|
};
|
||||||
|
|
||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
@@ -177,6 +175,44 @@ namespace 头罩视野.Views
|
|||||||
ShowError($"读取数据失败:{ex.Message}");
|
ShowError($"读取数据失败:{ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private async Task ReadAndUpdateFloatRangeAsync(int address, int length, string format, string unit)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ushort[] registers = await Task.Run(async () =>
|
||||||
|
await _modbusMaster?.ReadHoldingRegistersAsync(1, (ushort)address, (ushort)length)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (registers != null && registers.Length >= 2)
|
||||||
|
{
|
||||||
|
var controls = new TextBox[]
|
||||||
|
{
|
||||||
|
sdtsj1, sdtsj2, sdtsj3, sdtsj4, sdtsj5,
|
||||||
|
xdtsj1, xdtsj2, xdtsj3, xdtsj4, xdtsj5,
|
||||||
|
zdtsj1, zdtsj2, zdtsj3, zdtsj4, zdtsj5
|
||||||
|
};
|
||||||
|
|
||||||
|
// 一次在UI线程批量更新,避免多次Invoke阻塞
|
||||||
|
await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||||
|
{
|
||||||
|
for (int i = 0; i < controls.Length; i++)
|
||||||
|
{
|
||||||
|
// 因为你这里是1个寄存器=1个值,所以直接用registers[i]
|
||||||
|
controls[i].Text = registers[i].ToString(format);
|
||||||
|
}
|
||||||
|
}), DispatcherPriority.Background);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"读取地址{address}失败:{ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 地址, 根据格式显示字符长度 32:2 16:1 ,绑定页面的name 值,F2 保留两位小数,单位
|
// 地址, 根据格式显示字符长度 32:2 16:1 ,绑定页面的name 值,F2 保留两位小数,单位
|
||||||
private async Task ReadAndUpdateFloatAsync(int address, int length, System.Windows.Controls.TextBox control, string format, string unit)
|
private async Task ReadAndUpdateFloatAsync(int address, int length, System.Windows.Controls.TextBox control, string format, string unit)
|
||||||
{
|
{
|
||||||
@@ -269,5 +305,12 @@ namespace 头罩视野.Views
|
|||||||
ma = new Function(_modbusMaster);
|
ma = new Function(_modbusMaster);
|
||||||
c = new DataChange();
|
c = new DataChange();
|
||||||
}
|
}
|
||||||
|
private void Page_Unloaded(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
_timer?.Stop();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user