Skip to content

Commit cd67157

Browse files
committed
v2.49
- Enum type now showed as integer when match not found - Fix enable modbus tcp port (standard and secure) - Fix bug live edit coils - Minor fix tool command bit/byte/word FC03
1 parent 31ef2ac commit cd67157

File tree

7 files changed

+364
-244
lines changed

7 files changed

+364
-244
lines changed

ModBus_Client/ComandiBit.xaml.cs

Lines changed: 133 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
using System.Windows.Media;
4242
using System.Windows.Media.Imaging;
4343
using System.Windows.Shapes;
44+
using System.Net.Sockets;
4445

4546
using ModBusMaster_Chicco;
4647

@@ -63,7 +64,6 @@ namespace ModBus_Client
6364
/// </summary>
6465
public partial class ComandiBit : Window
6566
{
66-
ModBus_Chicco ModBus;
6767
MainWindow main;
6868
Language lang;
6969

@@ -94,7 +94,6 @@ public ComandiBit(ModBus_Chicco ModBus_, MainWindow main_)
9494
// Creo evento di chiusura del form
9595
this.Closing += Sim_Form_cs_Closing;
9696

97-
ModBus = ModBus_;
9897
main = main_;
9998
pathToConfiguration = main.pathToConfiguration;
10099

@@ -690,40 +689,110 @@ private void intToPicture(int row, int value)
690689

691690
private void picture(int row, int bit)
692691
{
693-
if (!(bool)checkBoxDisablePictureBoxCommands.IsChecked)
692+
try
694693
{
695-
pictureBoxBusy.Background = Brushes.Yellow;
694+
if (!(bool)checkBoxDisablePictureBoxCommands.IsChecked)
695+
{
696+
pictureBoxBusy.Background = Brushes.Yellow;
696697

697-
int repeat = new int();
698+
int repeat = new int();
699+
bool result = true;
698700

699-
if ((bool)checkBoxOffBit.IsChecked)
700-
repeat = 2;
701-
else
702-
repeat = 1;
701+
if ((bool)checkBoxOffBit.IsChecked)
702+
repeat = 2;
703+
else
704+
repeat = 1;
703705

704-
// Se ce la spunta sulla disattivazione automatica del bit ripeto due volte la seguente funzione
705-
for (int i = 0; i < repeat; i++)
706-
{
707-
if (pictureBox[row, bit].Background == Brushes.LightGray)
706+
// Se ce la spunta sulla disattivazione automatica del bit ripeto due volte la seguente funzione
707+
for (int i = 0; i < repeat; i++)
708708
{
709-
pictureBox[row, bit].Background = Brushes.Orange;
709+
if (pictureBox[row, bit].Background == Brushes.LightGray)
710+
{
711+
pictureBox[row, bit].Background = Brushes.Orange;
712+
}
713+
else
714+
{
715+
pictureBox[row, bit].Background = Brushes.LightGray;
716+
repeat = 1; // Se il bit era gia' settato da prima evito
717+
// di esguire la funzione due volte
718+
}
719+
720+
UInt16 val = pictureToInt(row);
721+
UInt16 address = (UInt16)(P.uint_parser(textBoxRegister[row], comboBox[row]) + P.uint_parser(textBoxHoldingOffset, comboBoxHoldingOffset));
722+
723+
textBoxValue[row].Text = val.ToString();
724+
725+
result = result & (bool)main.ModBus.presetSingleRegister_06(byte.Parse(textBoxModBusAddress.Text), address, val, main.readTimeout);
726+
}
727+
728+
if (result)
729+
{
730+
pictureBoxBusy.Background = Brushes.LightGray;
710731
}
711732
else
712733
{
713-
pictureBox[row, bit].Background = Brushes.LightGray;
714-
repeat = 1; // Se il bit era gia' settato da prima evito
715-
// di esguire la funzione due volte
734+
pictureBoxBusy.Background = Brushes.PaleVioletRed;
735+
MessageBox.Show("Error writing register", "Alert", MessageBoxButton.OK, MessageBoxImage.Error);
716736
}
737+
}
738+
}
739+
catch (Exception ex)
740+
{
741+
pictureBoxBusy.Background = Brushes.PaleVioletRed;
742+
MessageBox.Show("Error writing register", "Alert", MessageBoxButton.OK, MessageBoxImage.Error);
717743

718-
UInt16 val = pictureToInt(row);
719-
UInt16 address = (UInt16)(P.uint_parser(textBoxRegister[row], comboBox[row]) + P.uint_parser(textBoxHoldingOffset, comboBoxHoldingOffset));
720-
721-
textBoxValue[row].Text = val.ToString();
744+
if (ex is InvalidOperationException || ex is IOException || ex is SocketException)
745+
{
746+
main.SetTableDisconnectError(main.list_holdingRegistersTable, true);
722747

723-
ModBus.presetSingleRegister_06(byte.Parse(textBoxModBusAddress.Text), address, val, main.readTimeout);
748+
if (main.ModBus.type == main.ModBus_Def.TYPE_RTU || main.ModBus.type == main.ModBus_Def.TYPE_ASCII)
749+
{
750+
this.Dispatcher.Invoke((Action)delegate
751+
{
752+
if (main.ModBus.ClientActive)
753+
main.buttonSerialActive_Click(null, null);
754+
});
755+
}
756+
else if (main.ModBus.type == main.ModBus_Def.TYPE_TCP_SOCK || main.ModBus.type == main.ModBus_Def.TYPE_TCP_SECURE)
757+
{
758+
this.Dispatcher.Invoke((Action)delegate
759+
{
760+
if (main.ModBus.ClientActive)
761+
main.buttonTcpActive_Click(null, null);
762+
});
763+
}
764+
}
765+
else if (ex is ModbusException)
766+
{
767+
if (ex.Message.IndexOf("Timed out") != -1)
768+
{
769+
main.SetTableTimeoutError(main.list_holdingRegistersTable, false);
770+
}
771+
if (ex.Message.IndexOf("ModBus ErrCode") != -1)
772+
{
773+
main.SetTableModBusError(main.list_holdingRegistersTable, (ModbusException)ex, false);
774+
}
775+
if (ex.Message.IndexOf("ModbusProtocolError") != -1)
776+
{
777+
main.SetTableStringError(main.list_holdingRegistersTable, (ModbusException)ex, true);
778+
}
779+
if (ex.Message.IndexOf("CRC Error") != -1)
780+
{
781+
main.SetTableCrcError(main.list_holdingRegistersTable, false);
782+
}
783+
}
784+
else
785+
{
786+
main.SetTableInternalError(main.list_holdingRegistersTable, false);
724787
}
725788

726-
pictureBoxBusy.Background = Brushes.LightGray;
789+
this.Dispatcher.Invoke((Action)delegate
790+
{
791+
main.dataGridViewHolding.ItemsSource = null;
792+
main.dataGridViewHolding.ItemsSource = main.list_holdingRegistersTable;
793+
});
794+
795+
Console.WriteLine(ex);
727796
}
728797
}
729798

@@ -742,68 +811,73 @@ private void reset(int row)
742811

743812
try
744813
{
745-
ModBus.presetSingleRegister_06(byte.Parse(textBoxModBusAddress.Text), address, 0, main.readTimeout);
814+
if (main.ModBus.presetSingleRegister_06(byte.Parse(textBoxModBusAddress.Text), address, 0, main.readTimeout) == true)
815+
{
816+
pictureBoxBusy.Background = Brushes.LightGray;
817+
}
818+
else
819+
{
820+
pictureBoxBusy.Background = Brushes.PaleVioletRed;
821+
MessageBox.Show("Error writing register", "Alert", MessageBoxButton.OK, MessageBoxImage.Error);
822+
}
746823
}
747-
catch (InvalidOperationException err)
824+
catch (Exception ex)
748825
{
749-
if (err.Message.IndexOf("non-connected socket") != -1)
826+
pictureBoxBusy.Background = Brushes.PaleVioletRed;
827+
828+
if (ex is InvalidOperationException || ex is IOException || ex is SocketException)
750829
{
751-
main.SetTableDisconnectError(main.list_coilsTable, true);
830+
main.SetTableDisconnectError(main.list_holdingRegistersTable, true);
752831

753832
if (main.ModBus.type == main.ModBus_Def.TYPE_RTU || main.ModBus.type == main.ModBus_Def.TYPE_ASCII)
754833
{
755834
this.Dispatcher.Invoke((Action)delegate
756835
{
757-
main.buttonSerialActive_Click(null, null);
836+
if (main.ModBus.ClientActive)
837+
main.buttonSerialActive_Click(null, null);
758838
});
759839
}
760-
else
840+
else if (main.ModBus.type == main.ModBus_Def.TYPE_TCP_SOCK || main.ModBus.type == main.ModBus_Def.TYPE_TCP_SECURE)
761841
{
762842
this.Dispatcher.Invoke((Action)delegate
763843
{
764-
main.buttonTcpActive_Click(null, null);
844+
if (main.ModBus.ClientActive)
845+
main.buttonTcpActive_Click(null, null);
765846
});
766847
}
767848
}
768-
769-
Console.WriteLine(err);
770-
}
771-
catch (ModbusException err)
772-
{
773-
if (err.Message.IndexOf("Timed out") != -1)
774-
{
775-
main.SetTableTimeoutError(main.list_holdingRegistersTable, true);
776-
}
777-
if (err.Message.IndexOf("ModBus ErrCode") != -1)
849+
else if (ex is ModbusException)
778850
{
779-
main.SetTableModBusError(main.list_holdingRegistersTable, err, true);
851+
if (ex.Message.IndexOf("Timed out") != -1)
852+
{
853+
main.SetTableTimeoutError(main.list_holdingRegistersTable, false);
854+
}
855+
if (ex.Message.IndexOf("ModBus ErrCode") != -1)
856+
{
857+
main.SetTableModBusError(main.list_holdingRegistersTable, (ModbusException)ex, false);
858+
}
859+
if (ex.Message.IndexOf("ModbusProtocolError") != -1)
860+
{
861+
main.SetTableStringError(main.list_holdingRegistersTable, (ModbusException)ex, true);
862+
}
863+
if (ex.Message.IndexOf("CRC Error") != -1)
864+
{
865+
main.SetTableCrcError(main.list_holdingRegistersTable, false);
866+
}
780867
}
781-
if (err.Message.IndexOf("CRC Error") != -1)
868+
else
782869
{
783-
main.SetTableCrcError(main.list_holdingRegistersTable, true);
870+
main.SetTableInternalError(main.list_holdingRegistersTable, false);
784871
}
785872

786-
Console.WriteLine(err);
787-
788873
this.Dispatcher.Invoke((Action)delegate
789874
{
790875
main.dataGridViewHolding.ItemsSource = null;
791876
main.dataGridViewHolding.ItemsSource = main.list_holdingRegistersTable;
792877
});
793-
}
794-
catch (Exception err)
795-
{
796-
main.SetTableInternalError(main.list_holdingRegistersTable, true);
797-
Console.WriteLine(err);
798878

799-
this.Dispatcher.Invoke((Action)delegate
800-
{
801-
main.dataGridViewHolding.ItemsSource = null;
802-
main.dataGridViewHolding.ItemsSource = main.list_holdingRegistersTable;
803-
});
879+
Console.WriteLine(ex);
804880
}
805-
806-
pictureBoxBusy.Background = Brushes.LightGray;
807881
}
808882

809883
private void read(int row)
@@ -812,7 +886,7 @@ private void read(int row)
812886

813887
UInt16 address = (UInt16)(P.uint_parser(textBoxRegister[row], comboBox[row]) + P.uint_parser(textBoxHoldingOffset, comboBoxHoldingOffset));
814888

815-
UInt16 val = ModBus.readHoldingRegister_03(byte.Parse(textBoxModBusAddress.Text), address, 1, main.readTimeout)[0];
889+
UInt16 val = main.ModBus.readHoldingRegister_03(byte.Parse(textBoxModBusAddress.Text), address, 1, main.readTimeout)[0];
816890

817891
intToPicture(row, val);
818892
textBoxValue[row].Text = val.ToString();
@@ -834,7 +908,7 @@ private void edit(int row)
834908
S_titleLalbel[i] = textBoxLabel[i].Text;
835909
}
836910

837-
Editor editorForm = new Editor(false, ModBus, row, this, pathToConfiguration, !textBoxLabel[0].IsEnabled, main);
911+
Editor editorForm = new Editor(false, main.ModBus, row, this, pathToConfiguration, !textBoxLabel[0].IsEnabled, main);
838912
editorForm.Show();
839913
}
840914
catch (Exception error)

0 commit comments

Comments
 (0)