bandwidth-server-one.c
Go to the documentation of this file.
1 /*
2  * Copyright © 2008-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 
24 #include <modbus.h>
25 
26 enum {
27  TCP,
29 };
30 
31 int main(int argc, char *argv[])
32 {
33  int socket;
34  modbus_t *ctx;
36  int rc;
37  int use_backend;
38 
39  /* TCP */
40  if (argc > 1) {
41  if (strcmp(argv[1], "tcp") == 0) {
42  use_backend = TCP;
43  } else if (strcmp(argv[1], "rtu") == 0) {
44  use_backend = RTU;
45  } else {
46  printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwith\n\n", argv[0]);
47  exit(1);
48  }
49  } else {
50  /* By default */
51  use_backend = TCP;
52  }
53 
54  if (use_backend == TCP) {
55  ctx = modbus_new_tcp("127.0.0.1", 1502);
56  socket = modbus_tcp_listen(ctx, 1);
57  modbus_tcp_accept(ctx, &socket);
58 
59  } else {
60  ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
61  modbus_set_slave(ctx, 1);
62  modbus_connect(ctx);
63  }
64 
67  if (mb_mapping == NULL) {
68  fprintf(stderr, "Failed to allocate the mapping: %s\n",
69  modbus_strerror(errno));
70  modbus_free(ctx);
71  return -1;
72  }
73 
74  for(;;) {
75  uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
76 
77  rc = modbus_receive(ctx, query);
78  if (rc >= 0) {
79  modbus_reply(ctx, query, rc, mb_mapping);
80  } else {
81  /* Connection closed by the client or server */
82  break;
83  }
84  }
85 
86  printf("Quit the loop: %s\n", modbus_strerror(errno));
87 
88  modbus_mapping_free(mb_mapping);
89  close(socket);
90  modbus_free(ctx);
91 
92  return 0;
93 }
modbus_mapping_t * modbus_mapping_new(int nb_coil_status, int nb_input_status, int nb_holding_registers, int nb_input_registers)
Definition: modbus.c:1547
int modbus_receive(modbus_t *ctx, uint8_t *req)
Definition: modbus.c:460
#define MODBUS_TCP_MAX_ADU_LENGTH
Definition: modbus-tcp.h:41
void modbus_free(modbus_t *ctx)
Definition: modbus.c:1528
#define MODBUS_MAX_READ_REGISTERS
Definition: modbus.h:80
int modbus_set_slave(modbus_t *ctx, int slave)
Definition: modbus.c:1465
int modbus_reply(modbus_t *ctx, const uint8_t *req, int req_length, modbus_mapping_t *mb_mapping)
Definition: modbus.c:641
int modbus_tcp_accept(modbus_t *ctx, int *socket)
Definition: modbus-tcp.c:524
int main(int argc, char *argv[])
int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
Definition: modbus-tcp.c:387
modbus_t * ctx
const char * modbus_strerror(int errnum)
Definition: modbus.c:53
void modbus_mapping_free(modbus_mapping_t *mb_mapping)
Definition: modbus.c:1625
modbus_t * modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit)
Definition: modbus-rtu.c:917
int modbus_connect(modbus_t *ctx)
Definition: modbus.c:1515
modbus_mapping_t * mb_mapping
#define MODBUS_MAX_READ_BITS
Definition: modbus.h:70
modbus_t * modbus_new_tcp(const char *ip_address, int port)
Definition: modbus-tcp.c:636


libmodbus
Author(s):
autogenerated on Sat Nov 21 2020 03:17:32