Skip to content

Commit f0d0d19

Browse files
Push to Nuget
1 parent b8f5ac6 commit f0d0d19

File tree

6 files changed

+110
-48
lines changed

6 files changed

+110
-48
lines changed

EasyModbusClientExample/Properties/Resources.Designer.cs

Lines changed: 30 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EasyModbusServerSimulator/EasyModbusServerSimulator.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
<DefineConstants>TRACE;DEBUG;SSL</DefineConstants>
8080
<Prefer32Bit>false</Prefer32Bit>
8181
</PropertyGroup>
82+
<PropertyGroup>
83+
<ApplicationIcon>oie_ITkzMZD7tnyn.ico</ApplicationIcon>
84+
</PropertyGroup>
8285
<ItemGroup>
8386
<Reference Include="System" />
8487
<Reference Include="System.Data" />
@@ -158,5 +161,11 @@
158161
<Install>true</Install>
159162
</BootstrapperPackage>
160163
</ItemGroup>
164+
<ItemGroup>
165+
<None Include="Resources\small.png" />
166+
</ItemGroup>
167+
<ItemGroup>
168+
<Content Include="oie_ITkzMZD7tnyn.ico" />
169+
</ItemGroup>
161170
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
162171
</Project>

EasyModbusServerSimulator/Properties/Resources.Designer.cs

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EasyModbus_NET5/EasyModbus_V5.0.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net5.0</TargetFrameworks>
5+
<AssemblyName>EasyModbus</AssemblyName>
6+
<RootNamespace>EasyModbus</RootNamespace>
7+
<SignAssembly>false</SignAssembly>
58
</PropertyGroup>
69

710

EasyModbus_NET5/ModbusClient/EasyModbus.ModbusClient.Core.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,18 @@ public bool[] ReadDiscreteInputs(int startingAddress, int quantity)
180180
request.TransactionIdentifier = (ushort)transactionIdentifierInternal;
181181

182182

183-
byte[] data = new byte[2100];
184-
bool[] response;
183+
ApplicationDataUnit response = new ApplicationDataUnit(2);
184+
response.QuantityRead = (ushort)quantity;
185+
186+
byte[] data = new byte[255];
185187
if (serialport != null)
186188
{
187189
dataReceived = false;
188190
if (quantity % 8 == 0)
189191
bytesToRead = 5 + quantity / 8;
190192
else
191193
bytesToRead = 6 + quantity / 8;
192-
// serialport.ReceivedBytesThreshold = bytesToRead;
194+
193195
serialport.Write(request.Payload, 6, 8);
194196
if (debug)
195197
{
@@ -204,22 +206,24 @@ public bool[] ReadDiscreteInputs(int startingAddress, int quantity)
204206
SendDataChanged(this);
205207

206208
}
207-
data = new byte[2100];
209+
210+
211+
208212
readBuffer = new byte[256];
209213
DateTime dateTimeSend = DateTime.UtcNow;
210-
byte receivedUnitIdentifier = 0xFF;
211214

215+
response.UnitIdentifier = 0xFF;
212216

213-
while (receivedUnitIdentifier != unitIdentifier & !((DateTime.UtcNow.Ticks - dateTimeSend.Ticks) > TimeSpan.TicksPerMillisecond * connectTimeout))
217+
while (response.UnitIdentifier != unitIdentifier & !((DateTime.UtcNow.Ticks - dateTimeSend.Ticks) > TimeSpan.TicksPerMillisecond * connectTimeout))
214218
{
215219
while (dataReceived == false & !((DateTime.UtcNow.Ticks - dateTimeSend.Ticks) > TimeSpan.TicksPerMillisecond * connectTimeout))
216220
System.Threading.Thread.Sleep(1);
217-
data = new byte[2100];
221+
data = new byte[255];
218222
Array.Copy(readBuffer, 0, data, 6, readBuffer.Length);
219-
receivedUnitIdentifier = data[6];
223+
220224
}
221-
if (receivedUnitIdentifier != unitIdentifier)
222-
data = new byte[2100];
225+
if (response.UnitIdentifier != unitIdentifier)
226+
data = new byte[255];
223227
else
224228
countRetries = 0;
225229
}
@@ -250,8 +254,8 @@ public bool[] ReadDiscreteInputs(int startingAddress, int quantity)
250254
Array.Copy(data, 0, sendData, 0, data.Length - 2);
251255
SendDataChanged(this);
252256
}
253-
data = new Byte[2100];
254-
int NumberOfBytes = stream.Read(data, 0, data.Length);
257+
data = new Byte[255];
258+
int NumberOfBytes = stream.Read(response.Payload, 0, response.Payload.Length);
255259
if (ReceiveDataChanged != null)
256260
{
257261
receiveData = new byte[NumberOfBytes];
@@ -313,14 +317,8 @@ public bool[] ReadDiscreteInputs(int startingAddress, int quantity)
313317
}
314318
}
315319
}
316-
response = new bool[quantity];
317-
for (int i = 0; i < quantity; i++)
318-
{
319-
int intData = data[9 + i / 8];
320-
int mask = Convert.ToInt32(Math.Pow(2, (i % 8)));
321-
response[i] = Convert.ToBoolean((intData & mask) / mask);
322-
}
323-
return (response);
320+
321+
return response.RegisterDataBool;
324322
}
325323

