Remove function restriction from slave, fixed slave logs

This commit is contained in:
Graham
2020-06-14 08:49:37 -04:00
parent 7add7cf3cd
commit ab53f66cbd
11 changed files with 20 additions and 212 deletions
+6 -4
View File
@@ -597,13 +597,15 @@ namespace Modbus.Common
buttonPauseLog.Text = _logPaused ? "Resume" : "Pause";
}
protected void DriverIncommingData(byte[] data)
protected void DriverIncommingData(byte[] data, int len)
{
if (_logPaused)
return;
var hex = new StringBuilder(data.Length * 2);
foreach (byte b in data)
hex.AppendFormat("{0:x2} ", b);
var hex = new StringBuilder(len);
for(int i = 0; i < len; i++)
{
hex.AppendFormat("{0:x2} ", data[i]);
}
AppendLog(String.Format("RX: {0}", hex));
}
+1 -1
View File
@@ -64,7 +64,7 @@ namespace ModbusLib
length = CacheSize;
//read the data from the physical port
Port.Receive(buffer, length, SocketFlags.None);
Protocol.OnIncommingData(buffer);
Protocol.OnIncommingData(buffer, length);
//append the data to the writer
if (writer == null)
writer = new ByteArrayWriter();
+1 -1
View File
@@ -55,7 +55,7 @@ namespace ModbusLib
Port.ReceiveFrom(
buffer,
ref remote);
Protocol.OnIncommingData(buffer);
Protocol.OnIncommingData(buffer, length);
//try to decode the incoming data
var data = new ServerCommData(Protocol) {IncomingData = new ByteArrayReader(buffer)};
+1 -1
View File
@@ -76,7 +76,7 @@ namespace ModbusLib
buffer,
0,
length);
Protocol.OnIncommingData(buffer);
Protocol.OnIncommingData(buffer, length);
//append the data to the writer
if (writer == null)
writer = new ByteArrayWriter();
+3 -3
View File
@@ -39,9 +39,9 @@ namespace ModbusLib.Protocols
if (OutgoingData != null) OutgoingData(data);
}
public void OnIncommingData(byte[] data)
public void OnIncommingData(byte[] data, int len)
{
if (IncommingData != null) IncommingData(data);
if (IncommingData != null) IncommingData(data, len);
}
/// <summary>
@@ -69,7 +69,7 @@ namespace ModbusLib.Protocols
if (data.OutgoingData != null)
OnOutgoingData(data.OutgoingData.ToArray());
if (data.IncomingData != null)
OnIncommingData(data.IncomingData.ToArray());
OnIncommingData(data.IncomingData.ToArray(), data.IncomingData.Length);
return rVal;
}
+1 -1
View File
@@ -70,7 +70,7 @@ namespace ModbusLib.Protocols
public const byte ErrorMemoryParity = 8;
public delegate void OutgoingData(byte[] data);
public delegate void IncommingData(byte[] data);
public delegate void IncommingData(byte[] data, int len);
public ModbusCommand(byte fc)
{
+2 -2
View File
@@ -41,9 +41,9 @@ namespace ModbusLib.Protocols
if (OutgoingData != null) OutgoingData(data);
}
public void OnIncommingData(byte[] data)
public void OnIncommingData(byte[] data, int len)
{
if (IncommingData != null) IncommingData(data);
if (IncommingData != null) IncommingData(data, len);
}
/// <summary>
+1 -1
View File
@@ -29,7 +29,7 @@ namespace ModbusLib.Protocols
event ModbusCommand.IncommingData IncommingData;
void OnOutgoingData(byte[] data);
void OnIncommingData(byte[] data);
void OnIncommingData(byte[] data, int len);
}
-4
View File
@@ -2,10 +2,6 @@
{
partial class MasterForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
-73
View File
@@ -2,10 +2,6 @@
{
partial class SlaveForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
@@ -31,11 +27,6 @@
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SlaveForm));
this.buttonDisconnect = new System.Windows.Forms.Button();
this.btnConnect = new System.Windows.Forms.Button();
this.groupBoxFunctions = new System.Windows.Forms.GroupBox();
this.radioButtonCoilStatus = new System.Windows.Forms.RadioButton();
this.radioButtonInputStatus = new System.Windows.Forms.RadioButton();
this.radioButtonInputRegisters = new System.Windows.Forms.RadioButton();
this.radioButtonHoldingRegister = new System.Windows.Forms.RadioButton();
this.groupBox4.SuspendLayout();
this.groupBox3.SuspendLayout();
this.grpStart.SuspendLayout();
@@ -43,7 +34,6 @@
this.groupBoxMode.SuspendLayout();
this.groupBoxTCP.SuspendLayout();
this.grpExchange.SuspendLayout();
this.groupBoxFunctions.SuspendLayout();
this.SuspendLayout();
//
// label8
@@ -72,66 +62,10 @@
this.btnConnect.Text = "Listen";
this.btnConnect.Click += new System.EventHandler(this.BtnConnectClick);
//
// groupBoxFunctions
//
this.groupBoxFunctions.Controls.Add(this.radioButtonCoilStatus);
this.groupBoxFunctions.Controls.Add(this.radioButtonInputStatus);
this.groupBoxFunctions.Controls.Add(this.radioButtonInputRegisters);
this.groupBoxFunctions.Controls.Add(this.radioButtonHoldingRegister);
this.groupBoxFunctions.Location = new System.Drawing.Point(298, 144);
this.groupBoxFunctions.Name = "groupBoxFunctions";
this.groupBoxFunctions.Size = new System.Drawing.Size(196, 110);
this.groupBoxFunctions.TabIndex = 36;
this.groupBoxFunctions.TabStop = false;
this.groupBoxFunctions.Text = "Function";
//
// radioButtonCoilStatus
//
this.radioButtonCoilStatus.Location = new System.Drawing.Point(6, 19);
this.radioButtonCoilStatus.Name = "radioButtonCoilStatus";
this.radioButtonCoilStatus.Size = new System.Drawing.Size(184, 21);
this.radioButtonCoilStatus.TabIndex = 4;
this.radioButtonCoilStatus.Tag = "CoilStatus";
this.radioButtonCoilStatus.Text = "01 Coil Status (0x)";
this.radioButtonCoilStatus.CheckedChanged += new System.EventHandler(this.RadioButtonFunctionCheckedChanged);
//
// radioButtonInputStatus
//
this.radioButtonInputStatus.Location = new System.Drawing.Point(6, 40);
this.radioButtonInputStatus.Name = "radioButtonInputStatus";
this.radioButtonInputStatus.Size = new System.Drawing.Size(184, 21);
this.radioButtonInputStatus.TabIndex = 3;
this.radioButtonInputStatus.Tag = "InputStatus";
this.radioButtonInputStatus.Text = "02 Input Status (1x)";
this.radioButtonInputStatus.CheckedChanged += new System.EventHandler(this.RadioButtonFunctionCheckedChanged);
//
// radioButtonInputRegisters
//
this.radioButtonInputRegisters.Location = new System.Drawing.Point(6, 81);
this.radioButtonInputRegisters.Name = "radioButtonInputRegisters";
this.radioButtonInputRegisters.Size = new System.Drawing.Size(184, 21);
this.radioButtonInputRegisters.TabIndex = 6;
this.radioButtonInputRegisters.Tag = "InputRegister";
this.radioButtonInputRegisters.Text = "04 Input Register (3x)";
this.radioButtonInputRegisters.CheckedChanged += new System.EventHandler(this.RadioButtonFunctionCheckedChanged);
//
// radioButtonHoldingRegister
//
this.radioButtonHoldingRegister.Checked = true;
this.radioButtonHoldingRegister.Location = new System.Drawing.Point(6, 61);
this.radioButtonHoldingRegister.Name = "radioButtonHoldingRegister";
this.radioButtonHoldingRegister.Size = new System.Drawing.Size(184, 20);
this.radioButtonHoldingRegister.TabIndex = 5;
this.radioButtonHoldingRegister.TabStop = true;
this.radioButtonHoldingRegister.Tag = "HoldingRegister";
this.radioButtonHoldingRegister.Text = "03 Holding Register (4x)";
this.radioButtonHoldingRegister.CheckedChanged += new System.EventHandler(this.RadioButtonFunctionCheckedChanged);
//
// SlaveForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.ClientSize = new System.Drawing.Size(869, 887);
this.Controls.Add(this.groupBoxFunctions);
this.Controls.Add(this.buttonDisconnect);
this.Controls.Add(this.btnConnect);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
@@ -145,7 +79,6 @@
this.Controls.SetChildIndex(this.groupBox4, 0);
this.Controls.SetChildIndex(this.btnConnect, 0);
this.Controls.SetChildIndex(this.buttonDisconnect, 0);
this.Controls.SetChildIndex(this.groupBoxFunctions, 0);
this.groupBox4.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.grpStart.ResumeLayout(false);
@@ -157,7 +90,6 @@
this.groupBoxTCP.PerformLayout();
this.grpExchange.ResumeLayout(false);
this.grpExchange.PerformLayout();
this.groupBoxFunctions.ResumeLayout(false);
this.ResumeLayout(false);
}
@@ -166,10 +98,5 @@
private System.Windows.Forms.Button buttonDisconnect;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.GroupBox groupBoxFunctions;
private System.Windows.Forms.RadioButton radioButtonCoilStatus;
private System.Windows.Forms.RadioButton radioButtonInputStatus;
private System.Windows.Forms.RadioButton radioButtonInputRegisters;
private System.Windows.Forms.RadioButton radioButtonHoldingRegister;
}
}
+4 -121
View File
@@ -28,25 +28,13 @@ namespace ModbusSlave
private void SlaveFormClosing(object sender, FormClosingEventArgs e)
{
SaveUserData();
DoDisconnect();
}
private void SlaveFormLoading(object sender, EventArgs e)
{
LoadUserData();
}
private void LoadUserData()
{
FunctionCode = Modbus.Common.Properties.Settings.Default.Function;
}
private void SaveUserData()
{
Modbus.Common.Properties.Settings.Default.Function = FunctionCode;
Modbus.Common.Properties.Settings.Default.Save();
}
#endregion
@@ -103,7 +91,6 @@ namespace ModbusSlave
btnConnect.Enabled = false;
buttonDisconnect.Enabled = true;
grpExchange.Enabled = true;
groupBoxFunctions.Enabled = false;
groupBoxTCP.Enabled = false;
groupBoxRTU.Enabled = false;
groupBoxMode.Enabled = false;
@@ -143,7 +130,6 @@ namespace ModbusSlave
buttonDisconnect.Enabled = false;
grpExchange.Enabled = true;
groupBoxMode.Enabled = true;
groupBoxFunctions.Enabled = true;
SetMode();
AppendLog("Disconnected");
}
@@ -195,130 +181,27 @@ namespace ModbusSlave
switch (command.FunctionCode)
{
case ModbusCommand.FuncReadCoils:
if (_function == Function.CoilStatus)
DoRead(command);
else
IllegalFunction(command);
break;
case ModbusCommand.FuncReadInputDiscretes:
if (_function == Function.InputStatus)
DoRead(command);
else
IllegalFunction(command);
break;
case ModbusCommand.FuncReadInputRegisters:
if (_function == Function.InputRegister)
DoRead(command);
else
IllegalFunction(command);
break;
case ModbusCommand.FuncReadMultipleRegisters:
if (_function == Function.HoldingRegister)
DoRead(command);
else
IllegalFunction(command);
case ModbusCommand.FuncReadCustom:
DoRead(command);
break;
case ModbusCommand.FuncWriteCoil:
if (_function == Function.CoilStatus)
DoWrite(command);
else
IllegalFunction(command);
break;
case ModbusCommand.FuncForceMultipleCoils:
if (_function == Function.CoilStatus)
DoWrite(command);
else
IllegalFunction(command);
break;
case ModbusCommand.FuncWriteMultipleRegisters:
if (_function == Function.HoldingRegister)
DoWrite(command);
else
IllegalFunction(command);
break;
case ModbusCommand.FuncWriteSingleRegister:
if (_function == Function.HoldingRegister)
DoWrite(command);
else
IllegalFunction(command);
DoWrite(command);
break;
case ModbusCommand.FuncReadExceptionStatus:
//TODO
break;
case ModbusCommand.FuncReadCustom:
if (_function == Function.HoldingRegister)
{
for (int i = 0; i < command.Count; i++)
command.Data[i] = _registerData[command.Offset + i];
}
else
IllegalFunction(command);
break;
default:
AppendLog(String.Format("Illegal Function, expecting function code {0}.", command.FunctionCode));
//return an exception
command.ExceptionCode = ModbusCommand.ErrorIllegalFunction;
break;
}
}
private void IllegalFunction(ModbusCommand command)
{
AppendLog(String.Format("Illegal Function, expecting function code {0}.", FunctionCode));
command.ExceptionCode = ModbusCommand.ErrorIllegalFunction;
}
private byte FunctionCode
{
get
{
byte rVal = ModbusCommand.ErrorIllegalFunction;
switch (_function)
{
case Function.CoilStatus:
rVal = ModbusCommand.FuncReadCoils;
break;
case Function.HoldingRegister:
rVal = ModbusCommand.FuncReadMultipleRegisters;
break;
case Function.InputRegister:
rVal = ModbusCommand.FuncReadInputRegisters;
break;
case Function.InputStatus:
rVal = ModbusCommand.FuncReadInputDiscretes;
break;
}
return rVal;
}
set
{
switch (value)
{
case ModbusCommand.FuncReadCoils:
radioButtonCoilStatus.Checked = true;
_function = Function.CoilStatus;
break;
case ModbusCommand.FuncReadInputDiscretes:
radioButtonInputStatus.Checked = true;
_function = Function.InputStatus;
break;
case ModbusCommand.FuncReadInputRegisters:
radioButtonInputRegisters.Checked = true;
_function = Function.InputRegister;
break;
case ModbusCommand.FuncReadMultipleRegisters:
radioButtonHoldingRegister.Checked = true;
_function = Function.HoldingRegister;
break;
}
}
}
private void DoRead(ModbusCommand command)
{
for (int i = 0; i < command.Count; i++)