diff --git a/src/cfg.c b/src/cfg.c index 5a4bb10..d28c99e 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -73,7 +73,7 @@ cfg_init(void) cfg.maxtry = DEFAULT_MAXTRY; cfg.rqstpause = DEFAULT_RQSTPAUSE; cfg.respwait = DEFAULT_RESPWAIT; - cfg.resppause = DV(3, cfg.ttyspeed); + cfg.resppause = DV(3, DEFAULT_BITS_PER_CHAR, cfg.ttyspeed); cfg.conntimeout = DEFAULT_CONNTIMEOUT; } diff --git a/src/conn.c b/src/conn.c index cc8ad43..40501d6 100644 --- a/src/conn.c +++ b/src/conn.c @@ -56,11 +56,7 @@ int tty_reopen() { logw(3, "tty re-opening..."); tty_close(&tty); -#ifdef TRXCTL - tty_init(&tty, cfg.ttyport, cfg.ttyspeed, cfg.trxcntl); -#else - tty_init(&tty, cfg.ttyport, cfg.ttyspeed); -#endif + tty_init(&tty); if (tty_open(&tty) != RC_OK) { #ifdef LOG @@ -96,11 +92,7 @@ int conn_init(void) { /* tty device initialization */ -#ifdef TRXCTL - tty_init(&tty, cfg.ttyport, cfg.ttyspeed, cfg.trxcntl); -#else - tty_init(&tty, cfg.ttyport, cfg.ttyspeed); -#endif + tty_init(&tty); if (tty_open(&tty) != RC_OK) { #ifdef LOG @@ -254,43 +246,30 @@ conn_write(int d, void *buf, size_t nbytes, int istty) fd_set fs; struct timeval ts, tts; long delay; + #ifdef TRXCTL - if (istty) { - if (cfg.trxcntl != TRX_ADDC ) - tty_set_rts(d); - usleep(35000000l/cfg.ttyspeed); + if (istty && cfg.trxcntl != TRX_ADDC) + { + tty_set_rts(d); + tty_delay(35000000l/cfg.ttyspeed); } #endif + FD_ZERO(&fs); FD_SET(d, &fs); do - { /* trying write to descriptor while breaked by signals */ - gettimeofday(&ts, NULL); + { rc = write(d, buf, nbytes); } while (rc == -1 && errno == EINTR); - gettimeofday(&ts, NULL); - #ifdef TRXCTL - if (istty) { -#if 1 - do { - gettimeofday(&tts, NULL); - delay = DV(nbytes, cfg.ttyspeed) - - ((tts.tv_sec * 1000000l + tts.tv_usec) - (ts.tv_sec * 1000000l + ts.tv_usec)); - } while (delay > 0); -#else - gettimeofday(&tts, NULL); - delay = DV(nbytes, cfg.ttyspeed) - - ((tts.tv_sec * 1000000l + tts.tv_usec) - (ts.tv_sec * 1000000l + ts.tv_usec)); - usleep(delay); -#endif -/* tcdrain(d); - hangs sometimes, so make calculated delay */ - if (cfg.trxcntl != TRX_ADDC ) { - tty_clr_rts(d); - } + if (istty && cfg.trxcntl != TRX_ADDC ) + { + tty_delay(DV(nbytes, tty.bpc, cfg.ttyspeed)); + tty_clr_rts(d); } #endif + return (rc < 0) ? RC_ERR : rc; } @@ -601,7 +580,7 @@ conn_loop(void) } if (tty.rxlen > TTY_BUFSIZE) tty.rxlen = TTY_BUFSIZE; - tty.timer += DV(tty.rxlen, tty.speed); + tty.timer += DV(tty.rxlen, tty.bpc, tty.speed); #ifdef DEBUG logw(5, "tty: estimated %d bytes, waiting %lu usec", tty.rxlen, tty.timer); #endif @@ -841,7 +820,7 @@ conn_loop(void) { /* ### frame received completely ### */ #ifdef DEBUG t[0] = '\0'; - for (i = MB_UNIT_ID; i < curconn->ctr; i++) { + for (int i = MB_UNIT_ID; i < curconn->ctr; i++) { sprintf(v, "[%2.2x]", curconn->buf[i]); strncat(t, v, 1024-strlen(t)); } diff --git a/src/state.c b/src/state.c index c1107d6..1de1b72 100644 --- a/src/state.c +++ b/src/state.c @@ -148,7 +148,7 @@ state_tty_set(ttydata_t *mod, int state) logw(5, "tty: state now is TTY_RQST"); #endif #ifndef NOSILENT - tty_delay(DV(2, cfg.ttyspeed)); + tty_delay(DV(2, mod->bpc, cfg.ttyspeed)); #endif break; case TTY_RESP: @@ -156,7 +156,7 @@ state_tty_set(ttydata_t *mod, int state) mod->rxoffset = 0; /* XXX need real recv length? */ mod->rxlen = TTY_BUFSIZE; - mod->timer = cfg.respwait * 1000l + DV(mod->txlen, mod->speed); + mod->timer = cfg.respwait * 1000l + DV(mod->txlen, mod->bpc, mod->speed); #ifdef DEBUG logw(5, "tty: state now is TTY_RESP"); #endif diff --git a/src/tty.c b/src/tty.c index b7893a7..cd2a869 100644 --- a/src/tty.c +++ b/src/tty.c @@ -48,20 +48,25 @@ tty_sighup(void) } /* - * Init serial link parameters MOD to PORT name, SPEED and TRXCNTL type + * Init serial link parameters */ void -#ifdef TRXCTL -tty_init(ttydata_t *mod, char *port, int speed, int trxcntl) -#else -tty_init(ttydata_t *mod, char *port, int speed) -#endif +tty_init(ttydata_t *mod) { mod->fd = -1; - mod->port = port; - mod->speed = speed; -#ifdef TRXCTL - mod->trxcntl = trxcntl; + mod->port = cfg.ttyport; + mod->speed = cfg.ttyspeed; + mod->bpc = DEFAULT_BITS_PER_CHAR; + if (toupper(cfg.ttymode[1]) != 'N') + { + mod->bpc++; + } + if (cfg.ttymode[2] == '2') + { + mod->bpc++; + } +#ifdef TRXCTL + mod->trxcntl = cfg.trxcntl; #endif } @@ -412,7 +417,7 @@ tty_delay(int usec) do { (void)gettimeofday(&ttv, NULL); - ts = 1000000 * (ttv.tv_sec - tv.tv_sec) + (ttv.tv_usec - tv.tv_usec); + ts = 1000000l * (ttv.tv_sec - tv.tv_sec) + (ttv.tv_usec - tv.tv_usec); } while (ts < usec && !tty_break); } diff --git a/src/tty.h b/src/tty.h index d1a7868..e49965f 100644 --- a/src/tty.h +++ b/src/tty.h @@ -38,9 +38,12 @@ #include "cfg.h" /* - * Delay value calculation macros + * Delay value calculation macros. + * c - number of characters + * b - bits per character + * s - bits per second */ -#define DV(x, y) (x * 10000000l / y) +#define DV(c, b, s) (c * b * 1000000l / s) /* * Default tty port parameters @@ -58,6 +61,8 @@ #define DEFAULT_MODE "8N1" +#define DEFAULT_BITS_PER_CHAR 10 + /* * Maximum tty buffer size */ @@ -90,6 +95,7 @@ typedef struct int fd; /* tty file descriptor */ int speed; /* serial port speed */ char *port; /* serial port device name */ + int bpc; /* bits per character */ #ifdef TRXCTL int trxcntl; /* trx control type (enum - see values in config.h) */ #endif @@ -108,11 +114,7 @@ typedef struct /* prototypes */ void tty_sighup(void); -#ifdef TRXCTL -void tty_init(ttydata_t *mod, char *port, int speed, int trxcntl); -#else -void tty_init(ttydata_t *mod, char *port, int speed); -#endif +void tty_init(ttydata_t *mod); int tty_open(ttydata_t *mod); int tty_set_attr(ttydata_t *mod); speed_t tty_transpeed(int speed);