Skip to content

Commit b56877b

Browse files
Version 2.8
Static Methods to Read and Write Strings
1 parent 5e4c998 commit b56877b

File tree

9 files changed

+111
-22
lines changed

9 files changed

+111
-22
lines changed

ConsoleApplication1/Program.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,51 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6-
6+
using System;
7+
using System.Net.Sockets;
8+
using System.Net;
9+
using System.IO.Ports;
10+
using System.Reflection;
711
namespace ConsoleApplication1
812
{
913
class Program
1014
{
1115
static void Main(string[] args)
1216
{
13-
EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient("COM3");
17+
int[] registers = EasyModbus.ModbusClient.ConvertStringToRegisters("hello");
18+
SerialPort serialport = new SerialPort("COM3");
19+
/* serialport.PortName = "COM3";
20+
serialport.BaudRate = 9600;
21+
serialport.Parity = Parity.None;
22+
serialport.StopBits = StopBits.One;
23+
byte[] buffer = new byte[50];
24+
serialport.Open();
25+
byte[] bufferout = new byte[50];
26+
int numberOfBytesRead = 0;
27+
do
28+
{
29+
int quantity = serialport.Read(buffer, 0, 15);
30+
Buffer.BlockCopy(buffer, 0, bufferout, numberOfBytesRead, quantity);
31+
numberOfBytesRead = numberOfBytesRead + quantity;
32+
}
33+
while (numberOfBytesRead < 5);
34+
for (int i = 0; i < 15; i++)
35+
Console.WriteLine(bufferout[i].ToString());
36+
serialport.Write("ddddddddd");*/
37+
EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient("192.168.178.75", 502);
1438
modbusClient.Connect();
15-
Console.WriteLine("Execute FC5");
16-
modbusClient.WriteSingleCoil(0, true);
17-
Console.WriteLine("Execute FC6");
18-
modbusClient.WriteSingleRegister(0, 1234);
19-
Console.WriteLine("Execute FC15");
20-
modbusClient.WriteMultipleCoils(0, new bool[] { true, false, true, false, true, false, true });
39+
// Console.WriteLine("Execute FC5");
40+
// modbusClient.WriteSingleCoil(0, true);
41+
// Console.WriteLine("Execute FC6");
42+
// modbusClient.WriteSingleRegister(0, 1234);
43+
// Console.WriteLine("Execute FC15");
44+
// modbusClient.WriteMultipleCoils(0, new bool[] { true, false, true, false, true, false, true });
2145
Console.WriteLine("Execute FC16");
22-
modbusClient.WriteMultipleRegisters(5, new int[] { 1, 2, 3, 4, 5, 6, 6 });
23-
Console.WriteLine("Execute FC3");
24-
Console.WriteLine("Value of Holding Register 1000: " + modbusClient.ReadHoldingRegisters(1000, 1)[0]);
25-
Console.ReadKey();
46+
modbusClient.WriteMultipleRegisters(0, EasyModbus.ModbusClient.ConvertStringToRegisters("hallo2"));
47+
// Console.WriteLine("Execute FC3");
48+
// Console.WriteLine("Value of Holding Register 1000: " + modbusClient.ReadHoldingRegisters(1000, 1)[0]);
49+
50+
Console.ReadKey();
2651
}
2752
}
2853
}

EasyModbus/AdvancedModbusClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Text;
54

65
namespace EasyModbus
76
{
87
public partial class ModbusClient
98
{
9+
/*
1010
public enum DataType { Short = 0, UShort = 1, Long = 2, ULong = 3, Float = 4, Double = 5 };
1111
public object[] ReadHoldingRegisters(int startingAddress, int quantity, DataType dataType, RegisterOrder registerOrder)
1212
{
@@ -27,5 +27,6 @@ public object[] ReadHoldingRegisters(int startingAddress, int quantity, DataType
2727
2828
2929
}
30+
*/
3031
}
3132
}

EasyModbus/ModbusClient.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,49 @@ public static int[] ConvertDoubleToTwoRegisters(Int32 doubleValue, RegisterOrder
309309
return returnValue;
310310
}
311311

