Modified version of ModbusTCP

Dependencies:   EthernetNetIf mbed

Committer:
paleskyjp
Date:
Thu Mar 15 15:28:14 2012 +0000
Revision:
3:d7d7c67f21fa
Parent:
0:62be54b8975d
eMBRegCoilsCB was corrected.
(Implementation of writing single coil was wrong.)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
paleskyjp 0:62be54b8975d 1 /*
paleskyjp 0:62be54b8975d 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
paleskyjp 0:62be54b8975d 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
paleskyjp 0:62be54b8975d 4 * All rights reserved.
paleskyjp 0:62be54b8975d 5 *
paleskyjp 0:62be54b8975d 6 * Redistribution and use in source and binary forms, with or without
paleskyjp 0:62be54b8975d 7 * modification, are permitted provided that the following conditions
paleskyjp 0:62be54b8975d 8 * are met:
paleskyjp 0:62be54b8975d 9 * 1. Redistributions of source code must retain the above copyright
paleskyjp 0:62be54b8975d 10 * notice, this list of conditions and the following disclaimer.
paleskyjp 0:62be54b8975d 11 * 2. Redistributions in binary form must reproduce the above copyright
paleskyjp 0:62be54b8975d 12 * notice, this list of conditions and the following disclaimer in the
paleskyjp 0:62be54b8975d 13 * documentation and/or other materials provided with the distribution.
paleskyjp 0:62be54b8975d 14 * 3. The name of the author may not be used to endorse or promote products
paleskyjp 0:62be54b8975d 15 * derived from this software without specific prior written permission.
paleskyjp 0:62be54b8975d 16 *
paleskyjp 0:62be54b8975d 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
paleskyjp 0:62be54b8975d 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
paleskyjp 0:62be54b8975d 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
paleskyjp 0:62be54b8975d 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
paleskyjp 0:62be54b8975d 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
paleskyjp 0:62be54b8975d 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
paleskyjp 0:62be54b8975d 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
paleskyjp 0:62be54b8975d 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
paleskyjp 0:62be54b8975d 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
paleskyjp 0:62be54b8975d 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
paleskyjp 0:62be54b8975d 27 *
paleskyjp 0:62be54b8975d 28 * File: $Id: mbutils.c,v 1.6 2007/02/18 23:49:07 wolti Exp $
paleskyjp 0:62be54b8975d 29 */
paleskyjp 0:62be54b8975d 30
paleskyjp 0:62be54b8975d 31 /* ----------------------- System includes ----------------------------------*/
paleskyjp 0:62be54b8975d 32 #include "stdlib.h"
paleskyjp 0:62be54b8975d 33 #include "string.h"
paleskyjp 0:62be54b8975d 34
paleskyjp 0:62be54b8975d 35 /* ----------------------- Platform includes --------------------------------*/
paleskyjp 0:62be54b8975d 36 #include "port.h"
paleskyjp 0:62be54b8975d 37
paleskyjp 0:62be54b8975d 38 /* ----------------------- Modbus includes ----------------------------------*/
paleskyjp 0:62be54b8975d 39 #include "mb.h"
paleskyjp 0:62be54b8975d 40 #include "mbproto.h"
paleskyjp 0:62be54b8975d 41
paleskyjp 0:62be54b8975d 42 /* ----------------------- Defines ------------------------------------------*/
paleskyjp 0:62be54b8975d 43 #define BITS_UCHAR 8U
paleskyjp 0:62be54b8975d 44
paleskyjp 0:62be54b8975d 45 /* ----------------------- Start implementation -----------------------------*/
paleskyjp 0:62be54b8975d 46 void
paleskyjp 0:62be54b8975d 47 xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
paleskyjp 0:62be54b8975d 48 UCHAR ucValue )
paleskyjp 0:62be54b8975d 49 {
paleskyjp 0:62be54b8975d 50 USHORT usWordBuf;
paleskyjp 0:62be54b8975d 51 USHORT usMask;
paleskyjp 0:62be54b8975d 52 USHORT usByteOffset;
paleskyjp 0:62be54b8975d 53 USHORT usNPreBits;
paleskyjp 0:62be54b8975d 54 USHORT usValue = ucValue;
paleskyjp 0:62be54b8975d 55
paleskyjp 0:62be54b8975d 56 assert( ucNBits <= 8 );
paleskyjp 0:62be54b8975d 57 assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
paleskyjp 0:62be54b8975d 58
paleskyjp 0:62be54b8975d 59 /* Calculate byte offset for first byte containing the bit values starting
paleskyjp 0:62be54b8975d 60 * at usBitOffset. */
paleskyjp 0:62be54b8975d 61 usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
paleskyjp 0:62be54b8975d 62
paleskyjp 0:62be54b8975d 63 /* How many bits precede our bits to set. */
paleskyjp 0:62be54b8975d 64 usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
paleskyjp 0:62be54b8975d 65
paleskyjp 0:62be54b8975d 66 /* Move bit field into position over bits to set */
paleskyjp 0:62be54b8975d 67 usValue <<= usNPreBits;
paleskyjp 0:62be54b8975d 68
paleskyjp 0:62be54b8975d 69 /* Prepare a mask for setting the new bits. */
paleskyjp 0:62be54b8975d 70 usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
paleskyjp 0:62be54b8975d 71 usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
paleskyjp 0:62be54b8975d 72
paleskyjp 0:62be54b8975d 73 /* copy bits into temporary storage. */
paleskyjp 0:62be54b8975d 74 usWordBuf = ucByteBuf[usByteOffset];
paleskyjp 0:62be54b8975d 75 usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
paleskyjp 0:62be54b8975d 76
paleskyjp 0:62be54b8975d 77 /* Zero out bit field bits and then or value bits into them. */
paleskyjp 0:62be54b8975d 78 usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
paleskyjp 0:62be54b8975d 79
paleskyjp 0:62be54b8975d 80 /* move bits back into storage */
paleskyjp 0:62be54b8975d 81 ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
paleskyjp 0:62be54b8975d 82 ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
paleskyjp 0:62be54b8975d 83 }
paleskyjp 0:62be54b8975d 84
paleskyjp 0:62be54b8975d 85 UCHAR
paleskyjp 0:62be54b8975d 86 xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
paleskyjp 0:62be54b8975d 87 {
paleskyjp 0:62be54b8975d 88 USHORT usWordBuf;
paleskyjp 0:62be54b8975d 89 USHORT usMask;
paleskyjp 0:62be54b8975d 90 USHORT usByteOffset;
paleskyjp 0:62be54b8975d 91 USHORT usNPreBits;
paleskyjp 0:62be54b8975d 92
paleskyjp 0:62be54b8975d 93 /* Calculate byte offset for first byte containing the bit values starting
paleskyjp 0:62be54b8975d 94 * at usBitOffset. */
paleskyjp 0:62be54b8975d 95 usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
paleskyjp 0:62be54b8975d 96
paleskyjp 0:62be54b8975d 97 /* How many bits precede our bits to set. */
paleskyjp 0:62be54b8975d 98 usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
paleskyjp 0:62be54b8975d 99
paleskyjp 0:62be54b8975d 100 /* Prepare a mask for setting the new bits. */
paleskyjp 0:62be54b8975d 101 usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
paleskyjp 0:62be54b8975d 102
paleskyjp 0:62be54b8975d 103 /* copy bits into temporary storage. */
paleskyjp 0:62be54b8975d 104 usWordBuf = ucByteBuf[usByteOffset];
paleskyjp 0:62be54b8975d 105 usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
paleskyjp 0:62be54b8975d 106
paleskyjp 0:62be54b8975d 107 /* throw away unneeded bits. */
paleskyjp 0:62be54b8975d 108 usWordBuf >>= usNPreBits;
paleskyjp 0:62be54b8975d 109
paleskyjp 0:62be54b8975d 110 /* mask away bits above the requested bitfield. */
paleskyjp 0:62be54b8975d 111 usWordBuf &= usMask;
paleskyjp 0:62be54b8975d 112
paleskyjp 0:62be54b8975d 113 return ( UCHAR ) usWordBuf;
paleskyjp 0:62be54b8975d 114 }
paleskyjp 0:62be54b8975d 115
paleskyjp 0:62be54b8975d 116 eMBException
paleskyjp 0:62be54b8975d 117 prveMBError2Exception( eMBErrorCode eErrorCode )
paleskyjp 0:62be54b8975d 118 {
paleskyjp 0:62be54b8975d 119 eMBException eStatus;
paleskyjp 0:62be54b8975d 120
paleskyjp 0:62be54b8975d 121 switch ( eErrorCode )
paleskyjp 0:62be54b8975d 122 {
paleskyjp 0:62be54b8975d 123 case MB_ENOERR:
paleskyjp 0:62be54b8975d 124 eStatus = MB_EX_NONE;
paleskyjp 0:62be54b8975d 125 break;
paleskyjp 0:62be54b8975d 126
paleskyjp 0:62be54b8975d 127 case MB_ENOREG:
paleskyjp 0:62be54b8975d 128 eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
paleskyjp 0:62be54b8975d 129 break;
paleskyjp 0:62be54b8975d 130
paleskyjp 0:62be54b8975d 131 case MB_ETIMEDOUT:
paleskyjp 0:62be54b8975d 132 eStatus = MB_EX_SLAVE_BUSY;
paleskyjp 0:62be54b8975d 133 break;
paleskyjp 0:62be54b8975d 134
paleskyjp 0:62be54b8975d 135 default:
paleskyjp 0:62be54b8975d 136 eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
paleskyjp 0:62be54b8975d 137 break;
paleskyjp 0:62be54b8975d 138 }
paleskyjp 0:62be54b8975d 139
paleskyjp 0:62be54b8975d 140 return eStatus;
paleskyjp 0:62be54b8975d 141 }