-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
85 lines (48 loc) · 1.37 KB
/
main.cpp
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
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include "RDSModbusSlave.h"
#include <thread>
using namespace std;
void fun(RDSModbusSlave* server);
int main(int argc, char* argv[])
{
string portStr = "1502";
if(argc>1)
portStr = argv[1];
int port = atoi(portStr.c_str());
cout<<"modbus server listen on port:"<< port << endl;
RDSModbusSlave modServer("0.0.0.0", port);
modServer.run();
if(argc = 2)
{
std::thread td(fun, &modServer);
td.join();
}
int flag=0;
while (1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
flag++;
}
std::cout << "exit modbusServer" << std::endl;
return 0;
}
void fun(RDSModbusSlave* server)
{
int ret=0;
srand(time(0));
while(1)
{
uint16_t value = 1000 + (rand() % 1000);
cout<<"write value:"<<value <<endl;
ret = server->setInputRegisterValue(10, value);
ret = server->setHoldingRegisterValue(10, value);
server->setHoldingRegisterValue(30010, value);
float valuef = value / 100.0;
server->setInputRegisterValue(12, valuef);
server->setHoldingRegisterValue(12, valuef);
server->setHoldingRegisterValue(30012, valuef);
server->setTab_Input_Bits(30010, 1);
server->setTab_Input_Bits(30011, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}