From b5dbd8fc9c7c88b8d239cb93db09223466dcfb96 Mon Sep 17 00:00:00 2001 From: nikzori Date: Mon, 11 Nov 2024 10:43:58 +0300 Subject: [PATCH] better connection controls --- Form1.Designer.cs | 247 +++++++++++++++++++++++++++++++++++++++++++--- Form1.cs | 223 ++++++++++++++++++++++++++++++----------- Modbus.cs | 44 ++++++++- README.md | 10 +- 4 files changed, 451 insertions(+), 73 deletions(-) diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 9906083..baea33d 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -30,6 +30,12 @@ { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(App)); this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.panel8 = new System.Windows.Forms.Panel(); + this.CBox_DataBits = new System.Windows.Forms.ComboBox(); + this.label8 = new System.Windows.Forms.Label(); + this.panel7 = new System.Windows.Forms.Panel(); + this.CBox_BaudRate = new System.Windows.Forms.ComboBox(); + this.label7 = new System.Windows.Forms.Label(); this.ButtonConnect = new System.Windows.Forms.Button(); this.panel2 = new System.Windows.Forms.Panel(); this.UpDown_ModbusID = new System.Windows.Forms.NumericUpDown(); @@ -39,6 +45,9 @@ this.label1 = new System.Windows.Forms.Label(); this.TextBox_Log = new System.Windows.Forms.TextBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.panel6 = new System.Windows.Forms.Panel(); + this.UpDown_Value = new System.Windows.Forms.NumericUpDown(); + this.label6 = new System.Windows.Forms.Label(); this.panel5 = new System.Windows.Forms.Panel(); this.UpDown_RegLength = new System.Windows.Forms.NumericUpDown(); this.label5 = new System.Windows.Forms.Label(); @@ -49,35 +58,112 @@ this.panel4 = new System.Windows.Forms.Panel(); this.CBox_Function = new System.Windows.Forms.ComboBox(); this.label4 = new System.Windows.Forms.Label(); + this.panel9 = new System.Windows.Forms.Panel(); + this.comboBox1 = new System.Windows.Forms.ComboBox(); + this.label9 = new System.Windows.Forms.Label(); + this.panel10 = new System.Windows.Forms.Panel(); + this.CBox_StopBits = new System.Windows.Forms.ComboBox(); + this.label10 = new System.Windows.Forms.Label(); + this.panel11 = new System.Windows.Forms.Panel(); + this.comboBox2 = new System.Windows.Forms.ComboBox(); + this.label11 = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); + this.panel8.SuspendLayout(); + this.panel7.SuspendLayout(); this.panel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.UpDown_ModbusID)).BeginInit(); this.panel1.SuspendLayout(); this.groupBox2.SuspendLayout(); + this.panel6.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.UpDown_Value)).BeginInit(); this.panel5.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.UpDown_RegLength)).BeginInit(); this.panel3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.UpDown_RegAddress)).BeginInit(); this.panel4.SuspendLayout(); + this.panel9.SuspendLayout(); + this.panel10.SuspendLayout(); + this.panel11.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // + this.groupBox1.Controls.Add(this.panel11); + this.groupBox1.Controls.Add(this.panel10); + this.groupBox1.Controls.Add(this.panel8); + this.groupBox1.Controls.Add(this.panel7); this.groupBox1.Controls.Add(this.ButtonConnect); this.groupBox1.Controls.Add(this.panel2); this.groupBox1.Controls.Add(this.panel1); this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(344, 63); + this.groupBox1.Size = new System.Drawing.Size(490, 110); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Подключение"; // + // panel8 + // + this.panel8.Controls.Add(this.panel9); + this.panel8.Controls.Add(this.CBox_DataBits); + this.panel8.Controls.Add(this.label8); + this.panel8.Location = new System.Drawing.Point(162, 19); + this.panel8.Name = "panel8"; + this.panel8.Size = new System.Drawing.Size(79, 43); + this.panel8.TabIndex = 4; + // + // CBox_DataBits + // + this.CBox_DataBits.FormattingEnabled = true; + this.CBox_DataBits.Location = new System.Drawing.Point(6, 17); + this.CBox_DataBits.Name = "CBox_DataBits"; + this.CBox_DataBits.Size = new System.Drawing.Size(70, 21); + this.CBox_DataBits.TabIndex = 2; + this.CBox_DataBits.Text = "8"; + // + // label8 + // + this.label8.AutoSize = true; + this.label8.Location = new System.Drawing.Point(3, 0); + this.label8.Name = "label8"; + this.label8.Size = new System.Drawing.Size(73, 13); + this.label8.TabIndex = 0; + this.label8.Text = "Биты данных"; + // + // panel7 + // + this.panel7.Controls.Add(this.CBox_BaudRate); + this.panel7.Controls.Add(this.label7); + this.panel7.Location = new System.Drawing.Point(81, 19); + this.panel7.Name = "panel7"; + this.panel7.Size = new System.Drawing.Size(75, 43); + this.panel7.TabIndex = 3; + // + // CBox_BaudRate + // + this.CBox_BaudRate.FormattingEnabled = true; + this.CBox_BaudRate.Location = new System.Drawing.Point(6, 17); + this.CBox_BaudRate.Name = "CBox_BaudRate"; + this.CBox_BaudRate.Size = new System.Drawing.Size(65, 21); + this.CBox_BaudRate.TabIndex = 2; + this.CBox_BaudRate.Text = "9600"; + // + // label7 + // + this.label7.AutoSize = true; + this.label7.Location = new System.Drawing.Point(3, 0); + this.label7.Name = "label7"; + this.label7.Size = new System.Drawing.Size(34, 13); + this.label7.TabIndex = 0; + this.label7.Text = "Боды"; + // // ButtonConnect // - this.ButtonConnect.Location = new System.Drawing.Point(162, 34); + this.ButtonConnect.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonConnect.Location = new System.Drawing.Point(195, 75); this.ButtonConnect.Name = "ButtonConnect"; - this.ButtonConnect.Size = new System.Drawing.Size(176, 23); + this.ButtonConnect.Size = new System.Drawing.Size(100, 25); this.ButtonConnect.TabIndex = 4; this.ButtonConnect.Text = "Подключиться"; this.ButtonConnect.UseVisualStyleBackColor = true; @@ -87,7 +173,7 @@ // this.panel2.Controls.Add(this.UpDown_ModbusID); this.panel2.Controls.Add(this.label2); - this.panel2.Location = new System.Drawing.Point(81, 19); + this.panel2.Location = new System.Drawing.Point(415, 19); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(75, 43); this.panel2.TabIndex = 2; @@ -124,7 +210,7 @@ this.CBox_Ports.Name = "CBox_Ports"; this.CBox_Ports.Size = new System.Drawing.Size(65, 21); this.CBox_Ports.TabIndex = 1; - this.CBox_Ports.Text = "COM 1"; + this.CBox_Ports.Text = "COM1"; this.CBox_Ports.MouseClick += new System.Windows.Forms.MouseEventHandler(this.CBox_Ports_Click); // // label1 @@ -138,27 +224,53 @@ // // TextBox_Log // - this.TextBox_Log.Location = new System.Drawing.Point(12, 82); + this.TextBox_Log.Location = new System.Drawing.Point(12, 127); this.TextBox_Log.Multiline = true; this.TextBox_Log.Name = "TextBox_Log"; this.TextBox_Log.ReadOnly = true; this.TextBox_Log.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; - this.TextBox_Log.Size = new System.Drawing.Size(417, 87); + this.TextBox_Log.Size = new System.Drawing.Size(490, 118); this.TextBox_Log.TabIndex = 1; // // groupBox2 // + this.groupBox2.Controls.Add(this.panel6); this.groupBox2.Controls.Add(this.panel5); this.groupBox2.Controls.Add(this.Button_SendCommand); this.groupBox2.Controls.Add(this.panel3); this.groupBox2.Controls.Add(this.panel4); - this.groupBox2.Location = new System.Drawing.Point(12, 175); + this.groupBox2.Location = new System.Drawing.Point(12, 251); this.groupBox2.Name = "groupBox2"; - this.groupBox2.Size = new System.Drawing.Size(417, 63); + this.groupBox2.Size = new System.Drawing.Size(490, 63); this.groupBox2.TabIndex = 5; this.groupBox2.TabStop = false; this.groupBox2.Text = "Команды"; // + // panel6 + // + this.panel6.Controls.Add(this.UpDown_Value); + this.panel6.Controls.Add(this.label6); + this.panel6.Location = new System.Drawing.Point(337, 20); + this.panel6.Name = "panel6"; + this.panel6.Size = new System.Drawing.Size(75, 43); + this.panel6.TabIndex = 4; + // + // UpDown_Value + // + this.UpDown_Value.Location = new System.Drawing.Point(6, 17); + this.UpDown_Value.Name = "UpDown_Value"; + this.UpDown_Value.Size = new System.Drawing.Size(66, 20); + this.UpDown_Value.TabIndex = 1; + // + // label6 + // + this.label6.AutoSize = true; + this.label6.Location = new System.Drawing.Point(3, 0); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(55, 13); + this.label6.TabIndex = 0; + this.label6.Text = "Значение"; + // // panel5 // this.panel5.Controls.Add(this.UpDown_RegLength); @@ -186,7 +298,7 @@ // // Button_SendCommand // - this.Button_SendCommand.Location = new System.Drawing.Point(337, 35); + this.Button_SendCommand.Location = new System.Drawing.Point(415, 35); this.Button_SendCommand.Name = "Button_SendCommand"; this.Button_SendCommand.Size = new System.Drawing.Size(71, 23); this.Button_SendCommand.TabIndex = 4; @@ -236,6 +348,7 @@ this.CBox_Function.Size = new System.Drawing.Size(147, 21); this.CBox_Function.TabIndex = 1; this.CBox_Function.Text = "01 - Read Coil"; + this.CBox_Function.SelectedIndexChanged += new System.EventHandler(this.OnSelectedFunctionChanged); // // label4 // @@ -246,11 +359,92 @@ this.label4.TabIndex = 0; this.label4.Text = "Функция"; // + // panel9 + // + this.panel9.Controls.Add(this.comboBox1); + this.panel9.Controls.Add(this.label9); + this.panel9.Location = new System.Drawing.Point(82, 0); + this.panel9.Name = "panel9"; + this.panel9.Size = new System.Drawing.Size(79, 43); + this.panel9.TabIndex = 5; + // + // comboBox1 + // + this.comboBox1.FormattingEnabled = true; + this.comboBox1.Location = new System.Drawing.Point(6, 17); + this.comboBox1.Name = "comboBox1"; + this.comboBox1.Size = new System.Drawing.Size(70, 21); + this.comboBox1.TabIndex = 2; + this.comboBox1.Text = "8"; + // + // label9 + // + this.label9.AutoSize = true; + this.label9.Location = new System.Drawing.Point(3, 0); + this.label9.Name = "label9"; + this.label9.Size = new System.Drawing.Size(73, 13); + this.label9.TabIndex = 0; + this.label9.Text = "Биты данных"; + // + // panel10 + // + this.panel10.Controls.Add(this.CBox_StopBits); + this.panel10.Controls.Add(this.label10); + this.panel10.Location = new System.Drawing.Point(247, 19); + this.panel10.Name = "panel10"; + this.panel10.Size = new System.Drawing.Size(79, 43); + this.panel10.TabIndex = 5; + // + // CBox_StopBits + // + this.CBox_StopBits.FormattingEnabled = true; + this.CBox_StopBits.Location = new System.Drawing.Point(6, 17); + this.CBox_StopBits.Name = "CBox_StopBits"; + this.CBox_StopBits.Size = new System.Drawing.Size(70, 21); + this.CBox_StopBits.TabIndex = 2; + this.CBox_StopBits.Text = "8"; + // + // label10 + // + this.label10.AutoSize = true; + this.label10.Location = new System.Drawing.Point(3, 0); + this.label10.Name = "label10"; + this.label10.Size = new System.Drawing.Size(59, 13); + this.label10.TabIndex = 0; + this.label10.Text = "Стоп-биты"; + // + // panel11 + // + this.panel11.Controls.Add(this.comboBox2); + this.panel11.Controls.Add(this.label11); + this.panel11.Location = new System.Drawing.Point(330, 19); + this.panel11.Name = "panel11"; + this.panel11.Size = new System.Drawing.Size(79, 43); + this.panel11.TabIndex = 6; + // + // comboBox2 + // + this.comboBox2.FormattingEnabled = true; + this.comboBox2.Location = new System.Drawing.Point(6, 17); + this.comboBox2.Name = "comboBox2"; + this.comboBox2.Size = new System.Drawing.Size(70, 21); + this.comboBox2.TabIndex = 2; + this.comboBox2.Text = "8"; + // + // label11 + // + this.label11.AutoSize = true; + this.label11.Location = new System.Drawing.Point(3, 0); + this.label11.Name = "label11"; + this.label11.Size = new System.Drawing.Size(59, 13); + this.label11.TabIndex = 0; + this.label11.Text = "Стоп-биты"; + // // App // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(432, 242); + this.ClientSize = new System.Drawing.Size(514, 326); this.Controls.Add(this.groupBox2); this.Controls.Add(this.TextBox_Log); this.Controls.Add(this.groupBox1); @@ -261,12 +455,19 @@ this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.App_FormClosed); this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); + this.panel8.ResumeLayout(false); + this.panel8.PerformLayout(); + this.panel7.ResumeLayout(false); + this.panel7.PerformLayout(); this.panel2.ResumeLayout(false); this.panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.UpDown_ModbusID)).EndInit(); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); this.groupBox2.ResumeLayout(false); + this.panel6.ResumeLayout(false); + this.panel6.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.UpDown_Value)).EndInit(); this.panel5.ResumeLayout(false); this.panel5.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.UpDown_RegLength)).EndInit(); @@ -275,6 +476,12 @@ ((System.ComponentModel.ISupportInitialize)(this.UpDown_RegAddress)).EndInit(); this.panel4.ResumeLayout(false); this.panel4.PerformLayout(); + this.panel9.ResumeLayout(false); + this.panel9.PerformLayout(); + this.panel10.ResumeLayout(false); + this.panel10.PerformLayout(); + this.panel11.ResumeLayout(false); + this.panel11.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -302,6 +509,24 @@ private System.Windows.Forms.Panel panel4; private System.Windows.Forms.ComboBox CBox_Function; private System.Windows.Forms.Label label4; + private System.Windows.Forms.Panel panel6; + private System.Windows.Forms.NumericUpDown UpDown_Value; + private System.Windows.Forms.Label label6; + private System.Windows.Forms.Panel panel7; + private System.Windows.Forms.ComboBox CBox_BaudRate; + private System.Windows.Forms.Label label7; + private System.Windows.Forms.Panel panel8; + private System.Windows.Forms.ComboBox CBox_DataBits; + private System.Windows.Forms.Label label8; + private System.Windows.Forms.Panel panel10; + private System.Windows.Forms.ComboBox CBox_StopBits; + private System.Windows.Forms.Label label10; + private System.Windows.Forms.Panel panel9; + private System.Windows.Forms.ComboBox comboBox1; + private System.Windows.Forms.Label label9; + private System.Windows.Forms.Panel panel11; + private System.Windows.Forms.ComboBox comboBox2; + private System.Windows.Forms.Label label11; } } diff --git a/Form1.cs b/Form1.cs index b044d80..5eea92f 100644 --- a/Form1.cs +++ b/Form1.cs @@ -12,17 +12,27 @@ using System.Drawing.Drawing2D; using System.Threading; using System.Runtime; using System.Web; +using System.Windows.Forms.Automation; +using System.Diagnostics; namespace Gidrolock_Modbus_Scanner { public partial class App : Form { + public static int[] BaudRate = new int[] + { + 110, 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230300, 460800, 921600 + }; + public static int[] DataBits = new int[] { 7, 8 }; + int offset = 0; - byte[] data = new byte[255]; + byte[] message = new byte[255]; public bool isAwaitingResponse = false; + public bool isProcessingResponse = false; public short[] res = new short[12]; public SerialPort port = new SerialPort(); public int expectedLength = 0; + public App() { @@ -35,10 +45,45 @@ namespace Gidrolock_Modbus_Scanner CBox_Function.Items.Add("02 - Read Discrete Input"); CBox_Function.Items.Add("03 - Read Holding Register"); CBox_Function.Items.Add("04 - Read Input Register"); + CBox_Function.Items.Add("05 - Write Single Coil"); + CBox_Function.Items.Add("06 - Write Single Holding Register"); + CBox_Function.Items.Add("0F - Write Multiple Coils"); + CBox_Function.Items.Add("10 - Write Multiple Registers"); CBox_Function.SelectedItem = CBox_Function.Items[0]; + CBox_BaudRate.Items.Add("110"); + CBox_BaudRate.Items.Add("300"); + CBox_BaudRate.Items.Add("1200"); + CBox_BaudRate.Items.Add("2400"); + CBox_BaudRate.Items.Add("4800"); + CBox_BaudRate.Items.Add("9600"); + CBox_BaudRate.Items.Add("14400"); + CBox_BaudRate.Items.Add("19200"); + CBox_BaudRate.Items.Add("28800"); + CBox_BaudRate.Items.Add("38400"); + CBox_BaudRate.Items.Add("57600"); + CBox_BaudRate.Items.Add("76800"); + CBox_BaudRate.Items.Add("115200"); + CBox_BaudRate.Items.Add("230300"); + CBox_BaudRate.Items.Add("460800"); + CBox_BaudRate.Items.Add("921600"); + CBox_BaudRate.SelectedIndex = 5; + + CBox_DataBits.Items.Add("7"); + CBox_DataBits.Items.Add("8"); + CBox_DataBits.SelectedIndex = 1; + + CBox_StopBits.Items.Add("Нет"); + CBox_StopBits.Items.Add("1"); + CBox_StopBits.Items.Add("1.5"); + CBox_StopBits.Items.Add("2"); + CBox_StopBits.SelectedIndex = 1; + UpDown_RegAddress.Minimum = 0; - UpDown_RegAddress.Maximum = 65536; + UpDown_RegAddress.Maximum = 65535; + + UpDown_Value.Minimum = 0; + UpDown_Value.Maximum = 65535; // 2^16 } void App_FormClosed(object sender, FormClosedEventArgs e) { @@ -69,54 +114,85 @@ namespace Gidrolock_Modbus_Scanner if (UpDown_ModbusID.Value == 0) MessageBox.Show("Глобальное вещание пока не поддерживается"); - try - { - if (port.IsOpen) - port.Close(); - - port.Handshake = Handshake.None; - port.PortName = CBox_Ports.Text; - port.BaudRate = 9600; - port.Parity = Parity.None; - port.DataBits = 8; - port.StopBits = StopBits.One; - - port.ReadTimeout = 1000; - port.WriteTimeout = 1000; - - - offset = 0; - data = new byte[255]; - port.Open(); - - byte[] message = new byte[8]; - Modbus.BuildMessage((byte)UpDown_ModbusID.Value, (byte)(1 + functionCode), address, length, ref message); - string messageParsed = Modbus.ParseByteArray(message); - - var send = await Modbus.ReadRegAsync(port, functionCode, (byte)UpDown_ModbusID.Value, address, length); - AddLog("Отправка сообщения: " + messageParsed); - isAwaitingResponse = true; - Task timer = Task.Delay(2000); - await timer.ContinueWith(_ => - { - if (isAwaitingResponse) - { - MessageBox.Show("Истекло время ожидания ответа.", "Ошибка"); - port.Close(); - } - }); - - } - catch (Exception err) - { + // Port Setup + if (port.IsOpen) port.Close(); - MessageBox.Show(err.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + + port.Handshake = Handshake.None; + port.PortName = CBox_Ports.Text; + port.BaudRate = BaudRate[CBox_BaudRate.SelectedIndex]; + port.Parity = Parity.None; + port.DataBits = DataBits[CBox_DataBits.SelectedIndex]; + port.StopBits = (StopBits)CBox_StopBits.SelectedIndex; + + port.ReadTimeout = 1000; + port.WriteTimeout = 1000; + + + offset = 0; + message = new byte[255]; + port.Open(); + + /* - Reading from Registers - */ + if (CBox_Function.SelectedIndex < 4) + { + try + { + byte[] request = new byte[8]; + Modbus.BuildMessage((byte)UpDown_ModbusID.Value, (byte)(1 + functionCode), address, length, ref request); + string messageParsed = Modbus.ParseByteArray(request); + + var send = await Modbus.ReadRegAsync(port, functionCode, (byte)UpDown_ModbusID.Value, address, length); + AddLog("Отправка сообщения: " + messageParsed); + isAwaitingResponse = true; + Task timer = Task.Delay(2000); + await timer.ContinueWith(_ => + { + if (isAwaitingResponse) + { + MessageBox.Show("Истекло время ожидания ответа.", "Ошибка"); + port.Close(); + } + }); + } + catch (Exception err) + { + port.Close(); + MessageBox.Show(err.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } + + /* - Writing to Registers - */ + else + { + try + { + if (CBox_Function.SelectedIndex < 6) // Single Registers + { + byte[] request = new byte[8]; + Modbus.BuildMessage((byte)UpDown_ModbusID.Value, (byte)(1 + functionCode), address, length, ref request); + string messageParsed = Modbus.ParseByteArray(request); + + var send = await Modbus.WriteSingle(port, functionCode, (byte)UpDown_ModbusID.Value, address, (ushort)UpDown_Value.Value); + } + else // Multiple Registers + { + byte[] request = new byte[(int)UpDown_RegLength.Value * 2 + 6]; + } + } + catch (Exception err) + { + port.Close(); + MessageBox.Show(err.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } private async void ButtonConnect_Click(object sender, EventArgs e) { - await SendMessageAsync(FunctionCode.HoldingRegister, 128, 1); + AddLog("Попытка подключиться к устройству Gidrolock."); + await SendMessageAsync(FunctionCode.DiscreteInput, 200, 5); } void CBox_Ports_Click(object sender, EventArgs e) @@ -129,23 +205,40 @@ namespace Gidrolock_Modbus_Scanner { Console.WriteLine("Data receieved on Serial Port"); isAwaitingResponse = false; - try - { - int len = port.BytesToRead; - Console.WriteLine("Data length: " + len); - port.Read(data, offset, len); - offset += len; - string dataCleaned = Modbus.ParseByteArray(data); - TextBox_Log.Invoke((MethodInvoker)delegate { AddLog("Получен ответ: " + dataCleaned); }); - //MessageBox.Show("Получен ответ от устройства: " + dataCleaned, "Успех", MessageBoxButtons.OK); - } - catch (Exception err) + if (!isProcessingResponse) { - MessageBox.Show(err.Message); + isProcessingResponse = true; + try + { + port.Read(message, 0, 3); + int length = (int)message[2]; + for (int i = 0; i < length + 2; i++) + { + port.Read(message, i + 3, 1); + } + byte[] data = new byte[length]; + for (int i = 0; i < length; i++) + { + data[i] = message[i + 3]; + } + Console.WriteLine("Data: " + Modbus.ParseByteArray(data)); + string dataCleaned = Modbus.ParseByteArray(message); + + TextBox_Log.Invoke((MethodInvoker)delegate { AddLog("Получен ответ: " + dataCleaned); }); + TextBox_Log.Invoke((MethodInvoker)delegate { AddLog("ASCII: " + "wip"); }); + //MessageBox.Show("Получен ответ от устройства: " + dataCleaned, "Успех", MessageBoxButtons.OK); + port.DiscardInBuffer(); + isProcessingResponse = false; + } + catch (Exception err) + { + MessageBox.Show(err.Message); + } } + //port.Close(); - + } void AddLog(string message) @@ -161,7 +254,21 @@ namespace Gidrolock_Modbus_Scanner await SendMessageAsync(functionCode, address, length); } + + private void OnSelectedFunctionChanged(object sender, EventArgs e) + { + if (CBox_Function.SelectedIndex < 4) + UpDown_Value.Enabled = false; + else + { + if (CBox_Function.SelectedIndex == 4 || CBox_Function.SelectedIndex == 6) + UpDown_Value.Maximum = 1; + else UpDown_Value.Maximum = 65535; + + UpDown_Value.Enabled = true; + } + } } } -public enum FunctionCode { Coil, DiscreteInput, HoldingRegister, InputRegister} +public enum FunctionCode { Coil, DiscreteInput, HoldingRegister, InputRegister, WriteCoil, WriteRegister, WriteMultCoils, WriteMultRegisters }; diff --git a/Modbus.cs b/Modbus.cs index 8d94ac4..157bdac 100644 --- a/Modbus.cs +++ b/Modbus.cs @@ -62,7 +62,7 @@ namespace Gidrolock_Modbus_Scanner } #endregion - #region Function 3 - Read Holding Registers + #region Read Functions public static async Task ReadRegAsync(SerialPort port, FunctionCode functionCode, byte address, ushort start, ushort length) { //Ensure port is open: @@ -100,6 +100,48 @@ namespace Gidrolock_Modbus_Scanner } #endregion + #region Write Single Coil/Register + public static async Task WriteSingle(SerialPort port, FunctionCode functionCode, byte address, ushort start, uint value) + { + MessageBox.Show("Not implemented"); + return false; + /* + if (port.IsOpen) + { + //Clear in/out buffers: + port.DiscardOutBuffer(); + port.DiscardInBuffer(); + } + else + { + MessageBox.Show("Порт не открыт"); + return false; + } + */ + } + + public static bool ParseResponse(byte[] res, ref string verbose) + { + try + { + int dataLength = (int)res[2]; + if (res.Length < dataLength + 4) + { + verbose = "Сообщение устройства не соответствует ожидаемой длине!"; + return false; + } + + //TODO: Check CRC + return true; + } + catch + { + verbose = "Сообщение устройства не соответствует ожидаемой длине!"; + return false; + } + + } + /// /// Parses a byte array into a string. /// diff --git a/README.md b/README.md index ed2acf9..25d9edb 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,12 @@ # Gidrolock Modbus Scanner Modbus , Gidrolock. - Holding Register' 128 (Modbus ID). . + + "" Holding Register' `128` ( Modbus ID Gidrolock). + + . ## To-Do -1. / +1. +2. 2. `.json` -3. \ No newline at end of file +3. \ No newline at end of file