Skip to content

Commit

Permalink
Finishing up on Modbus Registers editor backend logic. New commands, …
Browse files Browse the repository at this point in the history
…improved logic handling.
  • Loading branch information
JHirniak committed Nov 11, 2023
1 parent 5b72337 commit 4aeab3d
Show file tree
Hide file tree
Showing 41 changed files with 5,770 additions and 2,062 deletions.
6 changes: 6 additions & 0 deletions Serial Monitor/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
<setting name="PRG_BOL_LimitExecution1ms" serializeAs="String">
<value>False</value>
</setting>
<setting name="THM_COL_MouseOverForeColor" serializeAs="String">
<value>LightGray</value>
</setting>
<setting name="THM_COL_MouseDownForeColor" serializeAs="String">
<value>DimGray</value>
</setting>
</Serial_Monitor.Properties.Settings>
</userSettings>
</configuration>
37 changes: 30 additions & 7 deletions Serial Monitor/Classes/EnumManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
using System.Threading.Tasks;
using static Serial_Monitor.Classes.Enums.ModbusEnums;

namespace Serial_Monitor.Classes
{
namespace Serial_Monitor.Classes {
public static class EnumManager {

#region Modbus Data Size
Expand Down Expand Up @@ -145,20 +144,44 @@ public static DataSelection ModbusStringToDataSelection(string Input) {
else if (Input == "mbTypeRegInput") {
return DataSelection.ModbusDataInputRegisters;
}
else if (Input == "C") {
return DataSelection.ModbusDataCoils;
}
else if (Input == "D") {
return DataSelection.ModbusDataDiscreteInputs;
}
else if (Input == "H") {
return DataSelection.ModbusDataHoldingRegisters;
}
else if (Input == "I") {
return DataSelection.ModbusDataInputRegisters;
}
return DataSelection.ModbusDataCoils;
}
public static StringPair ModbusDataSelectionToString(DataSelection Input) {
public static StringPair ModbusDataSelectionToString(DataSelection Input, bool UseShortenCode = false) {
if (Input == DataSelection.ModbusDataCoils) {
return new StringPair("Coils", "mbTypeCoils");
if (UseShortenCode == false) {
return new StringPair("Coils", "mbTypeCoils");
}
return new StringPair("Coils", "C");
}
else if (Input == DataSelection.ModbusDataDiscreteInputs) {
return new StringPair("Discrete", "mbTypeDiscrete");
if (UseShortenCode == false) {
return new StringPair("Discrete", "mbTypeDiscrete");
}
return new StringPair("Discrete", "D");
}
else if (Input == DataSelection.ModbusDataHoldingRegisters) {
return new StringPair("Holding Registers", "mbTypeRegHolding");
if (UseShortenCode == false) {
return new StringPair("Holding Registers", "mbTypeRegHolding");
}
return new StringPair("Holding Registers", "H");
}
else if (Input == DataSelection.ModbusDataInputRegisters) {
return new StringPair("Input Registers", "mbTypeRegInput");
if (UseShortenCode == false) {
return new StringPair("Input Registers", "mbTypeRegInput");
}
return new StringPair("Input Registers", "I");
}
return new StringPair("Coils", "mbTypeCoils");
}
Expand Down
5 changes: 5 additions & 0 deletions Serial Monitor/Classes/Enums/FormatEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ public enum LineFormatting {
CR = 0x02,
CRLF = 0x03
}
public enum SignedState {
Unsigned = 0x00,
Signed = 0x01,
Toogle = 0x02
}
}
}
20 changes: 11 additions & 9 deletions Serial Monitor/Classes/Modbus/ModbusCoil.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using Serial_Monitor.Classes.Structures;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serial_Monitor.Classes.Modbus
{
public class ModbusCoil {
public class ModbusCoil: ModbusObject {
SerialManager? parentManager = null;
public SerialManager? ParentManager {
get { return parentManager; }
Expand All @@ -32,16 +33,17 @@ public bool Value {
SystemManager.RegisterValueChanged(parentManager, this, Index, typeData);
}
}
string name = "";
public string Name {
get { return name; }
set {
name = value;
}
}
bool userChanged = false;
public bool UserChanged {
get { return userChanged; }
}
public void Set(StringPair Input) {
if (Input.A.ToLower() == "name") {
Name = Input.B;
}
else if (Input.A.ToLower() == "value") {
Value = (Input.B == "1" ? true : false);
}
}
}
}
71 changes: 69 additions & 2 deletions Serial Monitor/Classes/Modbus/ModbusEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Serial_Monitor.Classes.Enums.FormatEnums;
using static Serial_Monitor.Classes.Enums.ModbusEnums;
using ListControl = ODModules.ListControl;

Expand Down Expand Up @@ -274,6 +275,44 @@ public static void ChangeSizeList(object? sender, ListControl lstMonitor) {
}
lstMonitor.Invalidate();
}
public static void ChangeSignedList(ListControl lstMonitor, SignedState State) {
int SelectedCount = lstMonitor.SelectionCount;
if (SelectedCount <= 0) { return; }
foreach (ListItem Li in lstMonitor.CurrentItems) {
if (Li.SubItems.Count >= ModbusEditor.Indx_Value) {
if (Li.Selected == true) {
if (Li.Tag == null) { continue; }
if (Li.Tag.GetType() == typeof(ModbusRegister)) {
ModbusRegister Reg = (ModbusRegister)Li.Tag;
if (Reg.Format == DataFormat.Float) {
Reg.Signed = false;
}
else if (Reg.Format == DataFormat.Double) {
Reg.Signed = false;
}
else {
switch (State) {
case SignedState.Unsigned:
Reg.Signed = false; break;
case SignedState.Signed:
Reg.Signed = true; break;
case SignedState.Toogle:
Reg.Signed = !Reg.Signed; break;
}
}
Li[Indx_Signed].Checked = Reg.Signed;
Li[Indx_Value].Text = Reg.FormattedValue;
RetroactivelyApplyFormatChanges(Reg.Address, lstMonitor);
}
SelectedCount--;
}
if (SelectedCount <= 0) {
break;
}
}
}
lstMonitor.Invalidate();
}
#endregion
#region Format Editing Support
public static void CheckItem(object DropDownList, DataFormat CheckOn) {
Expand Down Expand Up @@ -431,7 +470,7 @@ public static void CopyRegistersAsText(ListControl ListEditor, bool ClearSelecti
public static void CopyRegisters(ListControl ListEditor, ModbusClipboardFlags Flags, bool ClearSelection = true) {
if (ListEditor.CurrentItems == null) { return; }
List<ModbusDataObject> list = new List<ModbusDataObject>();
for (int i = 0; i < ListEditor.CurrentItems.Count; i++) {
for (int i = 0; i < ListEditor.CurrentItems.Count; i++) {
if (ListEditor.CurrentItems[i].Selected == true) {
if (ListEditor.CurrentItems[i].SubItems.Count == 5) {
object? objCmd = ListEditor.CurrentItems[i].Tag;
Expand Down Expand Up @@ -536,7 +575,7 @@ private static void PasteRegisterNames(ListControl ListEditor) {
if (objCmd == null) { continue; }
if (objCmd.GetType() == typeof(ModbusRegister)) {
ModbusRegister Reg = (ModbusRegister)objCmd;
Reg.Name = CopiedItems[j].Replace("\r","");
Reg.Name = CopiedItems[j].Replace("\r", "");
ListEditor.CurrentItems[k][1].Text = Reg.Name;
SystemManager.RegisterNameChanged(Reg.ParentManager, Reg, Reg.Address, Reg.ComponentType);
}
Expand Down Expand Up @@ -571,5 +610,33 @@ private static bool FlagSet(ModbusClipboardFlags DataObj, ModbusClipboardFlags F
return false;
}
}


public static void ChangeAppearance(object? sender, ListControl lstMonitor) {
int SelectedCount = lstMonitor.SelectionCount;
if (SelectedCount <= 0) { return; }
foreach (ListItem Li in lstMonitor.CurrentItems) {
if (Li.SubItems.Count >= Indx_Value) {
if (Li.Selected == true) {
if (Li.Tag == null) { continue; }
if (Li.Tag.GetType() == typeof(ModbusObject)) {
ModbusObject Reg = (ModbusObject)Li.Tag;
if (Reg.UseBackColor == true) {
Li[Indx_Display].BackColor = Reg.BackColor;
}
if (Reg.UseForeColor == true) {
Li[Indx_Display].ForeColor = Reg.ForeColor;
}
}
SelectedCount--;
}
if (SelectedCount <= 0) {
break;
}
}
}
lstMonitor.Invalidate();
}
}

}
35 changes: 35 additions & 0 deletions Serial Monitor/Classes/Modbus/ModbusObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serial_Monitor.Classes.Modbus {
public class ModbusObject {
string name = "";
public string Name {
get { return name; }
set { name = value; }
}
Color backColor = Color.White;
public Color BackColor {
get { return backColor; }
set { backColor = value; }
}
Color foreColor = Color.Black;
public Color ForeColor {
get { return foreColor; }
set { foreColor = value; }
}
bool useBackColor = false;
public bool UseBackColor {
get { return useBackColor; }
set { useBackColor = value; }
}
bool useForeColor = false;
public bool UseForeColor {
get { return useForeColor; }
set { useForeColor = value; }
}
}
}
59 changes: 38 additions & 21 deletions Serial Monitor/Classes/Modbus/ModbusRegister.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Serial_Monitor.Classes.Enums;
using Serial_Monitor.Classes.Structures;
using System;
using System.Collections.Generic;
using System.Data;
Expand All @@ -8,11 +9,10 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Serial_Monitor.Classes.Modbus {

public class ModbusRegister {
public class ModbusRegister: ModbusObject {
const bool TEST = false;
int Index = 0;
SerialManager? parentManager = null;
public SerialManager? ParentManager {
Expand Down Expand Up @@ -50,11 +50,7 @@ public string FormattedValue {
SystemManager.RegisterValueChanged(parentManager, this, Index, typeData);
}
}
string name = "";
public string Name {
get { return name; }
set { name = value; }
}

bool userChanged = false;
public bool UserChanged {
get { return userChanged; }
Expand Down Expand Up @@ -222,17 +218,17 @@ public void PushValue(long Input, bool AllowTransmit) {
if ((AllowTransmit) && (parentManager.IsMaster)) {
SystemManager.SendModbusCommand(parentManager, typeData, "Write Register " + Index.ToString() + " = " + Value.ToString());
}
#if DEBUG
#if TEST
Debug.Print("Size: 16, Input: " + Input.ToString() + ", Set: " + regValue.ToString());
#endif
#endif
}
else if (dataSize == ModbusEnums.DataSize.Bits32) {
#if DEBUG
Debug.Print("Size: 32, Input: " + Input.ToString());
#if TEST
Debug.Print("Size: 32, Input: " + Input.ToString());
#endif
regValue = (short)(0xFFFF & Input);
#if DEBUG
Debug.Print("Size: 16, Set: " + regValue.ToString());
#if TEST
Debug.Print("Size: 16, Set: " + regValue.ToString());
#endif
if (Index + 1 < ModbusSupport.MaximumRegisters) {
SetData(Index + 1, 1, Input, typeData, parentManager, AllowTransmit);
Expand Down Expand Up @@ -306,16 +302,16 @@ public void ModifyValue() {
}
else {
if (Index + 1 < ModbusSupport.MaximumRegisters - 1) {
#if DEBUG
Debug.Print("Size: 32, Formatter: ");
Debug.Print(" - " + ((ushort)regValue).ToString());
Debug.Print(" - " + AppendData(Index + 1, 1, typeData, parentManager).ToString());
#if TEST
Debug.Print("Size: 32, Formatter: ");
Debug.Print(" - " + ((ushort)regValue).ToString());
Debug.Print(" - " + AppendData(Index + 1, 1, typeData, parentManager).ToString());
#endif
Temp = (long)(ushort)regValue;
Temp |= AppendData(Index + 1, 1, typeData, parentManager);
#if DEBUG
Debug.Print(" Results In: " + Temp.ToString());
#endif
#if TEST
Debug.Print(" Results In: " + Temp.ToString());
#endif
}
}
formattedValue = Formatters.LongToString(Temp, format, dataSize, signed);
Expand Down Expand Up @@ -370,5 +366,26 @@ private static void SetData(int NextIndex, int Shift, long Value, DataSelection
parentManager.HoldingRegisters[NextIndex].PushValue((long)Output, AllowTransmit);
}
}
public void Set(StringPair Input) {
if (Input.A.ToLower() == "name") {
Name = Input.B;
}
else if (Input.A.ToLower() == "value") {
short Temp = 0;
short.TryParse(Input.B, out Temp);
Value = Temp;
}
else if (Input.A.ToLower() == "format") {
Format = EnumManager.StringToDataFormat(Input.B);
}
else if (Input.A.ToLower() == "size") {
int Temp = 0;
int.TryParse(Input.B, out Temp);
Size = EnumManager.IntegerToDataSize(Temp);
}
else if (Input.A.ToLower() == "signed") {
Signed = (Input.B == "1" ? true : false);
}
}
}
}
Loading

0 comments on commit 4aeab3d

Please sign in to comment.