-
Notifications
You must be signed in to change notification settings - Fork 1
/
modbussend.c
149 lines (129 loc) · 3.48 KB
/
modbussend.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (C) 2020 Thomas Steger
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <vector>
#include "modbus.h"
std::vector<std::string> split(const std::string& input, const std::string& separator = " ")
{
std::vector<std::string> result;
std::string::size_type position, start = 0;
while (std::string::npos != (position = input.find(separator, start)))
{
result.push_back(input.substr(start, position-start));
start = position + separator.size();
}
result.push_back(input.substr(start));
return result;
}
int main( int argc, char* argv[] )
{
if (argc < 2)
{
printf("Error: No Device\n");
printf("Usage: modbussend [device]\n");
exit(0);
}
std::string device(argv[1]);
cModbus ctx(device, 115200, 0, 8, 1);
ctx.SetDebug(true);
ctx.SetSlave(1);
if (ctx.Connect() == -1)
exit(0);
printf("modbussend: send command or receive commands over modbus\n");
printf("Usage: send [HEX] - Send [HEX] Command and receive response\n");
printf(" receive - Receive Request\n");
std::string msg;
std::vector<std::string> command;
uint command_num;
for (;;)
{
msg.clear();
command.clear();
command_num = 0;
printf ("Modbus:> ");
std::getline (std::cin,msg);
if (msg.back() == '\n' || msg.back() == '\r')
msg.pop_back();
command = split(msg);
command_num = command.size(); //number of strings in command
if (command.at(0).compare("send") == 0)
{
if (command_num < 2)
{
printf("Error wrong Parameterlist\n");
}
else
{
uint8_t resp[MODBUS_RTU_MAX_ADU_LENGTH];
int resp_lenght = 0;
int i=0;
std::vector<uint8_t> req;
for (i = 0; i < command.at(1).length(); i += 2)
{
uint8_t byte;
std::string byteString = command.at(1).substr(i, 2);
try
{
byte = (uint8_t) stoi(byteString, NULL, 16); //covert string to HEX Value
}
catch (...)
{
printf("Error can not convert Input to HEX\n");
continue;
}
req.push_back(byte);
}
printf("sending Request\n");
resp_lenght = ctx.SendRequest(req.data(), req.size(), resp);
if (resp_lenght>0)
{
printf("Response received: %d Bytes", resp_lenght);
int n = 0;
for (n=0; n<resp_lenght; n++)
{
printf("0x%02X, ", resp[n]);
}
printf("\n");
resp_lenght = 0;
}
}
}
else if (command.at(0).compare("receive") == 0)
{
uint8_t req[MODBUS_RTU_MAX_ADU_LENGTH];
int req_lenght = 0;
printf("Waiting for Request:\n");
req_lenght = ctx.ReceiveRequest(req);
if (req_lenght>0)
{
printf("Request received: %d Bytes", req_lenght);
int n = 0;
for (n=0; n<req_lenght; n++)
{
printf("0x%02X, ", req[n]);
}
printf("\n");
req_lenght = 0;
}
}
}
return 0;
}