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: #ifndef MB_UTIL_H msousa@0: #define MB_UTIL_H msousa@0: msousa@0: /* This file has constants related to the modbus protocol */ msousa@0: /* msousa@0: * Some of these constants are specific to the layer two protocols msousa@0: * (i.e. master and slave), while others are specific of the msousa@0: * layer one protocols (i.e. rtu, ascii, tcp). msousa@0: * msousa@0: * a) Unfortunately, due to the nature of the modbus protocol, that does not msousa@0: * include a frame size field in the layer 1 frame (see note 1), and the msousa@0: * fact that we are implementing it at the user level, the implementation msousa@0: * of some layer 1 protocols need to know the content of the layer 2 protocol msousa@0: * in order to determine the size of the frame. msousa@0: * msousa@0: * b) The layer two message formats are in fact the same, just reversing the role msousa@0: * being played (master or slave). msousa@0: * msousa@0: * Bothe a) and b) mean we need the same modbus protocol constants in several files. msousa@0: * It ends up making more sense to put them all together in a single file, which msousa@0: * makes updating easier, even though we are trying to strictly seperate the layer 1 msousa@0: * and layer 2 protocols. msousa@0: * msousa@0: * msousa@0: * msousa@0: * Notes: msousa@0: * (1) There is no layer 1 field with the frame size, nevertheless this msousa@0: * size can be determined indirectly due to timing restrictions on the rtu msousa@0: * protocol. Unfortunately, due to the fact that we are implementing msousa@0: * it at the user level, we are not guaranteed to detect these timings msousa@0: * correctly, and therefore have to rely on layer 2 protocol info to msousa@0: * determine the frame size. msousa@0: * For the ascii protocol, the frame size is determined indirectly by msousa@0: * a frame header and tail, so we do not use layer 2 protocol info. msousa@0: */ msousa@0: msousa@0: msousa@0: /* Layer 2 Frame Structure... */ msousa@0: /* Valid for both master and slave protocols */ msousa@0: #define L2_FRAME_HEADER_LENGTH 6 msousa@0: #define L2_FRAME_BYTECOUNT_LENGTH 1 msousa@0: #define L2_FRAME_DATABYTES_LENGTH 255 msousa@0: #define MAX_L2_FRAME_LENGTH (L2_FRAME_HEADER_LENGTH + L2_FRAME_BYTECOUNT_LENGTH + \ msousa@0: L2_FRAME_DATABYTES_LENGTH) msousa@0: msousa@0: #define L2_FRAME_SLAVEID_OFS 0 msousa@0: #define L2_FRAME_FUNCTION_OFS 1 msousa@0: msousa@0: /* Layer 1 - Ascii Frame sizes... */ msousa@0: #define L2_TO_ASC_CODING 2 /* number of ascii bytes used to code a Layer 2 frame byte */ msousa@0: #define ASC_FRAME_HEADER_LENGTH 1 msousa@0: #define ASC_FRAME_HEADER ':' msousa@0: #define ASC_FRAME_TAIL_LENGTH 2 msousa@0: #define ASC_FRAME_TAIL_0 '\13' /* 'CR' */ msousa@0: #define ASC_FRAME_TAIL_1 '\10' /* 'LF' */ msousa@0: #define ASC_FRAME_LRC_LENGTH 2 msousa@0: msousa@0: /* Layer 1 - RTU Frame sizes... */ msousa@0: #define RTU_FRAME_CRC_LENGTH 2 msousa@0: msousa@0: /* Layer 1 - TCP Frame sizes... */ msousa@0: #define TCP_HEADER_LENGTH 6 msousa@0: msousa@0: /* Global Frame sizes */ msousa@14: #define MAX_RTU_FRAME_LENGTH (MAX_L2_FRAME_LENGTH + RTU_FRAME_CRC_LENGTH) msousa@0: #define MAX_ASC_FRAME_LENGTH ((MAX_L2_FRAME_LENGTH * L2_TO_ASC_CODING) + \ msousa@0: ASC_FRAME_HEADER_LENGTH + ASC_FRAME_TAIL_LENGTH + \ msousa@0: ASC_FRAME_LRC_LENGTH) msousa@0: msousa@0: msousa@0: /* Modbus Exception codes */ msousa@0: #define ERR_ILLEGAL_FUNCTION 0x01 msousa@0: #define ERR_ILLEGAL_DATA_ADDRESS 0x02 msousa@0: #define ERR_ILLEGAL_DATA_VALUE 0x03 msousa@0: #define ERR_SLAVE_DEVICE_FAILURE 0x04 msousa@0: #define ERR_ACKNOWLEDGE 0x05 msousa@0: #define ERR_SLAVE_DEVICE_BUSY 0x06 msousa@0: #define ERR_NEGATIVE_ACKNOWLEDGE 0x07 msousa@0: #define ERR_MEMORY_PARITY_ERROR 0x08 msousa@0: #define ERR_GATEWAY_PATH_UNAVAILABLE 0x0A msousa@0: #define ERR_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND 0x0B msousa@0: msousa@0: msousa@0: msousa@0: #endif /* MB_UTIL_H */ msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: