This commit is contained in:
nikzori
2024-12-16 11:35:20 +03:00
parent d95ea2b26a
commit 5ca452031a
5 changed files with 582 additions and 499 deletions

View File

@@ -15,6 +15,7 @@ namespace Gidrolock_Modbus_Scanner
{
public partial class Datasheet : Form
{
bool isPolling = false;
bool isAwaitingResponse = false;
bool isProcessingResponse = false;
byte[] message = new byte[255];
@@ -36,6 +37,9 @@ namespace Gidrolock_Modbus_Scanner
InitializeComponent();
Label_DeviceName.Text = device.name;
Label_Description.Text = device.description;
DGV_Device.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DGV_Device.MultiSelect = false;
DGV_Device.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
@@ -72,10 +76,16 @@ namespace Gidrolock_Modbus_Scanner
{
while (!closed)
{
for (int i = 0; i < entries.Count; i++)
if (isPolling)
{
activeEntryIndex = i;
await PollForEntry(entries[i]);
for (int i = 0; i < entries.Count; i++)
{
// TODO: figure out how to get the checkbox value out of DataGridView cells;
// holy fuck DGV is awful
activeEntryIndex = i;
await PollForEntry(entries[i]);
}
}
}
}
@@ -112,33 +122,26 @@ namespace Gidrolock_Modbus_Scanner
isAwaitingResponse = false;
try
{
byte length = e.message[2];
Console.WriteLine("Data length is:" + length);
byte[] data = new byte[length];
for (int i = 0; i < length; i++)
data[i] = e.message[i + 3];
Console.WriteLine("Data after processing in Datasheet.cs: " + Modbus.ByteArrayToString(data));
{
Console.WriteLine("Data after processing in Datasheet.cs: " + Modbus.ByteArrayToString(e.Data));
switch (entries[activeEntryIndex].dataType)
{
case ("bool"):
DGV_Device.Rows[activeEntryIndex].Cells[2].Value = data[0] > 0x00 ? "true" : "false";
DGV_Device.Rows[activeEntryIndex].Cells[2].Value = e.Data[0] > 0x00 ? "true" : "false";
break;
case ("uint16"):
Array.Reverse(data); // BitConverter.ToUInt is is little endian, I guess, so we need to flip the array
ushort test = BitConverter.ToUInt16(data, 0);
Array.Reverse(e.Data); // BitConverter.ToUInt is is little endian, I guess, so we need to flip the array
ushort test = BitConverter.ToUInt16(e.Data, 0);
Console.WriteLine("ushort parsed value: " + test);
DGV_Device.Rows[activeEntryIndex].Cells[2].Value = test;
break;
case ("uint32"):
Array.Reverse(data);
DGV_Device.Rows[activeEntryIndex].Cells[2].Value = BitConverter.ToUInt32(data, 0);
Array.Reverse(e.Data);
DGV_Device.Rows[activeEntryIndex].Cells[2].Value = BitConverter.ToUInt32(e.Data, 0);
break;
case ("utf8"):
DGV_Device.Rows[activeEntryIndex].Cells[2].Value = System.Text.Encoding.UTF8.GetString(data);
DGV_Device.Rows[activeEntryIndex].Cells[2].Value = System.Text.Encoding.UTF8.GetString(e.Data);
break;
default:
MessageBox.Show("Wrong data type set for entry " + entries[activeEntryIndex].name);
@@ -156,6 +159,12 @@ namespace Gidrolock_Modbus_Scanner
}
}
private void Button_StartStop_Click(object sender, EventArgs e)
{
isPolling = !isPolling;
Button_StartStop.Text = (isPolling ? "Стоп" : "Старт");
}
}
}