switched to Stopwatch for timers for more reliability

This commit is contained in:
nikzori
2025-03-06 10:16:30 +03:00
parent 80bf64b7d5
commit e00e96bea7
5 changed files with 235 additions and 91 deletions

18
Main.cs
View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Reflection;
using System.Diagnostics;
namespace Gidrolock_Modbus_Scanner
{
@@ -20,6 +22,7 @@ namespace Gidrolock_Modbus_Scanner
DateTime dateTime;
Datasheet datasheet;
Stopwatch stopwatch = new Stopwatch();
#region Initialization
public App()
{
@@ -77,8 +80,10 @@ namespace Gidrolock_Modbus_Scanner
}
#endregion
private async void ButtonConnect_Click(object sender, EventArgs e)
{
if (cBoxPorts.SelectedItem.ToString() == "COM1")
{
DialogResult res = MessageBox.Show("Выбран серийный порт COM1, который обычно является портом PS/2 или RS-232, не подключенным к Modbus устройству. Продолжить?", "Внимание", MessageBoxButtons.OKCancel);
@@ -123,17 +128,16 @@ namespace Gidrolock_Modbus_Scanner
latestMessage = null;
isAwaitingResponse = true;
var send = Modbus.ReadRegAsync((byte)upDownModbusID.Value, FunctionCode.ReadInput, 200, 6);
await Task.Delay(2000).ContinueWith(_ =>
stopwatch.Restart();
while (isAwaitingResponse)
{
if (isAwaitingResponse)
if (stopwatch.ElapsedMilliseconds > 1000)
{
isAwaitingResponse = false;
MessageBox.Show("Истекло время ожидания ответа от устройства.");
Console.WriteLine("Response timed out.");
break;
}
return;
});
}
while (isAwaitingResponse) { continue; }
if (latestMessage is null)
return;
if (latestMessage.Status == ModbusStatus.Error)