msousa@0: /*
msousa@0: * Copyright (c) 2002,2016 Mario de Sousa (msousa@fe.up.pt)
msousa@0: *
msousa@0: * This file is part of the Modbus library for Beremiz and matiec.
msousa@0: *
msousa@0: * This Modbus library is free software: you can redistribute it and/or modify
msousa@0: * it under the terms of the GNU Lesser General Public License as published by
msousa@0: * the Free Software Foundation, either version 3 of the License, or
msousa@0: * (at your option) any later version.
msousa@0: *
msousa@0: * This program is distributed in the hope that it will be useful, but
msousa@0: * WITHOUT ANY WARRANTY; without even the implied warranty of
msousa@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
msousa@0: * General Public License for more details.
msousa@0: *
msousa@0: * You should have received a copy of the GNU Lesser General Public License
msousa@0: * along with this Modbus library. If not, see .
msousa@0: *
msousa@0: * This code is made available on the understanding that it will not be
msousa@0: * used in safety-critical situations without a full and competent review.
msousa@0: */
msousa@0:
msousa@0:
msousa@0:
msousa@0: #include /* File control definitions */
msousa@0: #include /* Standard input/output */
msousa@0: #include
msousa@0: #include
msousa@0: #include /* POSIX terminal control definitions */
msousa@0: #include /* Time structures for select() */
msousa@0: #include /* POSIX Symbolic Constants */
msousa@0: #include
msousa@0: #include /* Error definitions */
msousa@0: #include
msousa@0: #include /* clock_gettime() */
msousa@0: #include /* required for INT_MAX */
msousa@0:
msousa@0: #include "mb_layer1.h"
msousa@0: #include "mb_ascii_private.h"
msousa@0:
msousa@0:
msousa@0: /* #define DEBUG */ /* uncomment to see the data sent and received */
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /************************************/
msousa@0: /** **/
msousa@0: /** Include common code... **/
msousa@0: /** **/
msousa@0: /************************************/
msousa@0:
msousa@0: #include "mb_ds_util.h" /* data structures... */
msousa@0: #include "mb_time_util.h" /* time conversion routines... */
msousa@0:
msousa@0:
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**** Purpose and Formats ****/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0: /*
msousa@0:
msousa@0: This file implements the ascii formating of the modbus protocol.
msousa@0: Many values, protocol related, are hardcoded into the code, as it
msousa@0: seems very unlikely this code will ever get re-used for anything
msousa@0: else but this specific protocol.
msousa@0:
msousa@0: Modbus ASCII frames have no timing restrictions whatsoever, and
msousa@0: abide by the following format:
msousa@0:
msousa@0: Header
msousa@0: ------
msousa@0: size : 1 byte
msousa@0: value: ':' (i.e. '\0x3A')
msousa@0:
msousa@0: Body
msousa@0: ----
msousa@0: size : variable, multiple of 2
msousa@0: value: binary data converted to ascii format,
msousa@0: i.e. each binary data byte is converted into two ascii characters
msousa@0: representing the byte in hexadecimal. Allowable characters are
msousa@0: '0' to '9' and 'A' to 'D'
msousa@0:
msousa@0: LRC
msousa@0: ---
msousa@0: size : 2 bytes
msousa@0: value: Longitudinal Redundancy Check of data, excluding any headers, tails,
msousa@0: etc...
msousa@0:
msousa@0: Tail
msousa@0: ----
msousa@0: size : 2 bytes
msousa@0: value: 'CR' + 'LF' (i.e. '\0x0D' + '\0x0A')
msousa@0:
msousa@0:
msousa@0: */
msousa@0:
msousa@0:
msousa@0:
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**** Forward Declarations ****/
msousa@0: /**** and Defaults ****/
msousa@0: /**** ****/
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0:
msousa@0:
msousa@0: typedef enum {fp_header, fp_body, fp_lrc, fp_tail, fp_done} frame_part_t;
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**** Local Utility functions... ****/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0:
msousa@0:
msousa@0: /*****************************************/
msousa@0: /** **/
msousa@0: /** lrc functions **/
msousa@0: /** **/
msousa@0: /*****************************************/
msousa@0:
msousa@0:
msousa@0: static inline void lrc_init(u8 *lrc) {
msousa@0: *lrc = 0;
msousa@0: }
msousa@0:
msousa@0: static inline void lrc_add_single(u8 *lrc, u8 data) {
msousa@0: *lrc += data;
msousa@0: }
msousa@0:
msousa@0: static inline void lrc_add_many(u8 *lrc, u8 *data, int count) {
msousa@0: for (; count > 0; count--, *lrc += *data++);
msousa@0: }
msousa@0:
msousa@0: static inline void lrc_end(u8 *lrc) {
msousa@0: *lrc = 1 + ~(*lrc);
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: /**************************************/
msousa@0: /** **/
msousa@0: /** Initialise a struct termios **/
msousa@0: /** **/
msousa@0: /**************************************/
msousa@0: static int termios_init(struct termios *tios,
msousa@0: int baud,
msousa@0: int parity,
msousa@0: int data_bits,
msousa@0: int stop_bits) {
msousa@0: speed_t baud_rate;
msousa@0:
msousa@0: if (tios == NULL)
msousa@0: return -1;
msousa@0:
msousa@0: /* reset all the values... */
msousa@0: /* NOTE: the following are initialised later on...
msousa@0: tios->c_iflag = 0;
msousa@0: tios->c_oflag = 0;
msousa@0: tios->c_cflag = 0;
msousa@0: tios->c_lflag = 0;
msousa@0: */
msousa@0: tios->c_line = 0;
msousa@0:
msousa@0: /* The minimum number of characters that should be received
msousa@0: * to satisfy a call to read().
msousa@0: */
msousa@0: tios->c_cc[VMIN ] = 0;
msousa@0:
msousa@0: /* The maximum inter-arrival interval between two characters,
msousa@0: * in deciseconds.
msousa@0: *
msousa@0: * NOTE: we could use this to detect the end of RTU frames,
msousa@0: * but we prefer to use select() that has higher resolution,
msousa@0: * even though this higher resolution is most probably not
msousa@0: * supported, and the effective resolution is 10ms,
msousa@0: * one tenth of a decisecond.
msousa@0: */
msousa@0: tios->c_cc[VTIME] = 0;
msousa@0:
msousa@0: /* configure the input modes... */
msousa@0: tios->c_iflag = IGNBRK | /* ignore BREAK condition on input */
msousa@0: IGNPAR | /* ignore framing errors and parity errors */
msousa@0: IXANY; /* enable any character to restart output */
msousa@0: /* BRKINT Only active if IGNBRK is not set.
msousa@0: * generate SIGINT on BREAK condition,
msousa@0: * otherwise read BREAK as character \0.
msousa@0: * PARMRK Only active if IGNPAR is not set.
msousa@0: * replace bytes with parity errors with
msousa@0: * \377 \0, instead of \0.
msousa@0: * INPCK enable input parity checking
msousa@0: * ISTRIP strip off eighth bit
msousa@0: * IGNCR ignore carriage return on input
msousa@0: * INLCR only active if IGNCR is not set.
msousa@0: * translate newline to carriage return on input
msousa@0: * ICRNL only active if IGNCR is not set.
msousa@0: * translate carriage return to newline on input
msousa@0: * IUCLC map uppercase characters to lowercase on input
msousa@0: * IXON enable XON/XOFF flow control on output
msousa@0: * IXOFF enable XON/XOFF flow control on input
msousa@0: * IMAXBEL ring bell when input queue is full
msousa@0: */
msousa@0:
msousa@0: /* configure the output modes... */
msousa@0: tios->c_oflag = OPOST; /* enable implementation-defined output processing */
msousa@0: /* ONOCR don't output CR at column 0
msousa@0: * OLCUC map lowercase characters to uppercase on output
msousa@0: * ONLCR map NL to CR-NL on output
msousa@0: * OCRNL map CR to NL on output
msousa@0: * OFILL send fill characters for a delay, rather than
msousa@0: * using a timed delay
msousa@0: * OFDEL fill character is ASCII DEL. If unset, fill
msousa@0: * character is ASCII NUL
msousa@0: * ONLRET don't output CR
msousa@0: * NLDLY NL delay mask. Values are NL0 and NL1.
msousa@0: * CRDLY CR delay mask. Values are CR0, CR1, CR2, or CR3.
msousa@0: * TABDLY horizontal tab delay mask. Values are TAB0, TAB1,
msousa@0: * TAB2, TAB3, or XTABS. A value of XTABS expands
msousa@0: * tabs to spaces (with tab stops every eight columns).
msousa@0: * BSDLY backspace delay mask. Values are BS0 or BS1.
msousa@0: * VTDLY vertical tab delay mask. Values are VT0 or VT1.
msousa@0: * FFDLY form feed delay mask. Values are FF0 or FF1.
msousa@0: */
msousa@0:
msousa@0: /* configure the control modes... */
msousa@0: tios->c_cflag = CREAD | /* enable receiver. */
msousa@0: CLOCAL; /* ignore modem control lines */
msousa@0: /* HUPCL lower modem control lines after last process
msousa@0: * closes the device (hang up).
msousa@0: * CRTSCTS flow control (Request/Clear To Send).
msousa@0: */
msousa@0: if (data_bits == 5) tios->c_cflag |= CS5;
msousa@0: else if (data_bits == 6) tios->c_cflag |= CS6;
msousa@0: else if (data_bits == 7) tios->c_cflag |= CS7;
msousa@0: else if (data_bits == 8) tios->c_cflag |= CS8;
msousa@0: else return -1;
msousa@0:
msousa@0: if (stop_bits == 1) tios->c_cflag &=~ CSTOPB;
msousa@0: else if (stop_bits == 2) tios->c_cflag |= CSTOPB;
msousa@0: else return -1;
msousa@0:
msousa@0: if(parity == 0) { /* none */
msousa@0: tios->c_cflag &=~ PARENB;
msousa@0: tios->c_cflag &=~ PARODD;
msousa@0: } else if(parity == 2) { /* even */
msousa@0: tios->c_cflag |= PARENB;
msousa@0: tios->c_cflag &=~ PARODD;
msousa@0: } else if(parity == 1) { /* odd */
msousa@0: tios->c_cflag |= PARENB;
msousa@0: tios->c_cflag |= PARODD;
msousa@0: } else return -1;
msousa@0:
msousa@0:
msousa@0: /* configure the local modes... */
msousa@0: tios->c_lflag = IEXTEN; /* enable implementation-defined input processing */
msousa@0: /* ISIG when any of the characters INTR, QUIT, SUSP, or DSUSP
msousa@0: * are received, generate the corresponding signal.
msousa@0: * ICANON enable canonical mode. This enables the special
msousa@0: * characters EOF, EOL, EOL2, ERASE, KILL, REPRINT,
msousa@0: * STATUS, and WERASE, and buffers by lines.
msousa@0: * ECHO echo input characters.
msousa@0: */
msousa@0:
msousa@0: /* Set the baud rate */
msousa@0: /* Must be done before reseting all the values to 0! */
msousa@0: switch(baud) {
msousa@0: case 110: baud_rate = B110; break;
msousa@0: case 300: baud_rate = B300; break;
msousa@0: case 600: baud_rate = B600; break;
msousa@0: case 1200: baud_rate = B1200; break;
msousa@0: case 2400: baud_rate = B2400; break;
msousa@0: case 4800: baud_rate = B4800; break;
msousa@0: case 9600: baud_rate = B9600; break;
msousa@0: case 19200: baud_rate = B19200; break;
msousa@0: case 38400: baud_rate = B38400; break;
msousa@0: case 57600: baud_rate = B57600; break;
msousa@0: case 115200: baud_rate = B115200; break;
msousa@0: default: return -1;
msousa@0: } /* switch() */
msousa@0:
msousa@0: if ((cfsetispeed(tios, baud_rate) < 0) ||
msousa@0: (cfsetospeed(tios, baud_rate) < 0))
msousa@0: return -1;;
msousa@0:
msousa@0: return 0;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /*****************************************/
msousa@0: /** **/
msousa@0: /** u8/ascii conversion functions **/
msousa@0: /** **/
msousa@0: /*****************************************/
msousa@0:
msousa@0: /* Convert binary data to ascii format.
msousa@0: * Both xxx_data_lengths specify the available bytes in
msousa@0: * in the respective arrays.
msousa@0: */
msousa@0: /* NOTE: this function is only called from a single location
msousa@0: * so we might just as well make it inline...
msousa@0: */
msousa@0: static inline int bin_2_asc(u8 *bin_data, int bin_data_length,
msousa@0: u8 *asc_data, int asc_data_length) {
msousa@0: u8 nibble;
msousa@0: int count = 0;
msousa@0: u8 *last_bin_data = bin_data + bin_data_length;
msousa@0: /* we need L2_TO_ASC_CODING ascii bytes for each bin byte, therefore the
msousa@0: * '- (L2_TO_ASC_CODING - 1)'
msousa@0: */
msousa@0: u8 *last_asc_data = asc_data + asc_data_length - (L2_TO_ASC_CODING - 1);
msousa@0:
msousa@0: while ((bin_data < last_bin_data) &&
msousa@0: (asc_data < last_asc_data)) {
msousa@0:
msousa@0: nibble = (*bin_data & 0xF0) >> 4;
msousa@0: *(asc_data++) = (nibble <= 9)?nibble + '0':nibble - 10 + 'A';
msousa@0:
msousa@0: nibble = (*bin_data & 0x0F);
msousa@0: *(asc_data++) = (nibble <= 9)?nibble + '0':nibble - 10 + 'A';
msousa@0:
msousa@0: count++;
msousa@0: bin_data++;
msousa@0: }
msousa@0:
msousa@0: /* return number of bytes converted... */
msousa@0: return count;
msousa@0: }
msousa@0:
msousa@0:
msousa@0: /* Convert from lowercase to upper case. */
msousa@0: /* It probably does not make sense calling the generic toupper()
msousa@0: * whose functionality depends on the current locale.
msousa@0: * Our own specific function is most probably much faster...
msousa@0: */
msousa@0: static inline u8 local_toupper(u8 val) {
msousa@0: if ((val >= 'a') && (val <= 'z'))
msousa@0: return val - 'a' + 'A';
msousa@0: return val;
msousa@0: }
msousa@0:
msousa@0: /* Convert ascii data to bin format.
msousa@0: * *asc_data must be a two byte array.
msousa@0: *
msousa@0: * If a non-ascii character is found, returns -1
msousa@0: */
msousa@0: /* NOTE: this function is only called from a single location
msousa@0: * so we might just as well make it inline...
msousa@0: */
msousa@0: static inline int asc_2_bin(u8 *asc_data, u8 *bin_data) {
msousa@0: if ((isxdigit(asc_data[0]) == 0) ||
msousa@0: (isxdigit(asc_data[1]) == 0))
msousa@0: return -1;
msousa@0:
msousa@0: asc_data[0] = local_toupper(asc_data[0]);
msousa@0: asc_data[1] = local_toupper(asc_data[1]);
msousa@0:
msousa@0: /* hi */ *(bin_data) = ((asc_data[0] <= '9')?
msousa@0: (asc_data[0] - '0'):(asc_data[0] - 'A' + 10)) * 0x10;
msousa@0: /* lo */ *(bin_data) += (asc_data[1] <= '9')?
msousa@0: (asc_data[1] - '0'):(asc_data[1] - 'A' + 10);
msousa@0:
msousa@0: return 0;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /************************************/
msousa@0: /** **/
msousa@0: /** A data structure - send buffer **/
msousa@0: /** **/
msousa@0: /************************************/
msousa@0:
msousa@0: /* data structure used to store the data to be sent in ascii format. */
msousa@0: /* The binary data is converted into ascii format before transmission. The
msousa@0: * frame is not converted as a single whole, but is rather done in chunks.
msousa@0: * The size of the chunks depends on the data size of the send_buffer.
msousa@0: *
msousa@0: * A lrc variable keeps a tab on the current value of the lrc as the data
msousa@0: * is being converted.
msousa@0: *
msousa@0: * Three special functions add the header, lrc and tail to the ascii frame.
msousa@0: */
msousa@0:
msousa@0: /* NOTE: The algorithms in the insert functions require a minimum buffer
msousa@0: * size to work correctly...
msousa@0: */
msousa@0: #define SEND_BUF_MIN_LENGTH ASC_FRAME_MIN_ELE_LENGTH
msousa@0:
msousa@0: typedef struct {
msousa@0: lb_buf_t data_buf;
msousa@0:
msousa@0: u8 lrc; /* the current value of the lrc, in binary format */
msousa@0: } send_buf_t;
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline u8 *send_buf_init(send_buf_t *buf, int size, int max_data_start) {
msousa@0: /* The algorithms in other functions require a minimum size
msousa@0: * to work correctly...
msousa@0: */
msousa@0: if (size < SEND_BUF_MIN_LENGTH)
msousa@0: return NULL;
msousa@0:
msousa@0: lrc_init(&buf->lrc);
msousa@0: return lb_init(&buf->data_buf, size, max_data_start);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline void send_buf_done(send_buf_t *buf) {
msousa@0: lb_done(&buf->data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline void send_buf_reset(send_buf_t *buf) {
msousa@0: lrc_init(&buf->lrc);
msousa@0: lb_data_purge_all(&buf->data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline int send_buf_data_count(send_buf_t *buf) {
msousa@0: return lb_data_count(&buf->data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline int send_buf_free_count(send_buf_t *buf) {
msousa@0: return lb_free_count(&buf->data_buf);
msousa@0: }
msousa@0: /* A small auxiliary function... */
msousa@0: static inline u8 *send_buf_data(send_buf_t *buf) {
msousa@0: return lb_data(&buf->data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline u8 *send_buf_free(send_buf_t *buf) {
msousa@0: return lb_free(&buf->data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline int send_buf_data_add(send_buf_t *buf, u8 *data, int data_count) {
msousa@0: int res = bin_2_asc(data, data_count, send_buf_free(buf), send_buf_free_count(buf));
msousa@0: if (res <=0) return res;
msousa@0: lb_data_add(&buf->data_buf, L2_TO_ASC_CODING * res);
msousa@0: lrc_add_many(&buf->lrc, data, res);
msousa@0: return res;
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline void send_buf_data_purge(send_buf_t *buf, int count) {
msousa@0: lb_data_purge(&buf->data_buf, count);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline void send_buf_data_purge_all(send_buf_t *buf) {
msousa@0: lb_data_purge_all(&buf->data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline int send_buf_lrc_append(send_buf_t *buf) {
msousa@0: #if ASC_FRAME_LRC_LENGTH != 2
msousa@0: #error Code assumes LRC length of 2 bytes, but ASC_FRAME_LRC_LENGTH != 2
msousa@0: #endif
msousa@0: if (lb_free_count(&buf->data_buf) < ASC_FRAME_LRC_LENGTH)
msousa@0: return -1;
msousa@0: lrc_end(&buf->lrc);
msousa@0: bin_2_asc(&buf->lrc, sizeof(buf->lrc),
msousa@0: lb_free(&buf->data_buf), ASC_FRAME_LRC_LENGTH);
msousa@0: lb_data_add(&buf->data_buf, ASC_FRAME_LRC_LENGTH);
msousa@0: return 0;
msousa@0: }
msousa@0:
msousa@0: static inline int send_buf_header_append(send_buf_t *buf) {
msousa@0: #if ASC_FRAME_HEADER_LENGTH != 1
msousa@0: #error Code assumes HEADER length of 1 bytes, but ASC_FRAME_HEADER_LENGTH != 1
msousa@0: #endif
msousa@0: if (lb_free_count(&buf->data_buf) < ASC_FRAME_HEADER_LENGTH)
msousa@0: return -1;
msousa@0:
msousa@0: /* add the ':' frame header */
msousa@0: *lb_free(&buf->data_buf) = ASC_FRAME_HEADER;
msousa@0: lb_data_add(&buf->data_buf, ASC_FRAME_HEADER_LENGTH);
msousa@0:
msousa@0: return 0;
msousa@0: }
msousa@0:
msousa@0: static inline int send_buf_tail_append(send_buf_t *buf) {
msousa@0: #if ASC_FRAME_TAIL_LENGTH != 2
msousa@0: #error Code assumes TAIL length of 2 bytes, but ASC_FRAME_TAIL_LENGTH != 2
msousa@0: #endif
msousa@0: if (lb_free_count(&buf->data_buf) < ASC_FRAME_TAIL_LENGTH)
msousa@0: return -1;
msousa@0:
msousa@0: /* add the CR+LF frame delimiter */
msousa@0: lb_free(&buf->data_buf)[0] = ASC_FRAME_TAIL_0;
msousa@0: lb_free(&buf->data_buf)[1] = ASC_FRAME_TAIL_1;
msousa@0: lb_data_add(&buf->data_buf, ASC_FRAME_TAIL_LENGTH);
msousa@0:
msousa@0: return 0;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /************************************/
msousa@0: /** **/
msousa@0: /** A data structure - recv buffer **/
msousa@0: /** **/
msousa@0: /************************************/
msousa@0:
msousa@0: /* data structure used to store the data received from the bus. */
msousa@0:
msousa@0: /* The ascii data received from the bus is added to the buffer, and is
msousa@0: * dynamically converted to binary format. Once a valid frame has been
msousa@0: * converted, conversion stops until this valid frame is deleted/purged.
msousa@0: */
msousa@0:
msousa@0: /* NOTE: The algorithms in the insert functions require a minimum buffer
msousa@0: * size to work correctly...
msousa@0: */
msousa@0: #define RECV_BUF_MIN_LENGTH ASC_FRAME_MIN_ELE_LENGTH
msousa@0:
msousa@0: #define RECV_BUF_BIN_BUF_SIZE (MAX_L2_FRAME_LENGTH + \
msousa@0: ASC_FRAME_LRC_LENGTH / L2_TO_ASC_CODING)
msousa@0:
msousa@0: typedef struct {
msousa@0: lb_buf_t asc_data_buf;
msousa@0: u8 bin_data_buf[RECV_BUF_BIN_BUF_SIZE];
msousa@0: int bin_data_free_ofs;
msousa@0:
msousa@0: frame_part_t frame_part;
msousa@0: u8 lrc; /* the current value of the lrc, in binary format */
msousa@0: u8 lrc_1; /* the previous value of the lrc... */
msousa@0: /* NOTE: We do a running conversion between ascii and binary format,
msousa@0: * i.e. we start converting from ascii to binary before we
msousa@0: * have received the complete ascii frame. This means that
msousa@0: * we also do a running computation of our local version of
msousa@0: * the frame lrc.
msousa@0: * The lrc, transmitted at the end of the ascii frame,
msousa@0: * but before the frame tail, also gets converted to binary
msousa@0: * before we get a chance to realize that it is the lrc value,
msousa@0: * and should therefore not be taken into account when computing
msousa@0: * our local version of the lrc.
msousa@0: * So we keep the previous value of the running lrc, and use
msousa@0: * that to confirm whether we have a valid frame.
msousa@0: */
msousa@0: } recv_buf_t;
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline u8 *recv_buf_init(recv_buf_t *buf, int size, int max_data_start) {
msousa@0: /* The algorithms in other functions require a minimum size
msousa@0: * to work correctly...
msousa@0: */
msousa@0: if (size < RECV_BUF_MIN_LENGTH)
msousa@0: return NULL;
msousa@0:
msousa@0: lrc_init(&buf->lrc);
msousa@0: buf->bin_data_free_ofs = 0;
msousa@0: buf->frame_part = fp_header;
msousa@0: return lb_init(&buf->asc_data_buf, size, max_data_start);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline void recv_buf_done(recv_buf_t *buf) {
msousa@0: lb_done(&buf->asc_data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline void recv_buf_reset(recv_buf_t *buf) {
msousa@0: lrc_init(&buf->lrc);
msousa@0: buf->bin_data_free_ofs = 0;
msousa@0: buf->frame_part = fp_header;
msousa@0: lb_data_purge_all(&buf->asc_data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline u8 *recv_buf_data(recv_buf_t *buf) {
msousa@0: return lb_data(&buf->asc_data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline u8 *recv_buf_free(recv_buf_t *buf) {
msousa@0: return lb_free(&buf->asc_data_buf);
msousa@0: }
msousa@0:
msousa@0: /* The function that really does all the conversion work... */
msousa@0: /* It finds frame limits, converts the data into binary format,
msousa@0: * and checks for correct lrc in the received frame.
msousa@0: */
msousa@0: /* NOTE: called indirectly from various locations! Do NOT inline! */
msousa@0: static void recv_buf_data_parse(recv_buf_t *buf) {
msousa@0: int count;
msousa@0: u8 *data;
msousa@0:
msousa@0: data = lb_data(&buf->asc_data_buf);
msousa@0: count = lb_data_count(&buf->asc_data_buf);
msousa@0:
msousa@0: /* NOTE: We need at least ASC_FRAME_MIN_ELE_LENGTH bytes to
msousa@0: * to be able to find that element of minimum length
msousa@0: */
msousa@0: while ((count >= ASC_FRAME_MIN_ELE_LENGTH) && (buf->frame_part != fp_done)) {
msousa@0: /* Check for frame header... */
msousa@0: /* The following few lines of code assume that ASC_FRAME_HEADER_LENGTH is 1! */
msousa@0: #if ASC_FRAME_HEADER_LENGTH != 1
msousa@0: #error The code is written in such a way that can only handle ASC_FRAME_HEADER_LENGTH == 1
msousa@0: #endif
msousa@0: if (data[0] == ASC_FRAME_HEADER) {
msousa@0: /* found the beginning of a frame...
msousa@0: * Even if we were previously converting a frame without errors,
msousa@0: * if we receive a new frame header we discard the previous
msousa@0: * frame that went unfinished!
msousa@0: */
msousa@0: data += ASC_FRAME_HEADER_LENGTH;
msousa@0: count -= ASC_FRAME_HEADER_LENGTH;
msousa@0: buf->frame_part = fp_body;
msousa@0: lrc_init(&buf->lrc);
msousa@0: buf->bin_data_free_ofs = 0;
msousa@0: continue;
msousa@0: }
msousa@0:
msousa@0: /* Check for frame tail... */
msousa@0: /*
msousa@0: * Note that the while() condition guarantees that we have at least
msousa@0: * two ascii bytes to handle.
msousa@0: */
msousa@0: /* The following few lines of code assume that ASC_FRAME_TAIL_LENGTH is 2! */
msousa@0: #if ASC_FRAME_TAIL_LENGTH != 2
msousa@0: #error The code is written in such a way that can only handle ASC_FRAME_TAIL_LENGTH == 2
msousa@0: #endif
msousa@0: if ((data[0] == ASC_FRAME_TAIL_0) &&
msousa@0: (data[1] == ASC_FRAME_TAIL_1)) {
msousa@0: /* end of binary data... */
msousa@0: data += ASC_FRAME_TAIL_LENGTH;
msousa@0: count -= ASC_FRAME_TAIL_LENGTH;
msousa@0:
msousa@0: /* let's check the lrc... */
msousa@0: if (buf->bin_data_free_ofs <= 0)
msousa@0: /* we have reached the end of a frame that did not include
msousa@0: * any binary data, not even the lrc value itself!
msousa@0: */
msousa@0: goto frame_error;
msousa@0:
msousa@0: /* Remember that we do not use the most recent lrc value, as this
msousa@0: * incorrectly includes the ascii bytes in the frame that code the
msousa@0: * frame's lrc. (pls read the note in the recv_but_t typedef)
msousa@0: */
msousa@0: /* The following few lines of code assume that
msousa@0: * (ASC_FRAME_LRC_LENGTH / L2_TO_ASC_CODING) is 1!
msousa@0: */
msousa@0: #if L2_TO_ASC_CODING != ASC_FRAME_LRC_LENGTH
msousa@0: #error The code is written in such a way that can only handle L2_TO_ASC_CODING == ASC_FRAME_LRC_LENGTH
msousa@0: #endif
msousa@0: lrc_end(&(buf->lrc_1));
msousa@0: if (buf->lrc_1 == buf->bin_data_buf[buf->bin_data_free_ofs-1]) {
msousa@0: /* we have received a correct frame... */
msousa@0: buf->frame_part = fp_done;
msousa@0: continue;
msousa@0: } else {
msousa@0: /* we have found a frame with an lrc error */
msousa@0: goto frame_error;
msousa@0: }
msousa@0: }
msousa@0:
msousa@0: if (buf->frame_part == fp_header) {
msousa@0: /* we are searching for beginning of a frame...
msousa@0: * but we did not find it in the previous condition!
msousa@0: * We continue looking in the next ascii byte
msousa@0: */
msousa@0: data++;
msousa@0: count--;
msousa@0: continue;
msousa@0: }
msousa@0:
msousa@0: if (buf->frame_part == fp_body) {
msousa@0: /* we have previously found the beginning of a frame,
msousa@0: * and are now converting the body into binary format...
msousa@0: *
msousa@0: * Note that the while() condition guarantees that we have at least
msousa@0: * two ascii bytes to convert into binary format.
msousa@0: */
msousa@0:
msousa@0: /* this is normal data... let's convert... */
msousa@0: if (asc_2_bin(data, buf->bin_data_buf + buf->bin_data_free_ofs) < 0)
msousa@0: /* error converting from ascii to binary.
msousa@0: * This must be due to an invalid ascii character in the ascii data.
msousa@0: * We discard the current frame...
msousa@0: *
msousa@0: * Note that we *do not* increment the data pointer,
msousa@0: * nor do we decrement the count variable. One of the ascii bytes could
msousa@0: * be the begining of a new valid frame, and we don't want to skip it!
msousa@0: */
msousa@0: goto frame_error;
msousa@0:
msousa@0: buf->lrc_1 = buf->lrc;
msousa@0: lrc_add_single(&(buf->lrc), *(buf->bin_data_buf + buf->bin_data_free_ofs));
msousa@0:
msousa@0: data += L2_TO_ASC_CODING;
msousa@0: count -= L2_TO_ASC_CODING;
msousa@0: if (++(buf->bin_data_free_ofs) >= RECV_BUF_BIN_BUF_SIZE)
msousa@0: /* Whoops, this shouldn't be hapening!
msousa@0: * The frame we are receiving is larger than the alocated buffer!
msousa@0: * Our only alternative is to discard the frame
msousa@0: * we are currently receiving...
msousa@0: */
msousa@0: goto frame_error;
msousa@0:
msousa@0: continue;
msousa@0: }
msousa@0:
msousa@0: /* if none of the above, then it must be some transmission error */
msousa@0: /* Actually, the code will never fall through since if we are in this loop,
msousa@0: * (frame_part == header) || (frame_part == body) is always true!
msousa@0: */
msousa@0: data++;
msousa@0: count--;
msousa@0: frame_error:
msousa@0: lrc_init(&buf->lrc);
msousa@0: buf->bin_data_free_ofs = 0;
msousa@0: buf->frame_part = fp_header;
msousa@0: } /* while () */
msousa@0:
msousa@0: lb_data_purge(&buf->asc_data_buf, lb_data_count(&buf->asc_data_buf) - count);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline void recv_buf_search_frame(recv_buf_t *buf) {
msousa@0: if (buf->frame_part != fp_done)
msousa@0: recv_buf_data_parse(buf);
msousa@0: }
msousa@0:
msousa@0: /* add ascii format data to buffer */
msousa@0: static inline void recv_buf_data_add(recv_buf_t *buf, int count) {
msousa@0: lb_data_add(&buf->asc_data_buf, count);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline int recv_buf_data_count(recv_buf_t *buf) {
msousa@0: return lb_data_count(&buf->asc_data_buf);
msousa@0: }
msousa@0:
msousa@0: /* A small auxiliary function... */
msousa@0: static inline int recv_buf_free_count(recv_buf_t *buf) {
msousa@0: return lb_free_count(&buf->asc_data_buf);
msousa@0: }
msousa@0:
msousa@0: /* Return pointer to frame, if a valid frame is available */
msousa@0: static inline u8 *recv_buf_frame(recv_buf_t *buf) {
msousa@0: recv_buf_search_frame(buf);
msousa@0: if (buf->frame_part == fp_done)
msousa@0: /* we have found a frame...! */
msousa@0: return buf->bin_data_buf;
msousa@0:
msousa@0: /* no frame... */
msousa@0: return NULL;
msousa@0: }
msousa@0:
msousa@0: /* Return number of bytes in frame, if a valid frame is available */
msousa@0: static inline int recv_buf_frame_count(recv_buf_t *buf) {
msousa@0: recv_buf_search_frame(buf);
msousa@0: if (buf->frame_part == fp_done)
msousa@0: /* we have found a frame...! */
msousa@0: return buf->bin_data_free_ofs - ASC_FRAME_LRC_LENGTH/L2_TO_ASC_CODING;
msousa@0:
msousa@0: /* no frame... */
msousa@0: return -1;
msousa@0: }
msousa@0:
msousa@0: /* Delete valid binary format frame! */
msousa@0: static inline void recv_buf_frame_purge(recv_buf_t *buf) {
msousa@0: /* NOTE: we only delete valid frames!!
msousa@0: * Partially converted frames are not deleted, the
msousa@0: * remaining bytes may be received later!
msousa@0: */
msousa@0: if (buf->frame_part == fp_done) {
msousa@0: buf->frame_part = fp_header;
msousa@0: buf->bin_data_free_ofs = 0;
msousa@0: }
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: /************************************/
msousa@0: /** **/
msousa@0: /** A data structure - nd entry **/
msousa@0: /** **/
msousa@0: /************************************/
msousa@0:
msousa@0: /* NOTE: nd = node descriptor */
msousa@0:
msousa@0: typedef struct {
msousa@0: /* The file descriptor associated with this node */
msousa@0: /* NOTE: if the node is not yet in use, i.e. if the node is free,
msousa@0: * then fd will be set to -1
msousa@0: */
msousa@0: int fd;
msousa@0: struct timeval time_15_char_;
msousa@0:
msousa@0: /* Modbus ascii frames are delimited by a ':' (colon) at the begining of
msousa@0: * a frame, and CR-LF sequence at the end the frame.
msousa@0: *
msousa@0: * Unless we want to take 'ages' reading the data off the serial line
msousa@0: * one byte at a time, we risk reading beyond the boundary of the
msousa@0: * frame currently being interpreted. The extra data belongs to the
msousa@0: * subsequent frame, and must therefore be buffered to be handled
msousa@0: * when the next frame is being interpreted.
msousa@0: *
msousa@0: * The receive buffer is therefore a static variable.
msousa@0: */
msousa@0: recv_buf_t recv_buf_;
msousa@0:
msousa@0: /* The send ascii buffer could be a local function variable
msousa@0: * instead of a static variable, but we might just as well
msousa@0: * allocate the memory at startup and not risk running out
msousa@0: * of memory while the module is running.
msousa@0: */
msousa@0: send_buf_t send_buf_;
msousa@0:
msousa@0: /* The old settings of the serial port, to be reset when the library is closed... */
msousa@0: struct termios old_tty_settings_;
msousa@0:
msousa@0: /* ignore echo flag.
msousa@0: * If set to 1, then it means that we will be reading every byte we
msousa@0: * ourselves write out to the bus, so we must ignore those bytes read
msousa@0: * before we really read the data sent by remote nodes.
msousa@0: *
msousa@0: * This comes in useful when using a RS232-RS485 converter that does
msousa@0: * not correctly control the RTS-CTS lines...
msousa@0: */
msousa@0: int ignore_echo;
msousa@0: } nd_entry_t;
msousa@0:
msousa@0:
msousa@0: static inline void nd_entry_init(nd_entry_t *nde) {
msousa@0: nde->fd = -1; /* The node is free... */
msousa@0: }
msousa@0:
msousa@0: static int nd_entry_connect(nd_entry_t *nde,
msousa@0: node_addr_t *node_addr,
msousa@0: optimization_t opt) {
msousa@0:
msousa@0: int parity_bits, start_bits, char_bits;
msousa@0: struct termios settings;
msousa@0: int buf_size;
msousa@0:
msousa@0: /*
msousa@0: if (nde == NULL)
msousa@0: goto error_exit_0;
msousa@0: */
msousa@0: if (nde->fd >= 0)
msousa@0: goto error_exit_0;
msousa@0:
msousa@0: /* initialise the termios data structure */
msousa@0: if (termios_init(&settings,
msousa@0: node_addr->addr.ascii.baud,
msousa@0: node_addr->addr.ascii.parity,
msousa@0: node_addr->addr.ascii.data_bits,
msousa@0: node_addr->addr.ascii.stop_bits)
msousa@0: < 0) {
msousa@0: #ifdef DEBUG
msousa@0: fprintf(stderr, "Invalid serial line settings"
msousa@0: "(baud=%d, parity=%d, data_bits=%d, stop_bits=%d)",
msousa@0: node_addr->addr.ascii.baud,
msousa@0: node_addr->addr.ascii.parity,
msousa@0: node_addr->addr.ascii.data_bits,
msousa@0: node_addr->addr.ascii.stop_bits);
msousa@0: #endif
msousa@0: goto error_exit_1;
msousa@0: }
msousa@0:
msousa@0: /* set the ignore_echo flag */
msousa@0: nde->ignore_echo = node_addr->addr.ascii.ignore_echo;
msousa@0:
msousa@0: /* initialise send buffer */
msousa@0: buf_size = (opt == optimize_size)?SEND_BUFFER_SIZE_SMALL:
msousa@0: SEND_BUFFER_SIZE_LARGE;
msousa@0: if (send_buf_init(&nde->send_buf_, buf_size, buf_size - SEND_BUF_MIN_LENGTH)
msousa@0: == NULL) {
msousa@0: #ifdef DEBUG
msousa@0: fprintf(stderr, "Out of memory: error initializing send buffer");
msousa@0: #endif
msousa@0: goto error_exit_1;
msousa@0: }
msousa@0:
msousa@0: /* initialise recv buffer */
msousa@0: buf_size = (opt == optimize_size)?RECV_BUFFER_SIZE_SMALL:
msousa@0: RECV_BUFFER_SIZE_LARGE;
msousa@0: if (recv_buf_init(&nde->recv_buf_, buf_size, buf_size - RECV_BUF_MIN_LENGTH)
msousa@0: == NULL) {
msousa@0: #ifdef DEBUG
msousa@0: fprintf(stderr, "Out of memory: error initializing receive buffer");
msousa@0: #endif
msousa@0: goto error_exit_2;
msousa@0: }
msousa@0:
msousa@0: /* open the serial port */
msousa@0: if((nde->fd = open(node_addr->addr.ascii.device, O_RDWR | O_NOCTTY | O_NDELAY))
msousa@0: < 0) {
msousa@0: #ifdef DEBUG
msousa@0: fprintf(stderr, "Error opening device %s (errno=%d)",
msousa@0: node_addr->addr.ascii.device, errno);
msousa@0: #endif
msousa@0: goto error_exit_3;
msousa@0: }
msousa@0:
msousa@0: if(tcgetattr(nde->fd, &nde->old_tty_settings_) < 0) {
msousa@0: #ifdef DEBUG
msousa@0: fprintf(stderr, "Error reading device's %s original settings.",
msousa@0: node_addr->addr.ascii.device);
msousa@0: #endif
msousa@0: goto error_exit_4;
msousa@0: }
msousa@0:
msousa@0: if(tcsetattr(nde->fd, TCSANOW, &settings) < 0) {
msousa@0: #ifdef DEBUG
msousa@0: fprintf(stderr, "Error configuring device %s"
msousa@0: "(baud=%d, parity=%d, data_bits=%d, stop_bits=%d)",
msousa@0: node_addr->addr.ascii.device,
msousa@0: node_addr->addr.ascii.baud,
msousa@0: node_addr->addr.ascii.parity,
msousa@0: node_addr->addr.ascii.data_bits,
msousa@0: node_addr->addr.ascii.stop_bits);
msousa@0: #endif
msousa@0: goto error_exit_4;
msousa@0: }
msousa@0:
msousa@0: parity_bits = (node_addr->addr.ascii.parity == 0)?0:1;
msousa@0: start_bits = 1;
msousa@0: char_bits = start_bits + node_addr->addr.ascii.data_bits +
msousa@0: parity_bits + node_addr->addr.ascii.stop_bits;
msousa@0: /* time_35_char_ = d_to_timeval(3.5*char_bits/baud); */
msousa@0: nde->time_15_char_ = d_to_timeval(1.5*char_bits/node_addr->addr.ascii.baud);
msousa@0:
msousa@0: #ifdef DEBUG
msousa@0: printf("nd_entry_connect(): %s open\n", node_addr->addr.ascii.device );
msousa@0: printf("nd_entry_connect(): returning fd=%d\n", nde->fd);
msousa@0: #endif
msousa@0: return nde->fd;
msousa@0:
msousa@0: error_exit_4:
msousa@0: close(nde->fd);
msousa@0: error_exit_3:
msousa@0: recv_buf_done(&nde->recv_buf_);
msousa@0: error_exit_2:
msousa@0: send_buf_done(&nde->send_buf_);
msousa@0: error_exit_1:
msousa@0: nde->fd = -1; /* set the node as free... */
msousa@0: error_exit_0:
msousa@0: return -1;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: static int nd_entry_free(nd_entry_t *nde) {
msousa@0: if (nde->fd < 0)
msousa@0: /* already free */
msousa@0: return -1;
msousa@0:
msousa@0: /* reset the tty device old settings... */
msousa@0: if(tcsetattr(nde->fd, TCSANOW, &nde->old_tty_settings_) < 0)
msousa@0: fprintf(stderr, "Error reconfiguring serial port to it's original settings.");
msousa@0:
msousa@0: recv_buf_done(&nde->recv_buf_);
msousa@0: send_buf_done(&nde->send_buf_);
msousa@0: close(nde->fd);
msousa@0: nde->fd = -1;
msousa@0:
msousa@0: return 0;
msousa@0: }
msousa@0:
msousa@0:
msousa@0: static inline int nd_entry_is_free(nd_entry_t *nde) {
msousa@0: return (nde->fd < 0);
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /************************************/
msousa@0: /** **/
msousa@0: /** A data structure - nd table **/
msousa@0: /** **/
msousa@0: /************************************/
msousa@0:
msousa@0: typedef struct {
msousa@0: /* the array of node descriptors, and current size... */
msousa@0: nd_entry_t *node;
msousa@0: int node_count; /* total number of nodes in the node[] array */
msousa@0: } nd_table_t;
msousa@0:
msousa@0:
msousa@0:
msousa@0: #if 1
msousa@0: /* nd_table_init()
msousa@0: * Version 1 of the nd_table_init() function.
msousa@0: * If called more than once, 2nd and any subsequent calls will
msousa@0: * be interpreted as a request to confirm that it was already correctly
msousa@0: * initialized with the requested number of nodes.
msousa@0: */
msousa@0: static int nd_table_init(nd_table_t *ndt, int nd_count) {
msousa@0: int count;
msousa@0:
msousa@0: if (ndt->node != NULL) {
msousa@0: /* this function has already been called, and the node table is already initialised */
msousa@0: return (ndt->node_count == nd_count)?0:-1;
msousa@0: }
msousa@0:
msousa@0: /* initialise the node descriptor metadata array... */
msousa@0: ndt->node = malloc(sizeof(nd_entry_t) * nd_count);
msousa@0: if (ndt->node == NULL) {
msousa@0: #ifdef DEBUG
msousa@0: fprintf(stderr, "Out of memory: error initializing node address buffer");
msousa@0: #endif
msousa@0: return -1;
msousa@0: }
msousa@0: ndt->node_count = nd_count;
msousa@0:
msousa@0: /* initialise the state of each node in the array... */
msousa@0: for (count = 0; count < ndt->node_count; count++) {
msousa@0: nd_entry_init(&ndt->node[count]);
msousa@0: } /* for() */
msousa@0:
msousa@0: return nd_count; /* number of succesfully created nodes! */
msousa@0: }
msousa@0: #else
msousa@0: /* nd_table_init()
msousa@0: * Version 2 of the nd_table_init() function.
msousa@0: * If called more than once, 2nd and any subsequent calls will
msousa@0: * be interpreted as a request to reserve an extra new_nd_count
msousa@0: * number of nodes. This will be done using realloc().
msousa@0: */
msousa@0: static int nd_table_init(nd_table_t *ndt, int new_nd_count) {
msousa@0: int count;
msousa@0:
msousa@0: /* initialise the node descriptor metadata array... */
msousa@0: ndt->node = realloc(ndt->node, sizeof(nd_entry_t) * (ndt->node_count + new_nd_count));
msousa@0: if (ndt->node == NULL) {
msousa@0: #ifdef ERRMSG
msousa@0: fprintf(stderr, ERRMSG_HEAD "Out of memory: error initializing node address buffer\n");
msousa@0: #endif
msousa@0: return -1;
msousa@0: }
msousa@0:
msousa@0: /* initialise the state of each newly added node in the array... */
msousa@0: for (count = ndt->node_count; count < ndt->node_count + new_nd_count; count++) {
msousa@0: nd_entry_init(&ndt->node[count]);
msousa@0: } /* for() */
msousa@0: ndt->node_count += new_nd_count;
msousa@0:
msousa@0: return new_nd_count; /* number of succesfully created nodes! */
msousa@0: }
msousa@0: #endif
msousa@0:
msousa@0:
msousa@0: static inline nd_entry_t *nd_table_get_nd(nd_table_t *ndt, int nd) {
msousa@0: if ((nd < 0) || (nd >= ndt->node_count))
msousa@0: return NULL;
msousa@0:
msousa@0: return &ndt->node[nd];
msousa@0: }
msousa@0:
msousa@0:
msousa@0: static inline void nd_table_done(nd_table_t *ndt) {
msousa@0: int i;
msousa@0:
msousa@0: if (ndt->node == NULL)
msousa@0: return;
msousa@0:
msousa@0: /* close all the connections... */
msousa@0: for (i = 0; i < ndt->node_count; i++)
msousa@0: nd_entry_free(&ndt->node[i]);
msousa@0:
msousa@0: /* Free memory... */
msousa@0: free(ndt->node);
msousa@0: *ndt = (nd_table_t){.node=NULL, .node_count=0};
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: static inline int nd_table_get_free_nd(nd_table_t *ndt) {
msousa@0: int count;
msousa@0:
msousa@0: for (count = 0; count < ndt->node_count; count++) {
msousa@0: if (nd_entry_is_free(&ndt->node[count]))
msousa@0: return count;
msousa@0: }
msousa@0:
msousa@0: /* none found... */
msousa@0: return -1;
msousa@0: }
msousa@0:
msousa@0:
msousa@0: static inline int nd_table_free_nd(nd_table_t *ndt, int nd) {
msousa@0: if ((nd < 0) || (nd >= ndt->node_count))
msousa@0: return -1;
msousa@0:
msousa@0: return nd_entry_free(&ndt->node[nd]);
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**** Global Library State ****/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0:
msousa@0:
msousa@0: /* The node descriptor table... */
msousa@0: /* NOTE: This variable must be correctly initialised here!! */
msousa@0: static nd_table_t nd_table_ = {.node=NULL, .node_count=0};
msousa@0:
msousa@0: /* The optimization choice... */
msousa@0: static optimization_t optimization_;
msousa@0:
msousa@0:
msousa@0:
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**** Sending of Modbus ASCII Frames ****/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0:
msousa@0: /*
msousa@0: * NOTE: for now the transmit_timeout is silently ignored in ASCII version!
msousa@0: */
msousa@0: int modbus_ascii_write(int nd,
msousa@0: u8 *data,
msousa@0: size_t data_length,
msousa@0: u16 transaction_id,
msousa@0: const struct timespec *transmit_timeout
msousa@0: )
msousa@0: {
msousa@0: fd_set rfds;
msousa@0: struct timeval timeout;
msousa@0: int res, bin_data_conv, send_retries;
msousa@0: frame_part_t frame_part;
msousa@0: nd_entry_t *nd_entry;
msousa@0:
msousa@0: /* check if nd is correct... */
msousa@0: if ((nd_entry = nd_table_get_nd(&nd_table_, nd)) == NULL)
msousa@0: return -1;
msousa@0:
msousa@0: /* check if nd is initialzed... */
msousa@0: if (nd_entry->fd < 0)
msousa@0: return -1;
msousa@0:
msousa@0: /* THE MAIN LOOP!!! */
msousa@0: send_retries = ASC_FRAME_SEND_RETRY + 1; /* must try at least once... */
msousa@0: while (send_retries > 0) {
msousa@0:
msousa@0: /*******************************
msousa@0: * synchronise with the bus... *
msousa@0: *******************************/
msousa@0: /* Remember that a RS485 bus is half-duplex, so we have to wait until
msousa@0: * nobody is transmitting over the bus for our turn to transmit.
msousa@0: * This will never happen on a modbus network if the master and
msousa@0: * slave state machines never get out of synch (granted, it probably
msousa@0: * only has two states, but a state machine nonetheless), but we want
msousa@0: * to make sure we can re-synchronise if they ever do get out of synch.
msousa@0: *
msousa@0: * The following lines are an attempt at re-synchronising with the bus.
msousa@0: * Unfortunately, due to the completely asynchronous nature of the
msousa@0: * modbus-ascii protocol (there are no time boundaries for sending a frame!),
msousa@0: * it is impossible to guarantee that we will synchronise correctly.
msousa@0: *
msousa@0: * Use of RTS/CTS over a half-duplex coms chanel would eliminate this
msousa@0: * 'feature', but not all RS232/RS485 converters delay activating the
msousa@0: * CTS signal, even though there may be current activity on the bus.
msousa@0: *
msousa@0: * We first wait until the bus is silent for at least 1.5 character interval.
msousa@0: * Note that we only get feedback from the device driver once a whole byte
msousa@0: * has been received, so we must wait longer than 1 character interval to make
msousa@0: * sure there is no current activity on the bus). We then flush the input and
msousa@0: * output queues.
msousa@0: */
msousa@0: /* NOTES:
msousa@0: * - we do not need to reset the rfds with FD_SET(ttyfd, &rfds)
msousa@0: * before every call to select! We only wait on one file descriptor,
msousa@0: * so if select returns succesfully, it must have that same file
msousa@0: * decriptor set in the rdfs!
msousa@0: * If select returns with a timeout, then we do not get to call
msousa@0: * select again!
msousa@0: * - We do not reset the timeout value. Normally this value is left
msousa@0: * unchanged when select() returns, so we will be witing for longer
msousa@0: * than the desired period.
msousa@0: * On Linux the timeout is changed to reflect the time remaining.
msousa@0: * In this case, things will work more nicely.
msousa@0: */
msousa@0: FD_ZERO(&rfds);
msousa@0: FD_SET(nd_entry->fd, &rfds);
msousa@0: timeout = nd_entry->time_15_char_;
msousa@0: while ((res = select(nd_entry->fd+1, &rfds, NULL, NULL, &timeout)) != 0) {
msousa@0: if (res > 0) {
msousa@0: /* we are receiving data over the serial port! */
msousa@0: /* Throw the data away! */
msousa@0: tcflush(nd_entry->fd, TCIFLUSH); /* flush the input stream */
msousa@0: /* reset the timeout value! */
msousa@0: timeout = nd_entry->time_15_char_;
msousa@0: } else {
msousa@0: /* some kind of error ocurred */
msousa@0: if (errno != EINTR)
msousa@0: /* we were not interrupted by a signal */
msousa@0: return -1;
msousa@0: /* We will be callind select() again.
msousa@0: * We need to reset the FD SET !
msousa@0: */
msousa@0: FD_ZERO(&rfds);
msousa@0: FD_SET(nd_entry->fd, &rfds);
msousa@0: }
msousa@0: } /* while (select()) */
msousa@0:
msousa@0: /* Flush both input and output streams... */
msousa@0: /* NOTE: Due to the nature of the modbus protocol,
msousa@0: * when a frame is sent all previous
msousa@0: * frames that may have arrived at the sending node become
msousa@0: * irrelevant.
msousa@0: */
msousa@0: tcflush(nd_entry->fd, TCIOFLUSH); /* flush the input & output streams */
msousa@0: recv_buf_reset(&nd_entry->recv_buf_); /* reset the recv buffer */
msousa@0:
msousa@0: /**********************
msousa@0: * write to output... *
msousa@0: **********************/
msousa@0: send_buf_reset(&nd_entry->send_buf_);
msousa@0:
msousa@0: frame_part = fp_header; /* start off by sending the header... */
msousa@0: bin_data_conv = 0; /* binary format data already converted to ascii format... */
msousa@0: while ((frame_part != fp_done) ||
msousa@0: (send_buf_data_count(&nd_entry->send_buf_) > 0)) {
msousa@0:
msousa@0: /* build the frame we will send over the wire... */
msousa@0: /* We use a state machine with four states... */
msousa@0: if (frame_part == fp_header) {
msousa@0: if (send_buf_header_append(&nd_entry->send_buf_) >= 0)
msousa@0: frame_part = fp_body;
msousa@0: }
msousa@0: if (frame_part == fp_body) {
msousa@0: res = send_buf_data_add(&nd_entry->send_buf_, data + bin_data_conv, data_length - bin_data_conv);
msousa@0: bin_data_conv += res;
msousa@0: if (bin_data_conv == data_length)
msousa@0: frame_part = fp_lrc;
msousa@0: }
msousa@0: if (frame_part == fp_lrc) {
msousa@0: if (send_buf_lrc_append(&nd_entry->send_buf_) >= 0)
msousa@0: frame_part = fp_tail;
msousa@0: }
msousa@0: if (frame_part == fp_tail) {
msousa@0: if (send_buf_tail_append(&nd_entry->send_buf_) >= 0)
msousa@0: frame_part = fp_done;
msousa@0: }
msousa@0:
msousa@0: /* send the frame... */
msousa@0: if ((res = write(nd_entry->fd,
msousa@0: send_buf_data(&nd_entry->send_buf_),
msousa@0: send_buf_data_count(&nd_entry->send_buf_))) < 0) {
msousa@0: if ((errno != EAGAIN ) && (errno != EINTR )) {
msousa@0: break;
msousa@0: } else {
msousa@0: /* there is no harm if we get interrupted while writing the frame.
msousa@0: * The ascii version of modbus does not place any timing
msousa@0: * constraints whatsoever...
msousa@0: *
msousa@0: * We simply continue sending the frame...
msousa@0: */
msousa@0: res = 0;
msousa@0: }
msousa@0: }
msousa@0: #ifdef DEBUG
msousa@0: /* Print each character that has just been sent on the bus */
msousa@0: { int i;
msousa@0: printf("bytes sent -> [");
msousa@0: for(i = 0; i < res; i++)
msousa@0: printf("%c", send_buf_data(&nd_entry->send_buf_)[i]);
msousa@0: printf("]\n");
msousa@0: }
msousa@0: #endif
msousa@0: send_buf_data_purge(&nd_entry->send_buf_, res);
msousa@0:
msousa@0: if ((frame_part == fp_done) &&
msousa@0: (send_buf_data_count(&nd_entry->send_buf_) == 0))
msousa@0: /* sent frame successfully! */
msousa@0: return data_length;
msousa@0:
msousa@0: } /* while(frame_not_sent) */
msousa@0: /* NOTE: Some error occured while trying to transmit. It will probably
msousa@0: * not go away by itself, but there is no harm in trying again
msousa@0: * if the upper layer so wishes...
msousa@0: */
msousa@0: send_retries--;
msousa@0: } /* while() MAIN LOOP */
msousa@0:
msousa@0: /* maximum retries exceeded */
msousa@0: return -1;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**** Receiving Modbus ASCII Frames ****/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0:
msousa@0:
msousa@0: /************************************/
msousa@0: /** **/
msousa@0: /** Read a frame **/
msousa@0: /** **/
msousa@0: /************************************/
msousa@0:
msousa@0: /* A function to read a valid frame off the modbus ascii bus.
msousa@0: *
msousa@0: * NOTES:
msousa@0: * - The returned frame is guaranteed to be a valid frame.
msousa@0: * - The returned length does *not* include the LRC.
msousa@0: */
msousa@0: /* NOTE: This function is only called from one place in the rest of the code,
msousa@0: * so we might just as well make it inline...
msousa@0: */
msousa@0: /* RETURNS: number of bytes in received frame
msousa@0: * -1 on read file error
msousa@0: * -2 on timeout
msousa@0: */
msousa@0: static inline int read_frame(nd_entry_t *nd_entry,
msousa@0: u8 **recv_data_ptr,
msousa@0: struct timespec *end_time)
msousa@0: {
msousa@0: /* temporary variables... */
msousa@0: fd_set rfds;
msousa@0: int res;
msousa@0:
msousa@0: /* start by deleting any previously converted frames... */
msousa@0: recv_buf_frame_purge(&nd_entry->recv_buf_);
msousa@0:
msousa@0: /*==============*
msousa@0: * read a frame *
msousa@0: *==============*/
msousa@0: #ifdef DEBUG
msousa@0: printf("bytes read -> <");
msousa@0: #endif
msousa@0: /* The main loop that reads one frame */
msousa@0: /* (multiple calls to read() ) */
msousa@0: /* and jumps out as soon as it finds a valid frame. */
msousa@0: /* NOTE: Do not change this into a do {...} until() loop!
msousa@0: * The while loop may find valid frames in the data read off the
msousa@0: * bus in previous invocations of this same fucntion, and may
msousa@0: * therefore not execute any loop iteration whatsoever,
msousa@0: * and not call read()
msousa@0: */
msousa@0: while ((*recv_data_ptr = recv_buf_frame(&nd_entry->recv_buf_)) == NULL) {
msousa@0: /* We need more bytes... */
msousa@0:
msousa@0: /*-----------------------------*
msousa@0: * check for data availability *
msousa@0: *-----------------------------*/
msousa@0: /* if we can't find a valid frame in the existing data, or no data
msousa@0: * was left over, then we need to read more bytes!
msousa@0: */
msousa@0: FD_ZERO(&rfds);
msousa@0: FD_SET(nd_entry->fd, &rfds);
msousa@0: {int sel_res = my_select(nd_entry->fd + 1, &rfds, NULL, end_time);
msousa@0: if (sel_res < 0)
msousa@0: return -1;
msousa@0: if (sel_res == 0)
msousa@0: return -2;
msousa@0: }
msousa@0:
msousa@0: /*------------------*
msousa@0: * read frame bytes *
msousa@0: *------------------*/
msousa@0: /* Read in as many bytes as possible... */
msousa@0: res = read(nd_entry->fd,
msousa@0: recv_buf_free(&nd_entry->recv_buf_),
msousa@0: recv_buf_free_count(&nd_entry->recv_buf_));
msousa@0: if (res < 0) {
msousa@0: if (errno != EINTR)
msousa@0: return -1;
msousa@0: else
msousa@0: res = 0;
msousa@0: }
msousa@0: #ifdef DEBUG
msousa@0: {/* display the hex code of each character received */
msousa@0: int i;
msousa@0: for (i=0; i < res; i++)
msousa@0: printf("%c", recv_buf_free(&nd_entry->recv_buf_)[i]);
msousa@0: }
msousa@0: #endif
msousa@0: recv_buf_data_add(&nd_entry->recv_buf_, res);
msousa@0: } /* while ()*/
msousa@0: #ifdef DEBUG
msousa@0: printf(">\n");
msousa@0: #endif
msousa@0:
msousa@0: /* We have a frame! */
msousa@0: return recv_buf_frame_count(&nd_entry->recv_buf_);
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /**************************************/
msousa@0: /** **/
msousa@0: /** Read a Modbus ASCII frame **/
msousa@0: /** **/
msousa@0: /**************************************/
msousa@0:
msousa@0: /* The public function that reads a valid modbus frame.
msousa@0: *
msousa@0: * The returned frame is guaranteed to be different to the
msousa@0: * the frame stored in send_data, and to start with the
msousa@0: * same slave address stored in send_data[0].
msousa@0: *
msousa@0: * If send_data is NULL, send_data_length = 0, or
msousa@0: * ignore_echo == 0, then the first valid frame read off
msousa@0: * the bus is returned.
msousa@0: *
msousa@0: * return value: The length (in bytes) of the valid frame,
msousa@0: * -1 on error
msousa@0: * -2 on timeout
msousa@0: */
msousa@0:
msousa@0: int modbus_ascii_read(int *nd,
msousa@0: u8 **recv_data_ptr,
msousa@0: u16 *transaction_id,
msousa@0: const u8 *send_data,
msousa@0: int send_length,
msousa@0: const struct timespec *recv_timeout) {
msousa@0:
msousa@0: struct timespec end_time, *ts_ptr;
msousa@0: int res, recv_length;
msousa@0: int iter; /* Warning: if you change the var type of iter from int,
msousa@0: * then you must also change the INT_MAX constant
msousa@0: * further on in this function...
msousa@0: */
msousa@0: u8 *local_recv_data_ptr;
msousa@0: nd_entry_t *nd_entry;
msousa@0:
msousa@0: /* Check input parameters... */
msousa@0: if (nd == NULL)
msousa@0: return -1;
msousa@0:
msousa@0: if (recv_data_ptr == NULL)
msousa@0: recv_data_ptr = &local_recv_data_ptr;
msousa@0:
msousa@0: if ((send_data == NULL) && (send_length != 0))
msousa@0: return -1;
msousa@0:
msousa@0: /* check if nd is correct... */
msousa@0: if ((nd_entry = nd_table_get_nd(&nd_table_, *nd)) == NULL)
msousa@0: return -1;
msousa@0:
msousa@0: /* check if nd is initialzed... */
msousa@0: if (nd_entry->fd < 0)
msousa@0: return -1;
msousa@0:
msousa@0: /* We will potentially read many frames, and we cannot reset the timeout
msousa@0: * for every frame we read. We therefore determine the absolute time_out,
msousa@0: * and use this as a parameter for each call to read_frame() instead of
msousa@0: * using a relative timeout.
msousa@0: *
msousa@0: * NOTE: see also the timeout related comment in the read_frame()= function!
msousa@0: */
msousa@0: ts_ptr = NULL;
msousa@0: if (recv_timeout != NULL) {
msousa@0: ts_ptr = &end_time;
msousa@0: *ts_ptr = timespec_add_curtime(*recv_timeout);
msousa@0: }
msousa@0:
msousa@0: /* NOTE: When using a half-duplex RS-485 bus, some (most ?) RS232-485
msousa@0: * converters will send back to the RS232 port whatever we write,
msousa@0: * so we will read in whatever we write out onto the bus.
msousa@0: * We will therefore have to compare
msousa@0: * the first frame we read with the one we sent. If they are
msousa@0: * identical it is because we are in fact working on a RS-485
msousa@0: * bus and must therefore read in a second frame which will be
msousa@0: * the true response to our query.
msousa@0: * If the first frame we receive is different to the last frame we
msousa@0: * just sent, then we are *not* working on a RS-485 bus, and
msousa@0: * that is already the real response to our query.
msousa@0: *
msousa@0: * Flushing the input cache immediately *after* sending a frame
msousa@0: * could solve this issue, but we have no guarantee that this
msousa@0: * process would not get swapped out between the write() and
msousa@0: * flush() calls, and we could therefore be flushing the response
msousa@0: * frame along with the last frame we sent!
msousa@0: */
msousa@0:
msousa@0: iter = 0;
msousa@0: while ((res = recv_length = read_frame(nd_entry, recv_data_ptr, ts_ptr)) >= 0) {
msousa@0: if (iter < INT_MAX) iter++;
msousa@0:
msousa@0: if ((send_length <= 0) || (nd_entry->ignore_echo == 0))
msousa@0: /* any valid frame will do... */
msousa@0: return recv_length;
msousa@0:
msousa@0: if ((send_length > L2_FRAME_SLAVEID_OFS + 1) && (iter == 1))
msousa@0: /* We have a frame in send_data,
msousa@0: * so we must make sure we are not reading in the frame just sent...
msousa@0: *
msousa@0: * We must only do this for the first frame we read. Subsequent
msousa@0: * frames are guaranteed not to be the previously sent frame
msousa@0: * since the modbus_ascii_write() resets the recv buffer.
msousa@0: * Remember too that valid modbus responses may be exactly the same
msousa@0: * as the request frame!!
msousa@0: */
msousa@0: if (recv_length == send_length)
msousa@0: if (memcmp(*recv_data_ptr, send_data, recv_length) == 0)
msousa@0: /* recv == send !!! */
msousa@0: /* read in another frame. */
msousa@0: continue;
msousa@0:
msousa@0: /* The frame read is either:
msousa@0: * - different to the frame in send_data
msousa@0: * - or there is only the slave id in send_data[L2_FRAME_SLAVEID_OFS]
msousa@0: * - or both of the above...
msousa@0: */
msousa@0: if (send_length > L2_FRAME_SLAVEID_OFS)
msousa@0: if (recv_length > L2_FRAME_SLAVEID_OFS)
msousa@0: /* check that frame is from/to the correct slave... */
msousa@0: if ((*recv_data_ptr)[L2_FRAME_SLAVEID_OFS] == send_data[L2_FRAME_SLAVEID_OFS])
msousa@0: /* yep, it is... */
msousa@0: return recv_length;
msousa@0:
msousa@0: /* The frame we have received is not acceptable...
msousa@0: * Let's read a new frame.
msousa@0: */
msousa@0: } /* while(...) */
msousa@0:
msousa@0: /* error reading response! */
msousa@0: /* Return the error returned by read_frame! */
msousa@0: return res;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**** Initialising and Shutting Down Library ****/
msousa@0: /**** ****/
msousa@0: /**** ****/
msousa@0: /**************************************************************/
msousa@0: /**************************************************************/
msousa@0:
msousa@0: /******************************/
msousa@0: /** **/
msousa@0: /** Load Default Values **/
msousa@0: /** **/
msousa@0: /******************************/
msousa@0:
msousa@0: static void set_defaults(int *baud,
msousa@0: int *parity,
msousa@0: int *data_bits,
msousa@0: int *stop_bits) {
msousa@0: /* Set the default values, if required... */
msousa@0: if (*baud == 0)
msousa@0: *baud = DEF_BAUD_RATE;
msousa@0: if (*data_bits == 0)
msousa@0: *data_bits = DEF_DATA_BITS;
msousa@0: if (*stop_bits == 0) {
msousa@0: if (*parity == 0)
msousa@0: *stop_bits = DEF_STOP_BITS_NOP; /* no parity */
msousa@0: else
msousa@0: *stop_bits = DEF_STOP_BITS_PAR; /* parity used */
msousa@0: }
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /******************************/
msousa@0: /** **/
msousa@0: /** Initialise Library **/
msousa@0: /** **/
msousa@0: /******************************/
msousa@0:
msousa@0: int modbus_ascii_init(int nd_count,
msousa@0: optimization_t opt,
msousa@0: int *extra_bytes)
msousa@0: {
msousa@0: #ifdef DEBUG
msousa@0: printf("modbus_asc_init(): called...\n");
msousa@0: printf("creating %d node descriptors\n", nd_count);
msousa@0: if (opt == optimize_speed)
msousa@0: printf("optimizing for speed\n");
msousa@0: if (opt == optimize_size)
msousa@0: printf("optimizing for size\n");
msousa@0: #endif
msousa@0:
msousa@0: /* check input parameters...*/
msousa@0: if (0 == nd_count) {
msousa@0: if (extra_bytes != NULL)
msousa@0: // Not the corect value for this layer.
msousa@0: // What we set it to in case this layer is not used!
msousa@0: *extra_bytes = 0;
msousa@0: return 0;
msousa@0: }
msousa@0: if (nd_count <= 0)
msousa@0: goto error_exit_0;
msousa@0:
msousa@0: if (extra_bytes == NULL)
msousa@0: goto error_exit_0;
msousa@0:
msousa@0: /* initialise nd table... */
msousa@0: if (nd_table_init(&nd_table_, nd_count) < 0)
msousa@0: goto error_exit_0;
msousa@0:
msousa@0: /* remember the optimization choice for later reference... */
msousa@0: optimization_ = opt;
msousa@0:
msousa@0: #ifdef DEBUG
msousa@0: printf("modbus_asc_init(): returning succesfuly...\n");
msousa@0: #endif
msousa@0: return 0;
msousa@0:
msousa@0: error_exit_0:
msousa@0: if (extra_bytes != NULL)
msousa@0: *extra_bytes = 0; // The value we set this to in case of error.
msousa@0: return -1;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: /******************************/
msousa@0: /** **/
msousa@0: /** Open node descriptor **/
msousa@0: /** **/
msousa@0: /******************************/
msousa@0:
msousa@0: /* Open a node for master or slave operation.
msousa@0: * Returns the node descriptor, or -1 on error.
msousa@0: *
msousa@0: * This function is mapped onto both
msousa@0: * modbus_connect() and modbus_listen()
msousa@0: */
msousa@0: int modbus_ascii_connect(node_addr_t node_addr) {
msousa@0: int node_descriptor;
msousa@0: nd_entry_t *nd_entry;
msousa@0:
msousa@0: #ifdef DEBUG
msousa@0: printf("modbus_ascii_connect(): called...\n");
msousa@0: printf("opening %s\n", node_addr.addr.ascii.device);
msousa@0: printf("baud_rate = %d\n", node_addr.addr.ascii.baud);
msousa@0: printf("parity = %d\n", node_addr.addr.ascii.parity);
msousa@0: printf("data_bits = %d\n", node_addr.addr.ascii.data_bits);
msousa@0: printf("stop_bits = %d\n", node_addr.addr.ascii.stop_bits);
msousa@0: printf("ignore_echo = %d\n", node_addr.addr.ascii.ignore_echo);
msousa@0: #endif
msousa@0:
msousa@0: /* Check for valid address family */
msousa@0: if (node_addr.naf != naf_ascii)
msousa@0: /* wrong address type... */
msousa@0: goto error_exit_0;
msousa@0:
msousa@0: /* find a free node descriptor */
msousa@0: if ((node_descriptor = nd_table_get_free_nd(&nd_table_)) < 0)
msousa@0: /* if no free nodes to initialize, then we are finished... */
msousa@0: goto error_exit_0;
msousa@0: if ((nd_entry = nd_table_get_nd(&nd_table_, node_descriptor)) == NULL)
msousa@0: /* strange, this should not occur... */
msousa@0: goto error_exit_0;
msousa@0:
msousa@0: /* set the default values... */
msousa@0: set_defaults(&(node_addr.addr.ascii.baud),
msousa@0: &(node_addr.addr.ascii.parity),
msousa@0: &(node_addr.addr.ascii.data_bits),
msousa@0: &(node_addr.addr.ascii.stop_bits));
msousa@0:
msousa@0: if (nd_entry_connect(nd_entry, &node_addr, optimization_) < 0)
msousa@0: goto error_exit_0;
msousa@0:
msousa@0: #ifdef DEBUG
msousa@0: printf("modbus_ascii_connect(): %s open\n", node_addr.addr.ascii.device );
msousa@0: printf("modbus_ascii_connect(): returning nd=%d\n", node_descriptor);
msousa@0: #endif
msousa@0: return node_descriptor;
msousa@0:
msousa@0: error_exit_0:
msousa@0: return -1;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: int modbus_ascii_listen(node_addr_t node_addr) {
msousa@0: return modbus_ascii_connect(node_addr);
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: /******************************/
msousa@0: /** **/
msousa@0: /** Close node descriptor **/
msousa@0: /** **/
msousa@0: /******************************/
msousa@0:
msousa@0: int modbus_ascii_close(int nd) {
msousa@0: return nd_table_free_nd(&nd_table_, nd);
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0: /******************************/
msousa@0: /** **/
msousa@0: /** Shutdown Library **/
msousa@0: /** **/
msousa@0: /******************************/
msousa@0:
msousa@0: int modbus_ascii_done(void) {
msousa@0: nd_table_done(&nd_table_);
msousa@0: return 0;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /******************************/
msousa@0: /** **/
msousa@0: /** **/
msousa@0: /** **/
msousa@0: /******************************/
msousa@0: int modbus_ascii_silence_init(void) {
msousa@0: return 0;
msousa@0: }
msousa@0:
msousa@0:
msousa@0:
msousa@0:
msousa@0: /******************************/
msousa@0: /** **/
msousa@0: /** **/
msousa@0: /** **/
msousa@0: /******************************/
msousa@0:
msousa@0:
msousa@0: double modbus_ascii_get_min_timeout(int baud,
msousa@0: int parity,
msousa@0: int data_bits,
msousa@0: int stop_bits) {
msousa@0: int parity_bits, start_bits, char_bits;
msousa@0:
msousa@0: set_defaults(&baud, &parity, &data_bits, &stop_bits);
msousa@0: parity_bits = (parity == 0)?0:1;
msousa@0: start_bits = 1;
msousa@0: char_bits = start_bits + data_bits + parity_bits + stop_bits;
msousa@0: return (double)((MAX_ASC_FRAME_LENGTH * char_bits) / baud);
msousa@0: }
msousa@0:
msousa@0:
msousa@0: