-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathProgram.cs
More file actions
75 lines (64 loc) · 3.32 KB
/
Program.cs
File metadata and controls
75 lines (64 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// int[] registers = EasyModbus.ModbusClient.ConvertStringToRegisters("hello");
// SerialPort serialport = new SerialPort("COM3");
/* serialport.PortName = "COM3";
serialport.BaudRate = 9600;
serialport.Parity = Parity.None;
serialport.StopBits = StopBits.One;
byte[] buffer = new byte[50];
serialport.Open();
byte[] bufferout = new byte[50];
int numberOfBytesRead = 0;
do
{
int quantity = serialport.Read(buffer, 0, 15);
Buffer.BlockCopy(buffer, 0, bufferout, numberOfBytesRead, quantity);
numberOfBytesRead = numberOfBytesRead + quantity;
}
while (numberOfBytesRead < 5);
for (int i = 0; i < 15; i++)
Console.WriteLine(bufferout[i].ToString());
serialport.Write("ddddddddd");*/
EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient("COM3");
modbusClient.Baudrate = 19200;
//modbusClient.SerialPort = "COM3";
//EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient("127.0.0.1", 502);
//modbusClient.LogFileFilename = "logfile.txt";
modbusClient.UnitIdentifier = 247;
modbusClient.Connect();
while (true)
{
// Console.WriteLine("Execute FC5");
// modbusClient.WriteSingleCoil(0, true);
// Console.WriteLine("Execute FC6");
// modbusClient.WriteSingleRegister(0, 1234);
// Console.WriteLine("Execute FC15");
// modbusClient.WriteMultipleCoils(0, new bool[] { true, false, true, false, true, false, true });
//Console.WriteLine("Execute FC16");
//modbusClient.WriteMultipleRegisters(0, EasyModbus.ModbusClient.ConvertStringToRegisters("hallo2"));
//modbusClient.Disconnect();
//System.Threading.Thread.Sleep(100);
//modbusClient.Connect();
//Console.WriteLine("Execute FC3");
//Console.WriteLine("Value of Holding Register 1000: " + modbusClient.ReadHoldingRegisters(1000, 1)[0]);
DateTime datetimeStart = DateTime.Now;
for (int i = 1; i < 125; i++)
{
modbusClient.WriteSingleRegister(i,i);
Console.WriteLine(modbusClient.ReadInputRegisters(i, 125)[0]);
}
DateTime datetimeEnd = DateTime.Now;
Console.WriteLine("Time elapsed: " + (datetimeEnd - datetimeStart));
// System.Threading.Thread.Sleep(1000);
}
modbusClient.Disconnect();
Console.ReadKey();
}
}
}