from Cam Marshall original modbus

Dependencies:   mbed

Fork of Modbus by Cam Marshall

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: mbutils.c,v 1.6 2007/02/18 23:49:07 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 "mbproto.h"
cam 0:0453a0a7e500 41
cam 0:0453a0a7e500 42 /* ----------------------- Defines ------------------------------------------*/
cam 0:0453a0a7e500 43 #define BITS_UCHAR 8U
cam 0:0453a0a7e500 44
cam 0:0453a0a7e500 45 /* ----------------------- Start implementation -----------------------------*/
cam 0:0453a0a7e500 46 void
cam 0:0453a0a7e500 47 xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
cam 0:0453a0a7e500 48 UCHAR ucValue )
cam 0:0453a0a7e500 49 {
cam 0:0453a0a7e500 50 USHORT usWordBuf;
cam 0:0453a0a7e500 51 USHORT usMask;
cam 0:0453a0a7e500 52 USHORT usByteOffset;
cam 0:0453a0a7e500 53 USHORT usNPreBits;
cam 0:0453a0a7e500 54 USHORT usValue = ucValue;
cam 0:0453a0a7e500 55
cam 0:0453a0a7e500 56 assert( ucNBits <= 8 );
cam 0:0453a0a7e500 57 assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
cam 0:0453a0a7e500 58
cam 0:0453a0a7e500 59 /* Calculate byte offset for first byte containing the bit values starting
cam 0:0453a0a7e500 60 * at usBitOffset. */
cam 0:0453a0a7e500 61 usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
cam 0:0453a0a7e500 62
cam 0:0453a0a7e500 63 /* How many bits precede our bits to set. */
cam 0:0453a0a7e500 64 usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
cam 0:0453a0a7e500 65
cam 0:0453a0a7e500 66 /* Move bit field into position over bits to set */
cam 0:0453a0a7e500 67 usValue <<= usNPreBits;
cam 0:0453a0a7e500 68
cam 0:0453a0a7e500 69 /* Prepare a mask for setting the new bits. */
cam 0:0453a0a7e500 70 usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
cam 0:0453a0a7e500 71 usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
cam 0:0453a0a7e500 72
cam 0:0453a0a7e500 73 /* copy bits into temporary storage. */
cam 0:0453a0a7e500 74 usWordBuf = ucByteBuf[usByteOffset];
cam 0:0453a0a7e500 75 usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
cam 0:0453a0a7e500 76
cam 0:0453a0a7e500 77 /* Zero out bit field bits and then or value bits into them. */
cam 0:0453a0a7e500 78 usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
cam 0:0453a0a7e500 79
cam 0:0453a0a7e500 80 /* move bits back into storage */
cam 0:0453a0a7e500 81 ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
cam 0:0453a0a7e500 82 ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
cam 0:0453a0a7e500 83 }
cam 0:0453a0a7e500 84
cam 0:0453a0a7e500 85 UCHAR
cam 0:0453a0a7e500 86 xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
cam 0:0453a0a7e500 87 {
cam 0:0453a0a7e500 88 USHORT usWordBuf;
cam 0:0453a0a7e500 89 USHORT usMask;
cam 0:0453a0a7e500 90 USHORT usByteOffset;
cam 0:0453a0a7e500 91 USHORT usNPreBits;
cam 0:0453a0a7e500 92
cam 0:0453a0a7e500 93 /* Calculate byte offset for first byte containing the bit values starting
cam 0:0453a0a7e500 94 * at usBitOffset. */
cam 0:0453a0a7e500 95 usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
cam 0:0453a0a7e500 96
cam 0:0453a0a7e500 97 /* How many bits precede our bits to set. */
cam 0:0453a0a7e500 98 usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
cam 0:0453a0a7e500 99
cam 0:0453a0a7e500 100 /* Prepare a mask for setting the new bits. */
cam 0:0453a0a7e500 101 usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
cam 0:0453a0a7e500 102
cam 0:0453a0a7e500 103 /* copy bits into temporary storage. */
cam 0:0453a0a7e500 104 usWordBuf = ucByteBuf[usByteOffset];
cam 0:0453a0a7e500 105 usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
cam 0:0453a0a7e500 106
cam 0:0453a0a7e500 107 /* throw away unneeded bits. */
cam 0:0453a0a7e500 108 usWordBuf >>= usNPreBits;
cam 0:0453a0a7e500 109
cam 0:0453a0a7e500 110 /* mask away bits above the requested bitfield. */
cam 0:0453a0a7e500 111 usWordBuf &= usMask;
cam 0:0453a0a7e500 112
cam 0:0453a0a7e500 113 return ( UCHAR ) usWordBuf;
cam 0:0453a0a7e500 114 }
cam 0:0453a0a7e500 115
cam 0:0453a0a7e500 116 eMBException
cam 0:0453a0a7e500 117 prveMBError2Exception( eMBErrorCode eErrorCode )
cam 0:0453a0a7e500 118 {
cam 0:0453a0a7e500 119 eMBException eStatus;
cam 0:0453a0a7e500 120
cam 0:0453a0a7e500 121 switch ( eErrorCode )
cam 0:0453a0a7e500 122 {
cam 0:0453a0a7e500 123 case MB_ENOERR:
cam 0:0453a0a7e500 124 eStatus = MB_EX_NONE;
cam 0:0453a0a7e500 125 break;
cam 0:0453a0a7e500 126
cam 0:0453a0a7e500 127 case MB_ENOREG:
cam 0:0453a0a7e500 128 eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
cam 0:0453a0a7e500 129 break;
cam 0:0453a0a7e500 130
cam 0:0453a0a7e500 131 case MB_ETIMEDOUT:
cam 0:0453a0a7e500 132 eStatus = MB_EX_SLAVE_BUSY;
cam 0:0453a0a7e500 133 break;
cam 0:0453a0a7e500 134
cam 0:0453a0a7e500 135 default:
cam 0:0453a0a7e500 136 eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
cam 0:0453a0a7e500 137 break;
cam 0:0453a0a7e500 138 }
cam 0:0453a0a7e500 139
cam 0:0453a0a7e500 140 return eStatus;
cam 0:0453a0a7e500 141 }