Forking https://os.mbed.com/users/cam/code/Modbus/ to work for NUCLEO 64 boards

Fork of Cam's original FreeModbus port (https://os.mbed.com/users/cam/code/Modbus/)

Change: - Serial implementation to work for NUCLEO 64 boards and receive interrupts instead of timer. (see `portserial.cpp`)

Added: - Custom RTU mode. Allows for external implementation of packet receiving and sending. Sends and receives packets as whole frames (address + PDU) (i.e. this was added for a custom LoRa implementation). implement `xMBRTUCustGetPDU` and `xMBRTUCustSendResponse` (see `mbport.h`) and call `eMBRTUCustomInit( address )`. implementations need to be fully initialised as `eMBRTUCustomInit` only sets the address and nothing else.

Committer:
cam
Date:
Thu Apr 15 12:10:34 2010 +0000
Revision:
0:0453a0a7e500

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cam 0:0453a0a7e500 1 /*
cam 0:0453a0a7e500 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
cam 0:0453a0a7e500 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
cam 0:0453a0a7e500 4 * All rights reserved.
cam 0:0453a0a7e500 5 *
cam 0:0453a0a7e500 6 * Redistribution and use in source and binary forms, with or without
cam 0:0453a0a7e500 7 * modification, are permitted provided that the following conditions
cam 0:0453a0a7e500 8 * are met:
cam 0:0453a0a7e500 9 * 1. Redistributions of source code must retain the above copyright
cam 0:0453a0a7e500 10 * notice, this list of conditions and the following disclaimer.
cam 0:0453a0a7e500 11 * 2. Redistributions in binary form must reproduce the above copyright
cam 0:0453a0a7e500 12 * notice, this list of conditions and the following disclaimer in the
cam 0:0453a0a7e500 13 * documentation and/or other materials provided with the distribution.
cam 0:0453a0a7e500 14 * 3. The name of the author may not be used to endorse or promote products
cam 0:0453a0a7e500 15 * derived from this software without specific prior written permission.
cam 0:0453a0a7e500 16 *
cam 0:0453a0a7e500 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
cam 0:0453a0a7e500 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
cam 0:0453a0a7e500 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
cam 0:0453a0a7e500 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
cam 0:0453a0a7e500 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
cam 0:0453a0a7e500 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
cam 0:0453a0a7e500 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
cam 0:0453a0a7e500 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
cam 0:0453a0a7e500 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
cam 0:0453a0a7e500 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cam 0:0453a0a7e500 27 *
cam 0:0453a0a7e500 28 * File: $Id: mbfunccoils.c,v 1.8 2007/02/18 23:47:16 wolti Exp $
cam 0:0453a0a7e500 29 */
cam 0:0453a0a7e500 30
cam 0:0453a0a7e500 31 /* ----------------------- System includes ----------------------------------*/
cam 0:0453a0a7e500 32 #include "stdlib.h"
cam 0:0453a0a7e500 33 #include "string.h"
cam 0:0453a0a7e500 34
cam 0:0453a0a7e500 35 /* ----------------------- Platform includes --------------------------------*/
cam 0:0453a0a7e500 36 #include "port.h"
cam 0:0453a0a7e500 37
cam 0:0453a0a7e500 38 /* ----------------------- Modbus includes ----------------------------------*/
cam 0:0453a0a7e500 39 #include "mb.h"
cam 0:0453a0a7e500 40 #include "mbframe.h"
cam 0:0453a0a7e500 41 #include "mbproto.h"
cam 0:0453a0a7e500 42 #include "mbconfig.h"
cam 0:0453a0a7e500 43
cam 0:0453a0a7e500 44 /* ----------------------- Defines ------------------------------------------*/
cam 0:0453a0a7e500 45 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
cam 0:0453a0a7e500 46 #define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
cam 0:0453a0a7e500 47 #define MB_PDU_FUNC_READ_SIZE ( 4 )
cam 0:0453a0a7e500 48 #define MB_PDU_FUNC_READ_COILCNT_MAX ( 0x07D0 )
cam 0:0453a0a7e500 49
cam 0:0453a0a7e500 50 #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
cam 0:0453a0a7e500 51 #define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
cam 0:0453a0a7e500 52 #define MB_PDU_FUNC_WRITE_SIZE ( 4 )
cam 0:0453a0a7e500 53
cam 0:0453a0a7e500 54 #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
cam 0:0453a0a7e500 55 #define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
cam 0:0453a0a7e500 56 #define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
cam 0:0453a0a7e500 57 #define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
cam 0:0453a0a7e500 58 #define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
cam 0:0453a0a7e500 59 #define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ( 0x07B0 )
cam 0:0453a0a7e500 60
cam 0:0453a0a7e500 61 /* ----------------------- Static functions ---------------------------------*/
cam 0:0453a0a7e500 62 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
cam 0:0453a0a7e500 63
cam 0:0453a0a7e500 64 /* ----------------------- Start implementation -----------------------------*/
cam 0:0453a0a7e500 65
cam 0:0453a0a7e500 66 #if MB_FUNC_READ_COILS_ENABLED > 0
cam 0:0453a0a7e500 67
cam 0:0453a0a7e500 68 eMBException
cam 0:0453a0a7e500 69 eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
cam 0:0453a0a7e500 70 {
cam 0:0453a0a7e500 71 USHORT usRegAddress;
cam 0:0453a0a7e500 72 USHORT usCoilCount;
cam 0:0453a0a7e500 73 UCHAR ucNBytes;
cam 0:0453a0a7e500 74 UCHAR *pucFrameCur;
cam 0:0453a0a7e500 75
cam 0:0453a0a7e500 76 eMBException eStatus = MB_EX_NONE;
cam 0:0453a0a7e500 77 eMBErrorCode eRegStatus;
cam 0:0453a0a7e500 78
cam 0:0453a0a7e500 79 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
cam 0:0453a0a7e500 80 {
cam 0:0453a0a7e500 81 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
cam 0:0453a0a7e500 82 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
cam 0:0453a0a7e500 83 usRegAddress++;
cam 0:0453a0a7e500 84
cam 0:0453a0a7e500 85 usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
cam 0:0453a0a7e500 86 usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
cam 0:0453a0a7e500 87
cam 0:0453a0a7e500 88 /* Check if the number of registers to read is valid. If not
cam 0:0453a0a7e500 89 * return Modbus illegal data value exception.
cam 0:0453a0a7e500 90 */
cam 0:0453a0a7e500 91 if( ( usCoilCount >= 1 ) &&
cam 0:0453a0a7e500 92 ( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
cam 0:0453a0a7e500 93 {
cam 0:0453a0a7e500 94 /* Set the current PDU data pointer to the beginning. */
cam 0:0453a0a7e500 95 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
cam 0:0453a0a7e500 96 *usLen = MB_PDU_FUNC_OFF;
cam 0:0453a0a7e500 97
cam 0:0453a0a7e500 98 /* First byte contains the function code. */
cam 0:0453a0a7e500 99 *pucFrameCur++ = MB_FUNC_READ_COILS;
cam 0:0453a0a7e500 100 *usLen += 1;
cam 0:0453a0a7e500 101
cam 0:0453a0a7e500 102 /* Test if the quantity of coils is a multiple of 8. If not last
cam 0:0453a0a7e500 103 * byte is only partially field with unused coils set to zero. */
cam 0:0453a0a7e500 104 if( ( usCoilCount & 0x0007 ) != 0 )
cam 0:0453a0a7e500 105 {
cam 0:0453a0a7e500 106 ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
cam 0:0453a0a7e500 107 }
cam 0:0453a0a7e500 108 else
cam 0:0453a0a7e500 109 {
cam 0:0453a0a7e500 110 ucNBytes = ( UCHAR )( usCoilCount / 8 );
cam 0:0453a0a7e500 111 }
cam 0:0453a0a7e500 112 *pucFrameCur++ = ucNBytes;
cam 0:0453a0a7e500 113 *usLen += 1;
cam 0:0453a0a7e500 114
cam 0:0453a0a7e500 115 eRegStatus =
cam 0:0453a0a7e500 116 eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount,
cam 0:0453a0a7e500 117 MB_REG_READ );
cam 0:0453a0a7e500 118
cam 0:0453a0a7e500 119 /* If an error occured convert it into a Modbus exception. */
cam 0:0453a0a7e500 120 if( eRegStatus != MB_ENOERR )
cam 0:0453a0a7e500 121 {
cam 0:0453a0a7e500 122 eStatus = prveMBError2Exception( eRegStatus );
cam 0:0453a0a7e500 123 }
cam 0:0453a0a7e500 124 else
cam 0:0453a0a7e500 125 {
cam 0:0453a0a7e500 126 /* The response contains the function code, the starting address
cam 0:0453a0a7e500 127 * and the quantity of registers. We reuse the old values in the
cam 0:0453a0a7e500 128 * buffer because they are still valid. */
cam 0:0453a0a7e500 129 *usLen += ucNBytes;;
cam 0:0453a0a7e500 130 }
cam 0:0453a0a7e500 131 }
cam 0:0453a0a7e500 132 else
cam 0:0453a0a7e500 133 {
cam 0:0453a0a7e500 134 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
cam 0:0453a0a7e500 135 }
cam 0:0453a0a7e500 136 }
cam 0:0453a0a7e500 137 else
cam 0:0453a0a7e500 138 {
cam 0:0453a0a7e500 139 /* Can't be a valid read coil register request because the length
cam 0:0453a0a7e500 140 * is incorrect. */
cam 0:0453a0a7e500 141 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
cam 0:0453a0a7e500 142 }
cam 0:0453a0a7e500 143 return eStatus;
cam 0:0453a0a7e500 144 }
cam 0:0453a0a7e500 145
cam 0:0453a0a7e500 146 #if MB_FUNC_WRITE_COIL_ENABLED > 0
cam 0:0453a0a7e500 147 eMBException
cam 0:0453a0a7e500 148 eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
cam 0:0453a0a7e500 149 {
cam 0:0453a0a7e500 150 USHORT usRegAddress;
cam 0:0453a0a7e500 151 UCHAR ucBuf[2];
cam 0:0453a0a7e500 152
cam 0:0453a0a7e500 153 eMBException eStatus = MB_EX_NONE;
cam 0:0453a0a7e500 154 eMBErrorCode eRegStatus;
cam 0:0453a0a7e500 155
cam 0:0453a0a7e500 156 if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
cam 0:0453a0a7e500 157 {
cam 0:0453a0a7e500 158 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
cam 0:0453a0a7e500 159 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
cam 0:0453a0a7e500 160 usRegAddress++;
cam 0:0453a0a7e500 161
cam 0:0453a0a7e500 162 if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
cam 0:0453a0a7e500 163 ( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
cam 0:0453a0a7e500 164 ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
cam 0:0453a0a7e500 165 {
cam 0:0453a0a7e500 166 ucBuf[1] = 0;
cam 0:0453a0a7e500 167 if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
cam 0:0453a0a7e500 168 {
cam 0:0453a0a7e500 169 ucBuf[0] = 1;
cam 0:0453a0a7e500 170 }
cam 0:0453a0a7e500 171 else
cam 0:0453a0a7e500 172 {
cam 0:0453a0a7e500 173 ucBuf[0] = 0;
cam 0:0453a0a7e500 174 }
cam 0:0453a0a7e500 175 eRegStatus =
cam 0:0453a0a7e500 176 eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
cam 0:0453a0a7e500 177
cam 0:0453a0a7e500 178 /* If an error occured convert it into a Modbus exception. */
cam 0:0453a0a7e500 179 if( eRegStatus != MB_ENOERR )
cam 0:0453a0a7e500 180 {
cam 0:0453a0a7e500 181 eStatus = prveMBError2Exception( eRegStatus );
cam 0:0453a0a7e500 182 }
cam 0:0453a0a7e500 183 }
cam 0:0453a0a7e500 184 else
cam 0:0453a0a7e500 185 {
cam 0:0453a0a7e500 186 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
cam 0:0453a0a7e500 187 }
cam 0:0453a0a7e500 188 }
cam 0:0453a0a7e500 189 else
cam 0:0453a0a7e500 190 {
cam 0:0453a0a7e500 191 /* Can't be a valid write coil register request because the length
cam 0:0453a0a7e500 192 * is incorrect. */
cam 0:0453a0a7e500 193 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
cam 0:0453a0a7e500 194 }
cam 0:0453a0a7e500 195 return eStatus;
cam 0:0453a0a7e500 196 }
cam 0:0453a0a7e500 197
cam 0:0453a0a7e500 198 #endif
cam 0:0453a0a7e500 199
cam 0:0453a0a7e500 200 #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
cam 0:0453a0a7e500 201 eMBException
cam 0:0453a0a7e500 202 eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
cam 0:0453a0a7e500 203 {
cam 0:0453a0a7e500 204 USHORT usRegAddress;
cam 0:0453a0a7e500 205 USHORT usCoilCnt;
cam 0:0453a0a7e500 206 UCHAR ucByteCount;
cam 0:0453a0a7e500 207 UCHAR ucByteCountVerify;
cam 0:0453a0a7e500 208
cam 0:0453a0a7e500 209 eMBException eStatus = MB_EX_NONE;
cam 0:0453a0a7e500 210 eMBErrorCode eRegStatus;
cam 0:0453a0a7e500 211
cam 0:0453a0a7e500 212 if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
cam 0:0453a0a7e500 213 {
cam 0:0453a0a7e500 214 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
cam 0:0453a0a7e500 215 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
cam 0:0453a0a7e500 216 usRegAddress++;
cam 0:0453a0a7e500 217
cam 0:0453a0a7e500 218 usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
cam 0:0453a0a7e500 219 usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
cam 0:0453a0a7e500 220
cam 0:0453a0a7e500 221 ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
cam 0:0453a0a7e500 222
cam 0:0453a0a7e500 223 /* Compute the number of expected bytes in the request. */
cam 0:0453a0a7e500 224 if( ( usCoilCnt & 0x0007 ) != 0 )
cam 0:0453a0a7e500 225 {
cam 0:0453a0a7e500 226 ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
cam 0:0453a0a7e500 227 }
cam 0:0453a0a7e500 228 else
cam 0:0453a0a7e500 229 {
cam 0:0453a0a7e500 230 ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
cam 0:0453a0a7e500 231 }
cam 0:0453a0a7e500 232
cam 0:0453a0a7e500 233 if( ( usCoilCnt >= 1 ) &&
cam 0:0453a0a7e500 234 ( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) &&
cam 0:0453a0a7e500 235 ( ucByteCountVerify == ucByteCount ) )
cam 0:0453a0a7e500 236 {
cam 0:0453a0a7e500 237 eRegStatus =
cam 0:0453a0a7e500 238 eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
cam 0:0453a0a7e500 239 usRegAddress, usCoilCnt, MB_REG_WRITE );
cam 0:0453a0a7e500 240
cam 0:0453a0a7e500 241 /* If an error occured convert it into a Modbus exception. */
cam 0:0453a0a7e500 242 if( eRegStatus != MB_ENOERR )
cam 0:0453a0a7e500 243 {
cam 0:0453a0a7e500 244 eStatus = prveMBError2Exception( eRegStatus );
cam 0:0453a0a7e500 245 }
cam 0:0453a0a7e500 246 else
cam 0:0453a0a7e500 247 {
cam 0:0453a0a7e500 248 /* The response contains the function code, the starting address
cam 0:0453a0a7e500 249 * and the quantity of registers. We reuse the old values in the
cam 0:0453a0a7e500 250 * buffer because they are still valid. */
cam 0:0453a0a7e500 251 *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
cam 0:0453a0a7e500 252 }
cam 0:0453a0a7e500 253 }
cam 0:0453a0a7e500 254 else
cam 0:0453a0a7e500 255 {
cam 0:0453a0a7e500 256 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
cam 0:0453a0a7e500 257 }
cam 0:0453a0a7e500 258 }
cam 0:0453a0a7e500 259 else
cam 0:0453a0a7e500 260 {
cam 0:0453a0a7e500 261 /* Can't be a valid write coil register request because the length
cam 0:0453a0a7e500 262 * is incorrect. */
cam 0:0453a0a7e500 263 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
cam 0:0453a0a7e500 264 }
cam 0:0453a0a7e500 265 return eStatus;
cam 0:0453a0a7e500 266 }
cam 0:0453a0a7e500 267
cam 0:0453a0a7e500 268 #endif
cam 0:0453a0a7e500 269
cam 0:0453a0a7e500 270 #endif