Merge pull request #13 from aprsworld/master

add file based RS-485 direction control
This commit is contained in:
Victor Antonovich 2017-02-18 15:31:23 +03:00 committed by GitHub
commit fa5a950171
7 changed files with 71 additions and 14 deletions

View File

@ -48,6 +48,12 @@ Usage:
-h Usage help. -h Usage help.
-d Instruct mbusd not to fork itself (non-daemonize). -d Instruct mbusd not to fork itself (non-daemonize).
-t Enable RTS RS-485 data direction control (if not disabled while compile). -t Enable RTS RS-485 data direction control (if not disabled while compile).
-y file
Enable RS-485 direction data direction control by writing '1' to file
for transmitter enable and '0' to file for transmitter disable
-Y file
Enable RS-485 direction data direction control by writing '0' to file
for transmitter enable and '1' to file for transmitter disable
-v level -v level
Specifies log verbosity level (0 for errors only, 1 for warnings Specifies log verbosity level (0 for errors only, 1 for warnings
and 2 for also information messages.) If mbusd was compiled in debug mode, and 2 for also information messages.) If mbusd was compiled in debug mode,
@ -107,6 +113,9 @@ Andrew Denysenko (<nitr0@seti.kr.ua>):
- RTS RS-485 data direction control - RTS RS-485 data direction control
- RTU response receiving by length - RTU response receiving by length
James Jarvis (<jj@aprsworld.com>):
- file based RS-485 data direction control
License: License:
-------- --------

View File

@ -6,6 +6,10 @@ mbusd \- MODBUS/TCP to MODBUS/RTU gateway.
.RB [ -h ] .RB [ -h ]
.RB [ -d ] .RB [ -d ]
.RB [ -t ] .RB [ -t ]
.RB [ -y
.IR file ]
.RB [ -Y
.IR file ]
.RB [ -v .RB [ -v
.IR level ] .IR level ]
.RB [ -L .RB [ -L
@ -37,6 +41,12 @@ Usage help.
Instruct \fImbusd\fR not to fork itself (non-daemonize). Instruct \fImbusd\fR not to fork itself (non-daemonize).
.IP \fB-t\fR .IP \fB-t\fR
Enable RTS RS-485 data direction control (if not disabled while compile). Enable RTS RS-485 data direction control (if not disabled while compile).
.IP "\fB-y \fIfile\fR"
Enable RS-485 direction data direction control by writing '1' to file
for transmitter enable and '0' to file for transmitter disable
.IP "\fB-Y \fIfile\fR"
Enable RS-485 direction data direction control by writing '0' to file
for transmitter enable and '1' to file for transmitter disable
.IP "\fB-v \fIlevel\fR" .IP "\fB-v \fIlevel\fR"
Specifies log verbosity level. 0 enables logging of errors only, Specifies log verbosity level. 0 enables logging of errors only,
1 also enables warnings and 2 enables information messages. 1 also enables warnings and 2 enables information messages.

View File

@ -54,8 +54,10 @@ typedef struct
int ttyspeed; int ttyspeed;
/* tty mode */ /* tty mode */
char *ttymode; char *ttymode;
/* trx control type (0 - ADDC, 1 - by RTS) */ /* trx control type (0 - ADDC, 1 - by RTS, 2 - by sysfs GPIO with 1 activating transmit, 3 - by sysfs GPIO with 0 activating transmit) */
int trxcntl; int trxcntl;
/* trx control sysfs file */
char trxcntl_file[INTBUFSIZE + 1];
/* TCP server port number */ /* TCP server port number */
int serverport; int serverport;
/* maximum number of connections */ /* maximum number of connections */

View File

@ -248,7 +248,7 @@ conn_write(int d, void *buf, size_t nbytes, int istty)
long delay; long delay;
#ifdef TRXCTL #ifdef TRXCTL
if (istty) { if (istty) {
if (cfg.trxcntl == TRX_RTS) if (cfg.trxcntl != TRX_ADDC )
tty_set_rts(d); tty_set_rts(d);
usleep(35000000l/cfg.ttyspeed); usleep(35000000l/cfg.ttyspeed);
} }
@ -278,7 +278,7 @@ conn_write(int d, void *buf, size_t nbytes, int istty)
usleep(delay); usleep(delay);
#endif #endif
/* tcdrain(d); - hangs sometimes, so make calculated delay */ /* tcdrain(d); - hangs sometimes, so make calculated delay */
if (cfg.trxcntl == TRX_RTS) { if (cfg.trxcntl != TRX_ADDC ) {
tty_clr_rts(d); tty_clr_rts(d);
} }
} }

View File

