msousa@0: /* msousa@0: * Copyright (c) 2001,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: #ifndef MODBUS_RTU_PRIVATE_H msousa@0: #define MODBUS_RTU_PRIVATE_H msousa@0: msousa@0: #include "mb_types.h" /* get the data types */ msousa@0: #include "mb_util.h" msousa@0: msousa@0: msousa@0: /* serial port default configuration... */ msousa@0: #define DEF_DATA_BITS 8 msousa@0: #define DEF_STOP_BITS_PAR 1 /* default stop bits if parity is used */ msousa@0: #define DEF_STOP_BITS_NOP 2 /* default stop bits if parity is not used */ msousa@0: #define DEF_BAUD_RATE 9600 msousa@0: msousa@0: msousa@0: /* Send retries of rtu frames... */ msousa@0: #define RTU_FRAME_SEND_RETRY 1 msousa@0: /* NOTES: msousa@0: * - the above are the retries at the layer1 level, msousa@0: * higher layers may decide to retry for themselves! msousa@0: */ msousa@0: msousa@0: msousa@0: /* Buffer sizes... */ msousa@0: /* We use double the maximum frame length for the read buffer, msousa@0: * due to the algorithm used to work around aborted frames. msousa@0: */ msousa@0: #define RECV_BUFFER_SIZE_SMALL (MAX_RTU_FRAME_LENGTH + 10) msousa@14: #define RECV_BUFFER_SIZE_LARGE (2 * (MAX_RTU_FRAME_LENGTH)) msousa@0: msousa@0: msousa@0: /* Frame lengths... */ msousa@0: msousa@0: /* The number of bytes in each frame format, excluding CRC. msousa@0: * msousa@0: * BYTE_COUNT_3 denotes that third byte of frame contains the number of bytes; msousa@0: * - total number of bytes in frame is msousa@0: * BYTE_COUNT_3_HEADER + byte_count msousa@0: * BYTE_COUNT_34 denotes that third+fourth bytes of frame contain the number of bytes; msousa@0: * - total number of bytes in frame is msousa@0: * BYTE_COUNT_34_HEADER + byte_count msousa@0: * BYTE_COUNT_7 denotes that seventh byte of frame contain the number of bytes; msousa@0: * - total number of bytes in frame is msousa@0: * BYTE_COUNT_7_HEADER + byte_count msousa@0: * BYTE_COUNT_11 denotes that eleventh byte of frame contain the number of bytes; msousa@0: * - total number of bytes in frame is msousa@0: * BYTE_COUNT_11_HEADER + byte_count msousa@0: * BYTE_COUNT_U denotes unknown number of bytes; msousa@0: */ msousa@0: msousa@0: #define BYTE_COUNT_3_HEADER 3 msousa@0: #define BYTE_COUNT_34_HEADER 4 msousa@0: #define BYTE_COUNT_7_HEADER 7 msousa@0: #define BYTE_COUNT_11_HEADER 11 msousa@0: msousa@0: #define BYTE_COUNT_3 (-3) msousa@0: #define BYTE_COUNT_34 (-34) msousa@0: #define BYTE_COUNT_7 (-7) msousa@0: #define BYTE_COUNT_11 (-11) msousa@0: #define BYTE_COUNT_U (-128) msousa@0: msousa@0: msousa@0: #define MAX_FUNCTION_CODE 0x18 msousa@0: msousa@0: #define MIN_FRAME_LENGTH 3 msousa@0: #define EXCEPTION_FRAME_LENGTH 3 msousa@0: msousa@0: static i8 query_frame_lengths[MAX_FUNCTION_CODE+1] = { msousa@0: /* 0x00 */ 0, /* unused */ msousa@0: /* 0x01 */ 6, /* Read Coil Status */ msousa@0: /* 0x02 */ 6, /* Read Input Status */ msousa@0: /* 0x03 */ 6, /* Read Holding Registers */ msousa@0: /* 0x04 */ 6, /* Read Input Registers */ msousa@0: /* 0x05 */ 6, /* Force Single Coil */ msousa@0: /* 0x06 */ 6, /* Preset Single Register */ msousa@0: /* 0x07 */ 2, /* Read Exception Status */ msousa@0: /* 0x08 */ 4, /* Diagnostics */ msousa@0: /* 0x09 */ BYTE_COUNT_U, /* Program 484 */ msousa@0: /* 0x0A */ BYTE_COUNT_U, /* Poll 484 */ msousa@0: /* 0x0B */ 2, /* Fetch Comm. Event Counter */ msousa@0: /* 0x0C */ 2, /* Fetch Comm. Event Log */ msousa@0: /* 0x0D */ BYTE_COUNT_U, /* Program Controller */ msousa@0: /* 0x0E */ BYTE_COUNT_U, /* Poll Controller */ msousa@0: /* 0x0F */ BYTE_COUNT_7, /* Force Multiple Coils */ msousa@0: /* 0x10 */ BYTE_COUNT_7, /* Preset Multiple Registers */ msousa@0: /* 0x11 */ 2, /* Report Slave ID */ msousa@0: /* 0x12 */ BYTE_COUNT_U, /* Program 884/M84 */ msousa@0: /* 0x13 */ BYTE_COUNT_U, /* Reset. Comm. Link */ msousa@0: /* 0x14 */ BYTE_COUNT_3, /* Read General Reference */ msousa@0: /* 0x15 */ BYTE_COUNT_3, /* Write General Reference */ msousa@0: /* 0x16 */ 8, /* Mask Write 4X Register */ msousa@0: /* 0x17 */ BYTE_COUNT_11, /* Read/Write 4x Register */ msousa@0: /* 0x18 */ 4 /* Read FIFO Queue */ msousa@0: }; msousa@0: msousa@0: static i8 response_frame_lengths[MAX_FUNCTION_CODE+1] = { msousa@0: /* 0x00 */ 0, /* unused */ msousa@0: /* 0x01 */ BYTE_COUNT_3, /* Read Coil Status */ msousa@0: /* 0x02 */ BYTE_COUNT_3, /* Read Input Status */ msousa@0: /* 0x03 */ BYTE_COUNT_3, /* Read Holding Registers */ msousa@0: /* 0x04 */ BYTE_COUNT_3, /* Read Input Registers */ msousa@0: /* 0x05 */ 6, /* Force Single Coil */ msousa@0: /* 0x06 */ 6, /* Preset Single Register */ msousa@0: /* 0x07 */ 3, /* Read Exception Status */ msousa@0: /* 0x08 */ 6,/*see (1)*/ /* Diagnostics */ msousa@0: /* 0x09 */ BYTE_COUNT_U, /* Program 484 */ msousa@0: /* 0x0A */ BYTE_COUNT_U, /* Poll 484 */ msousa@0: /* 0x0B */ 6, /* Fetch Comm. Event Counter */ msousa@0: /* 0x0C */ BYTE_COUNT_3, /* Fetch Comm. Event Log */ msousa@0: /* 0x0D */ BYTE_COUNT_U, /* Program Controller */ msousa@0: /* 0x0E */ BYTE_COUNT_U, /* Poll Controller */ msousa@0: /* 0x0F */ 6, /* Force Multiple Coils */ msousa@0: /* 0x10 */ 6, /* Preset Multiple Registers */ msousa@0: /* 0x11 */ BYTE_COUNT_3, /* Report Slave ID */ msousa@0: /* 0x12 */ BYTE_COUNT_U, /* Program 884/M84 */ msousa@0: /* 0x13 */ BYTE_COUNT_U, /* Reset. Comm. Link */ msousa@0: /* 0x14 */ BYTE_COUNT_3, /* Read General Reference */ msousa@0: /* 0x15 */ BYTE_COUNT_3, /* Write General Reference */ msousa@0: /* 0x16 */ 8, /* Mask Write 4X Register */ msousa@0: /* 0x17 */ BYTE_COUNT_3, /* Read/Write 4x Register */ msousa@0: /* 0x18 */ BYTE_COUNT_34 /* Read FIFO Queue */ msousa@0: }; msousa@0: msousa@0: /* NOTE (1): msousa@0: * The diagnostic function (0x08) has sub-functions. In particular, msousa@0: * sub-function 21 (0x15) has two sub-sub-functions. In the very msousa@0: * particular case of *one* of these sub-sub-functions, the reply msousa@0: * frame does *not* have a size of 4, but is variable in length msousa@0: * and includes a byte counter. msousa@0: * To take this into account in the table would require an extra two msousa@0: * tables. msousa@0: * The above length has been hardcoded into the frame_length() function msousa@0: * (in file modbus_rtu.c) msousa@0: */ msousa@0: msousa@0: msousa@0: #define FALSE 0 msousa@0: #define TRUE 1 msousa@0: msousa@0: msousa@0: #endif /* MODBUS_RTU_PRIVATE_H */ msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: