From 604cb9d4598dedf4bd3494b299735c9f8a2ca0f5 Mon Sep 17 00:00:00 2001 From: nikzori Date: Tue, 24 Dec 2024 09:42:45 +0300 Subject: [PATCH] inp register group read still doesn`t work --- Datasheet.cs | 45 ++++++++++++++++++++++++++++----------------- Modbus.cs | 4 ++-- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Datasheet.cs b/Datasheet.cs index c35eb89..3717a8b 100644 --- a/Datasheet.cs +++ b/Datasheet.cs @@ -122,8 +122,7 @@ namespace Gidrolock_Modbus_Scanner if (Convert.ToBoolean(chbox.Value)) { Console.WriteLine("Polling for " + device.entries[activeEntryIndex].name); - await PollForEntry(entries[activeEntryIndex]); - await Task.Delay(150); + await PollForEntry(entries[activeEntryIndex]).ContinueWith(_ => Task.Delay(150)); } else //need to skip multiple dgv entries without accidentaly skipping entries { @@ -143,8 +142,6 @@ namespace Gidrolock_Modbus_Scanner activeEntryIndex = 0; if (activeDGVIndex >= DGV_Device.RowCount) activeDGVIndex = 0; - - Console.WriteLine("entry index: " + activeEntryIndex + "; dgv index: " + activeDGVIndex); } } } @@ -161,7 +158,7 @@ namespace Gidrolock_Modbus_Scanner var send = await Modbus.ReadRegAsync(port, slaveID, (FunctionCode)entry.registerType, entry.address, entry.length); isAwaitingResponse = true; - Task delay = Task.WhenAny(Task.Delay(timeout), Task.Run( () => { while (isAwaitingResponse) { } return; })).ContinueWith((t) => + Task delay = Task.WhenAny(Task.Delay(timeout), Task.Run(() => { while (isAwaitingResponse) { } return; })).ContinueWith((t) => { if (isAwaitingResponse) { @@ -181,7 +178,7 @@ namespace Gidrolock_Modbus_Scanner try { if (entries[activeEntryIndex].readOnce) - { + { DataGridViewCheckBoxCell chbox = DGV_Device.Rows[activeDGVIndex].Cells[4] as DataGridViewCheckBoxCell; chbox.Value = false; } @@ -196,9 +193,9 @@ namespace Gidrolock_Modbus_Scanner else { 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"; } } @@ -239,26 +236,38 @@ namespace Gidrolock_Modbus_Scanner { 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++; } else // value group { - List values = new List(); - for (int i = 0; i < dbc; i += 2) + try { - ushort s = BitConverter.ToUInt16(e.Data, i); - values.Add(s); + List values = new List(); + for (int i = 0; i < dbc - 2; i += 2) + { + ushort s = BitConverter.ToUInt16(e.Data, i); + Console.WriteLine("ushort value: " + s); + values.Add(s); + } + for (int i = 0; i < entries[activeEntryIndex].labels.Count; i++) + { + DGV_Device.Rows[activeDGVIndex].Cells[2].Value = values[i]; + activeDGVIndex++; + } } - for (int i = 0; i < entries[activeEntryIndex].labels.Count; i++) + catch (Exception err) { - DGV_Device.Rows[activeDGVIndex].Cells[2].Value = values[i]; - activeDGVIndex++; + 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; case ("uint32"): Array.Reverse(e.Data); @@ -291,7 +300,9 @@ namespace Gidrolock_Modbus_Scanner //MessageBox.Show("Получен ответ от устройства: " + dataCleaned, "Успех", MessageBoxButtons.OK); port.DiscardInBuffer(); } - catch { return; } + catch (Exception err) { + MessageBox.Show(err.Message, "Publish response error"); + } } if (activeDGVIndex >= DGV_Device.Rows.Count) diff --git a/Modbus.cs b/Modbus.cs index 1424bb8..1c7c7b5 100644 --- a/Modbus.cs +++ b/Modbus.cs @@ -241,7 +241,7 @@ namespace Gidrolock_Modbus_Scanner { byte[] message = new byte[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 { Console.WriteLine("It's a read message"); @@ -263,7 +263,7 @@ namespace Gidrolock_Modbus_Scanner } catch (Exception err) { - MessageBox.Show(err.Message); + MessageBox.Show(err.Message, "Modbus message reception error"); } port.DiscardInBuffer(); }