326324

EasyModbus_NET5/ModbusClient/EasyModbus.ModbusClient.Protocol.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ class ProtocolDataUnit
2727
public byte ByteCount { get; }
2828
public byte ErroCode { get; }
2929
public byte ExceptionCode { get; }
30-
public object[] RegisterData { get; set; }
30+
public int[] RegisterDataInt { get; set; }
31+
32+
public bool[] RegisterDataBool { get; set; }
33+
34+
3135

3236
public byte[] Data
3337
{
38+
//return the data in case of a request
3439
get
3540
{
3641
Byte[] returnvalue = null;
@@ -62,6 +67,27 @@ public byte[] Data
6267
}
6368
return returnvalue;
6469
}
70+
//set the data in case of a response
71+
set
72+
{
73+
switch (FunctionCode)
74+
{
75+
// FC 01: Read Coils and 02 Read Discrete Inputs provide the same response
76+
case 1: case 2:
77+
byte byteCount = value[1];
78+
RegisterDataBool = new bool[QuantityRead];
79+
for (int i = 0; i < QuantityRead; i++)
80+
{
81+
int intData = data[i / 8];
82+
int mask = Convert.ToInt32(Math.Pow(2, (i % 8)));
83+
RegisterDataBool[i] = (Convert.ToBoolean((intData & mask) / mask));
84+
}
85+
break;
86+
87+
}
88+
}
89+
90+
6591
}
6692

6793
}
@@ -103,13 +129,13 @@ public byte[] Mbap_Header
103129
ushort length = 0x0006;
104130
if (FunctionCode == 15)
105131
{
106-
byte byteCount = (byte)((RegisterData.Length % 8 != 0 ? RegisterData.Length / 8 + 1 : (RegisterData.Length / 8)));
132+
byte byteCount = (byte)((RegisterDataBool.Length % 8 != 0 ? RegisterDataBool.Length / 8 + 1 : (RegisterDataBool.Length / 8)));
107133
length = (ushort)(7 + byteCount);
108134
}
109135
if (FunctionCode == 16)
110-
length = (ushort)(7 + RegisterData.Length * 2);
136+
length = (ushort)(7 + RegisterDataInt.Length * 2);
111137
if (FunctionCode == 23)
112-
length = (ushort)(11 + RegisterData.Length * 2);
138+
length = (ushort)(11 + RegisterDataInt.Length * 2);
113139

114140
Byte[] returnvalue = new byte[]
115141
{
@@ -123,13 +149,13 @@ public byte[] Mbap_Header
123149
};
124150

125151
return returnvalue;
126-
127152
}
128153
}
129154

130155

131156

132157
public byte[] Payload {
158+
// Return the Payload in case of a request
133159
get
134160
{
135161
List<byte> returnvalue = new List<byte>();
@@ -141,7 +167,13 @@ public byte[] Payload {
141167
byte [] crc = BitConverter.GetBytes(ModbusClient.calculateCRC(returnvalue.ToArray(), (ushort)(returnvalue.Count - 8), 6));
142168
returnvalue.AddRange(crc);
143169
return returnvalue.ToArray();
144-
170+
}
171+
// Set the Payload in case of a resonse
172+
set
173+
{
174+
175+
TransactionIdentifier = BitConverter.ToUInt16(value, 0);
176+
UnitIdentifier = value[6];
145177

146178
}
147179

0 commit comments

Comments
 (0)