inp register group read still doesn`t work

This commit is contained in:
nikzori
2024-12-24 09:42:45 +03:00
parent 86412fff02
commit 604cb9d459
2 changed files with 30 additions and 19 deletions

View File

@@ -122,8 +122,7 @@ namespace Gidrolock_Modbus_Scanner
if (Convert.ToBoolean(chbox.Value)) if (Convert.ToBoolean(chbox.Value))
{ {
Console.WriteLine("Polling for " + device.entries[activeEntryIndex].name); Console.WriteLine("Polling for " + device.entries[activeEntryIndex].name);
await PollForEntry(entries[activeEntryIndex]); await PollForEntry(entries[activeEntryIndex]).ContinueWith(_ => Task.Delay(150));
await Task.Delay(150);
} }
else //need to skip multiple dgv entries without accidentaly skipping entries else //need to skip multiple dgv entries without accidentaly skipping entries
{ {
@@ -143,8 +142,6 @@ namespace Gidrolock_Modbus_Scanner
activeEntryIndex = 0; activeEntryIndex = 0;
if (activeDGVIndex >= DGV_Device.RowCount) if (activeDGVIndex >= DGV_Device.RowCount)
activeDGVIndex = 0; activeDGVIndex = 0;
Console.WriteLine("entry index: " + activeEntryIndex + "; dgv index: " + activeDGVIndex);
} }
} }
} }
@@ -196,9 +193,9 @@ namespace Gidrolock_Modbus_Scanner
else else
{ {
try { DGV_Device.Rows[activeEntryIndex].Cells[2].Value = e.Data[0] > 0x00 ? entries[activeEntryIndex].valueParse["true"] : entries[activeEntryIndex].valueParse["false"]; } try { DGV_Device.Rows[activeEntryIndex].Cells[2].Value = e.Data[0] > 0x00 ? entries[activeEntryIndex].valueParse["true"] : entries[activeEntryIndex].valueParse["false"]; }
catch catch (Exception err)
{ {
Console.WriteLine("Value parsing error for bool entry: " + entries[activeEntryIndex].name); MessageBox.Show("Value parsing error for bool entry: " + entries[activeEntryIndex].name + "; " + err.Message);
DGV_Device.Rows[activeEntryIndex].Cells[2].Value = e.Data[0] > 0x00 ? "true" : "false"; DGV_Device.Rows[activeEntryIndex].Cells[2].Value = e.Data[0] > 0x00 ? "true" : "false";
} }
} }
@@ -239,17 +236,23 @@ namespace Gidrolock_Modbus_Scanner
{ {
DGV_Device.Rows[activeDGVIndex].Cells[2].Value = entries[activeEntryIndex].valueParse[value.ToString()]; DGV_Device.Rows[activeDGVIndex].Cells[2].Value = entries[activeEntryIndex].valueParse[value.ToString()];
} }
catch { DGV_Device.Rows[activeDGVIndex].Cells[2].Value = value; Console.WriteLine("Error parsing uint value at address: " + entries[activeEntryIndex].address); } catch (Exception err)
{
DGV_Device.Rows[activeDGVIndex].Cells[2].Value = value; MessageBox.Show("Error parsing uint value at address: " + entries[activeEntryIndex].address + "; " + err.Message, "uint16 parse");
}
} }
activeDGVIndex++; activeDGVIndex++;
} }
else // value group else // value group
{
try
{ {
List<ushort> values = new List<ushort>(); List<ushort> values = new List<ushort>();
for (int i = 0; i < dbc; i += 2) for (int i = 0; i < dbc - 2; i += 2)
{ {
ushort s = BitConverter.ToUInt16(e.Data, i); ushort s = BitConverter.ToUInt16(e.Data, i);
Console.WriteLine("ushort value: " + s);
values.Add(s); values.Add(s);
} }
for (int i = 0; i < entries[activeEntryIndex].labels.Count; i++) for (int i = 0; i < entries[activeEntryIndex].labels.Count; i++)
@@ -258,6 +261,12 @@ namespace Gidrolock_Modbus_Scanner
activeDGVIndex++; activeDGVIndex++;
} }
} }
catch (Exception err)
{
DGV_Device.Rows[activeDGVIndex].Cells[2].Value = value; MessageBox.Show("Error parsing uint value at address: " + entries[activeEntryIndex].address + "; " + err.Message, "uint16 group req parse");
}
}
break; break;
case ("uint32"): case ("uint32"):
@@ -291,7 +300,9 @@ namespace Gidrolock_Modbus_Scanner
//MessageBox.Show("Получен ответ от устройства: " + dataCleaned, "Успех", MessageBoxButtons.OK); //MessageBox.Show("Получен ответ от устройства: " + dataCleaned, "Успех", MessageBoxButtons.OK);
port.DiscardInBuffer(); port.DiscardInBuffer();
} }
catch { return; } catch (Exception err) {
MessageBox.Show(err.Message, "Publish response error");
}
} }
if (activeDGVIndex >= DGV_Device.Rows.Count) if (activeDGVIndex >= DGV_Device.Rows.Count)

View File

@@ -241,7 +241,7 @@ namespace Gidrolock_Modbus_Scanner
{ {
byte[] message = new byte[port.BytesToRead]; byte[] message = new byte[port.BytesToRead];
port.Read(message, 0, port.BytesToRead); port.Read(message, 0, port.BytesToRead);
Console.WriteLine("Incoming message: " + ByteArrayToString(message)); Console.WriteLine("Incoming message: " + ByteArrayToString(message, false));
if (message[1] <= 0x04) // read functions if (message[1] <= 0x04) // read functions
{ {
Console.WriteLine("It's a read message"); Console.WriteLine("It's a read message");
@@ -263,7 +263,7 @@ namespace Gidrolock_Modbus_Scanner
} }
catch (Exception err) catch (Exception err)
{ {
MessageBox.Show(err.Message); MessageBox.Show(err.Message, "Modbus message reception error");
} }
port.DiscardInBuffer(); port.DiscardInBuffer();
} }