312+
313+
/// <summary>
314+
/// Converts 16 - Bit Register values to String
315+
/// </summary>
316+
/// <param name="registers">Register array received via Modbus</param>
317+
/// <param name="offset">First Register containing the String to convert</param>
318+
/// <param name="stringLength">number of characters in String (must be even)</param>
319+
/// <returns>Converted String</returns>
320+
public static string ConvertRegistersToString(int[] registers, int offset, int stringLength)
321+
{
322+
byte[] result = new byte[stringLength];
323+
byte[] registerResult = new byte[2];
324+
325+
for (int i = 0; i < stringLength/2; i++)
326+
{
327+
registerResult = BitConverter.GetBytes(registers[offset + i]);
328+
result[i * 2] = registerResult[0];
329+
result[i * 2 + 1] = registerResult[1];
330+
}
331+
return System.Text.Encoding.Default.GetString(result);
332+
}
333+
334+
/// <summary>
335+
/// Converts a String to 16 - Bit Registers
336+
/// </summary>
337+
/// <param name="registers">Register array received via Modbus</param>
338+
/// <returns>Converted String</returns>
339+
public static int[] ConvertStringToRegisters(string stringToConvert)
340+
{
341+
byte[] array = System.Text.Encoding.ASCII.GetBytes(stringToConvert);
342+
int[] returnarray = new int[stringToConvert.Length / 2 + stringToConvert.Length % 2];
343+
for (int i = 0; i < returnarray.Length; i++)
344+
{
345+
returnarray[i] = array[i * 2];
346+
if (i*2 +1< array.Length)
347+
{
348+
returnarray[i] = returnarray[i] | ((int)array[i * 2 + 1] << 8);
349+
}
350+
}
351+
return returnarray;
352+
}
353+
354+
312355
/// <summary>
313356
/// Calculates the CRC16 for Modbus-RTU
314357
/// </summary>

EasyModbus/ModbusServer.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Net;
1111
using System.Threading;
1212
using System.Net.NetworkInformation;
13-
using System.Threading.Tasks;
1413
using System.IO.Ports;
1514

1615
namespace EasyModbus
@@ -73,6 +72,8 @@ internal class TCPHandler
7372

7473
public int NumberOfConnectedClients { get; set; }
7574

75+
public string ipAddress = null;
76+
7677
public TCPHandler(int port)
7778
{
7879

@@ -82,13 +83,33 @@ public TCPHandler(int port)
8283
server.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
8384
}
8485

86+
public TCPHandler(string ipAddress, int port)
87+
{
88+
this.ipAddress = ipAddress;
89+
IPAddress localAddr = IPAddress.Any;
90+
server = new TcpListener(localAddr, port);
91+
server.Start();
92+
server.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
93+
}
94+
95+
8596
private void AcceptTcpClientCallback(IAsyncResult asyncResult)
8697
{
8798
TcpClient tcpClient = new TcpClient();
8899
try
89100
{
90101
tcpClient = server.EndAcceptTcpClient(asyncResult);
91102
tcpClient.ReceiveTimeout = 4000;
103+
if (ipAddress != null)
104+
{
105+
string ipEndpoint = tcpClient.Client.RemoteEndPoint.ToString();
106+
ipEndpoint = ipEndpoint.Split(':')[0];
107+
if (ipEndpoint != ipAddress)
108+
{
109+
tcpClient.Client.Disconnect(false);
110+
return;
111+
}
112+
}
92113
}
93114
catch (Exception) { }
94115
try
@@ -365,9 +386,9 @@ private void ListenerThread()
365386
if (debug) StoreLogData.Instance.Store("EasyModbus RTU-Server listing for incomming data at Serial Port " + serialPort, System.DateTime.Now);
366387
serialport = new SerialPort();
367388
serialport.PortName = serialPort;
368-
serialport.BaudRate = this.baudrate;
369-
serialport.Parity = this.parity;
370-
serialport.StopBits = stopBits;
389+
serialport.BaudRate = this.baudrate;
390+
serialport.Parity = this.parity;
391+
serialport.StopBits = stopBits;
371392
serialport.WriteTimeout = 10000;
372393
serialport.ReadTimeout = 1000;
373394
serialport.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

EasyModbus/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
//
2929
// You can specify all the values or you can use the default the Revision and
3030
// Build Numbers by using the '*' as shown below:
31-
[assembly: AssemblyVersion("2.6.*")]
31+
[assembly: AssemblyVersion("2.8.*")]

EasyModbus/StoreLogData.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Text;
54

65
namespace EasyModbus

EasyModbusAdvancedClient/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
//
2929
// You can specify all the values or you can use the default the Revision and
3030
// Build Numbers by using the '*' as shown below:
31-
[assembly: AssemblyVersion("2.6.*")]
31+
[assembly: AssemblyVersion("2.7.*")]

EasyModbusClientExample/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
//
2929
// You can specify all the values or you can use the default the Revision and
3030
// Build Numbers by using the '*' as shown below:
31-
[assembly: AssemblyVersion("2.6.*")]
31+
[assembly: AssemblyVersion("2.7.*")]

EasyModbusServerSimulator/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
//
2525
// You can specify all the values or you can use the default the Revision and
2626
// Build Numbers by using the '*' as shown below:
27-
[assembly: AssemblyVersion ("2.6.*")]
27+
[assembly: AssemblyVersion ("2.8.*")]

0 commit comments

Comments
 (0)