Added serial port mode configuration option (-m).

This commit is contained in:
Victor Antonovich 2015-06-08 18:36:29 +04:00
parent ef654a9921
commit e2a9f262e9
7 changed files with 89 additions and 19 deletions

View File

@ -1,4 +1,4 @@
.TH "mbusd" 8 "23 February 2015" "mbusd @VERSION@"
.TH "mbusd" 8 "8 June 2015" "mbusd @VERSION@"
.SH NAME
mbusd \- MODBUS/TCP to MODBUS/RTU gateway.
.SH SYNOPSIS
@ -14,6 +14,8 @@ mbusd \- MODBUS/TCP to MODBUS/RTU gateway.
.IR device ]
.RB [ -s
.IR speed ]
.RB [ -m
.IR mode ]
.RB [ -P
.IR port ]
.RB [ -C
@ -47,6 +49,8 @@ Specifies log file name ('-' for logging to STDOUT only, default /var/log/mbusd.
Specifies serial port device name.
.IP "\fB-s \fIspeed\fR"
Specifies serial port speed.
.IP "\fB-m \fIspeed\fR"
Specifies serial port mode (like 8N1).
.IP "\fB-P \fIport\fR"
Specifies TCP port number.
.IP "\fB-C \fImaxconn\fR"

View File

@ -4,18 +4,18 @@
* cfg.c - configuration related procedures
*
* Copyright (c) 2002-2003, 2013, Victor Antonovich (v.antonovich@gmail.com)
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -39,7 +39,7 @@ cfg_t cfg;
/*
* Setting up config defaults
*/
void
void
cfg_init(void)
{
#ifdef LOG
@ -48,6 +48,7 @@ cfg_init(void)
#endif
strncpy(cfg.ttyport, DEFAULT_PORT, INTBUFSIZE);
cfg.ttyspeed = DEFAULT_SPEED;
cfg.ttymode = DEFAULT_MODE;
#ifdef TRXCTL
cfg.trxcntl = TRX_ADDC;
#endif

View File

@ -4,18 +4,18 @@
* cfg.h - configuration related procedures
*
* Copyright (c) 2002-2003, 2013, Victor Antonovich (v.antonovich@gmail.com)
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -52,6 +52,8 @@ typedef struct
char ttyport[INTBUFSIZE + 1];
/* tty speed */
int ttyspeed;
/* tty mode */
char *ttymode;
/* trx control type (0 - ADDC, 1 - by RTS) */
int trxcntl;
/* TCP server port number */
@ -74,4 +76,4 @@ typedef struct
extern cfg_t cfg;
void cfg_init(void);
#endif /* _CFG_H */
#endif /* _CFG_H */

View File

@ -4,18 +4,18 @@
* globals.h - global definitions
*
* Copyright (c) 2002-2003, 2013, Victor Antonovich (v.antonovich@gmail.com)
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -58,6 +58,7 @@
#include <signal.h>
#include <netdb.h>
#include <fcntl.h>
#include <ctype.h>
#ifdef HAVE_LIBUTIL
# include <libutil.h>
#endif

View File

@ -99,13 +99,14 @@ daemon(nochdir, noclose)
void
usage(char *exename)
{
cfg_init();
printf("%s-%s Copyright (C) 2002-2003, 2011, 2013, 2015 Victor Antonovich <v.antonovich@gmail.com>, "
"Andrew Denysenko <nitr0@seti.kr.ua>\n\n"
"Usage: %s [-h] [-d] "
#ifdef TRXCTL
"[-t] "
#endif
"[-v level] [-L logfile] [-p device] [-s speed] [-P port]\n"
"[-v level] [-L logfile] [-p device] [-s speed] [-m mode] [-P port]\n"
" [-C maxconn] [-N retries] [-R pause] [-W wait] [-T timeout]\n\n"
"Options:\n"
" -h : this help\n"
@ -124,6 +125,7 @@ usage(char *exename)
#endif
" -p device : set serial port device name (default %s)\n"
" -s speed : set serial port speed (default %d)\n"
" -m mode : set serial port mode (default %s)\n"
" -P port : set TCP server port number (default %d)\n"
" -C maxconn : set maximum number of simultaneous TCP connections\n"
" (1-128, default %d)\n"
@ -139,7 +141,7 @@ usage(char *exename)
#ifdef LOG
cfg.dbglvl, LOGPATH, LOGNAME,
#endif
cfg.ttyport, cfg.ttyspeed, cfg.serverport, cfg.maxconn,
cfg.ttyport, cfg.ttyspeed, cfg.ttymode, cfg.serverport, cfg.maxconn,
cfg.maxtry, cfg.rqstpause, cfg.respwait, cfg.conntimeout);
exit(0);
}
@ -149,6 +151,7 @@ main(int argc, char *argv[])
{
int err = 0, rc;
char *exename;
char ttyparity;
sig_init();
cfg_init();
@ -167,7 +170,7 @@ main(int argc, char *argv[])
#ifdef LOG
"v:L:"
#endif
"p:s:P:C:N:R:W:T:")) != RC_ERR)
"p:s:m:P:C:N:R:W:T:")) != RC_ERR)
{
switch (rc)
{
@ -229,6 +232,35 @@ main(int argc, char *argv[])
case 's':
cfg.ttyspeed = strtoul(optarg, NULL, 0);
break;
case 'm':
cfg.ttymode = optarg;
/* tty mode sanity checks */
if (strlen(cfg.ttymode) != 3)
{
printf("%s: -m: invalid serial port mode ('%s')\n", exename, cfg.ttymode);
exit(-1);
}
if (cfg.ttymode[0] < '5' || cfg.ttymode[0] > '8')
{
printf("%s: -m: invalid serial port character size "
"(%c, must be 5, 6, 7 or 8)\n",
exename, cfg.ttymode[0]);
exit(-1);
}
ttyparity = toupper(cfg.ttymode[1]);
if (ttyparity != 'N' && ttyparity != 'E' && ttyparity != 'O')
{
printf("%s: -m: invalid serial port parity "
"(%c, must be N, E or O)\n", exename, ttyparity);
exit(-1);
}
if (cfg.ttymode[2] != '1' && cfg.ttymode[2] != '2')
{
printf("%s: -m: invalid serial port stop bits "
"(%c, must be 1 or 2)\n", exename, cfg.ttymode[2]);
exit(-1);
}
break;
case 'P':
cfg.serverport = strtoul(optarg, NULL, 0);
break;

View File

@ -136,7 +136,35 @@ tty_set_attr(ttydata_t *mod)
mod->tios.c_oflag &= ~OPOST;
mod->tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
mod->tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CRTSCTS);
mod->tios.c_cflag |= CS8 | CREAD | CLOCAL ;
mod->tios.c_cflag |= CREAD | CLOCAL ;
switch (cfg.ttymode[0])
{
case '5':
mod->tios.c_cflag |= CS5;
break;
case '6':
mod->tios.c_cflag |= CS6;
break;
case '7':
mod->tios.c_cflag |= CS7;
break;
case '8':
mod->tios.c_cflag |= CS8;
break;
}
switch (toupper(cfg.ttymode[1]))
{
case 'E':
mod->tios.c_cflag |= PARENB;
break;
case 'O':
mod->tios.c_cflag |= (PARENB | PARODD);
break;
}
if (cfg.ttymode[2] == '2')
{
mod->tios.c_cflag |= CSTOPB;
}
#endif
mod->tios.c_cc[VTIME] = 0;
mod->tios.c_cc[VMIN] = 1;

View File

@ -56,6 +56,8 @@
#define DEFAULT_SPEED 19200
#define DEFAULT_BSPEED B19200
#define DEFAULT_MODE "8N1"
/*
* Maximum tty buffer size
*/