@ -104,7 +104,7 @@ usage(char *exename)
"Andrew Denysenko <nitr0@seti.kr.ua>\n\n" "Andrew Denysenko <nitr0@seti.kr.ua>\n\n"
"Usage: %s [-h] [-d] " "Usage: %s [-h] [-d] "
#ifdef TRXCTL #ifdef TRXCTL
"[-t] " "[-t] [-y sysfsfile] [-Y sysfsfile]\n"
#endif #endif
"[-v level] [-L logfile] [-p device] [-s speed] [-m mode] [-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" " [-C maxconn] [-N retries] [-R pause] [-W wait] [-T timeout]\n\n"
@ -112,7 +112,9 @@ usage(char *exename)
" -h : this help\n" " -h : this help\n"
" -d : don't daemonize\n" " -d : don't daemonize\n"
#ifdef TRXCTL #ifdef TRXCTL
" -t : enable RTS RS-485 data direction control\n" " -t : enable RTS RS-485 data direction control using RTS\n"
" -y : enable RTS RS-485 data direction control using sysfs file, active transmit\n"
" -Y : enable RTS RS-485 data direction control using sysfs file, active receive\n"
#endif #endif
#ifdef LOG #ifdef LOG
#ifdef DEBUG #ifdef DEBUG
@ -165,7 +167,7 @@ main(int argc, char *argv[])
while ((rc = getopt(argc, argv, while ((rc = getopt(argc, argv,
"dh" "dh"
#ifdef TRXCTL #ifdef TRXCTL
"t" "ty:Y:"
#endif #endif
#ifdef LOG #ifdef LOG
"v:L:" "v:L:"
@ -183,6 +185,14 @@ main(int argc, char *argv[])
case 't': case 't':
cfg.trxcntl = TRX_RTS; cfg.trxcntl = TRX_RTS;
break; break;
case 'y':
cfg.trxcntl = TRX_SYSFS_1;
strncpy(cfg.trxcntl_file, optarg, INTBUFSIZE);
break;
case 'Y':
cfg.trxcntl = TRX_SYSFS_0;
strncpy(cfg.trxcntl_file, optarg, INTBUFSIZE);
break;
#endif #endif
#ifdef LOG #ifdef LOG
case 'v': case 'v':

View File

@ -362,20 +362,44 @@ tty_close(ttydata_t *mod)
} }
#ifdef TRXCTL #ifdef TRXCTL
void sysfs_gpio_set(char *filename, char *value) {
int fd;
fd = open(filename, O_WRONLY);
/* write first byte. Should only be 0 or 1 */
write(fd, value, 1);
close(fd);
logw(9, "tty: sysfs_gpio_set(%s,%s)\n",filename,value);
}
/* Set RTS line to active state */ /* Set RTS line to active state */
void void
tty_set_rts(int fd) tty_set_rts(int fd)
{ {
int mstat = TIOCM_RTS; if ( TRX_RTS == cfg.trxcntl ) {
ioctl(fd, TIOCMBIS, &mstat); int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIS, &mstat);
} else if ( TRX_SYSFS_1 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"1");
} else if ( TRX_SYSFS_0 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"0");
}
} }
/* Set RTS line to passive state */ /* Set RTS line to passive state */
void void
tty_clr_rts(int fd) tty_clr_rts(int fd)
{ {
int mstat = TIOCM_RTS; if ( TRX_RTS == cfg.trxcntl ) {
ioctl(fd, TIOCMBIC, &mstat); int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIC, &mstat);
} else if ( TRX_SYSFS_1 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"0");
} else if ( TRX_SYSFS_0 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"1");
}
} }
#endif #endif

View File

@ -67,8 +67,10 @@
* TRX control types * TRX control types
*/ */
#ifdef TRXCTL #ifdef TRXCTL
#define TRX_ADDC 0 #define TRX_ADDC 0
#define TRX_RTS 1 #define TRX_RTS 1
#define TRX_SYSFS_1 2
#define TRX_SYSFS_0 3
#endif #endif
/* /*
@ -89,7 +91,7 @@ typedef struct
int speed; /* serial port speed */ int speed; /* serial port speed */
char *port; /* serial port device name */ char *port; /* serial port device name */
#ifdef TRXCTL #ifdef TRXCTL
int trxcntl; /* trx control type (0 - Automatic Data Direction Control (ADDC), 1 - by RTS) */ int trxcntl; /* trx control type (enum - see values in config.h) */
#endif #endif
struct termios tios; /* working termios structure */ struct termios tios; /* working termios structure */
struct termios savedtios; /* saved termios structure */ struct termios savedtios; /* saved termios structure */