Trying to make Modbus code into a lib

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbutils.cpp Source File

mbutils.cpp

00001 /* 
00002  * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
00003  * Copyright (c) 2006 Christian Walter <wolti@sil.at>
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote products
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  *
00028  * File: $Id: mbutils.c,v 1.6 2007/02/18 23:49:07 wolti Exp $
00029  */
00030 
00031 /* ----------------------- System includes ----------------------------------*/
00032 #include "stdlib.h"
00033 #include "string.h"
00034 
00035 /* ----------------------- Platform includes --------------------------------*/
00036 #include "port.h"
00037 
00038 /* ----------------------- Modbus includes ----------------------------------*/
00039 #include "mb.h"
00040 #include "mbproto.h"
00041 
00042 /* ----------------------- Defines ------------------------------------------*/
00043 #define BITS_UCHAR      8U
00044 
00045 /* ----------------------- Start implementation -----------------------------*/
00046 void
00047 xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
00048                 UCHAR ucValue )
00049 {
00050     USHORT          usWordBuf;
00051     USHORT          usMask;
00052     USHORT          usByteOffset;
00053     USHORT          usNPreBits;
00054     USHORT          usValue = ucValue;
00055 
00056     assert( ucNBits <= 8 );
00057     assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
00058 
00059     /* Calculate byte offset for first byte containing the bit values starting
00060      * at usBitOffset. */
00061     usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
00062 
00063     /* How many bits precede our bits to set. */
00064     usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
00065 
00066     /* Move bit field into position over bits to set */
00067     usValue <<= usNPreBits;
00068 
00069     /* Prepare a mask for setting the new bits. */
00070     usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
00071     usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
00072 
00073     /* copy bits into temporary storage. */
00074     usWordBuf = ucByteBuf[usByteOffset];
00075     usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
00076 
00077     /* Zero out bit field bits and then or value bits into them. */
00078     usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
00079 
00080     /* move bits back into storage */
00081     ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
00082     ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
00083 }
00084 
00085 UCHAR
00086 xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
00087 {
00088     USHORT          usWordBuf;
00089     USHORT          usMask;
00090     USHORT          usByteOffset;
00091     USHORT          usNPreBits;
00092 
00093     /* Calculate byte offset for first byte containing the bit values starting
00094      * at usBitOffset. */
00095     usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
00096 
00097     /* How many bits precede our bits to set. */
00098     usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
00099 
00100     /* Prepare a mask for setting the new bits. */
00101     usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
00102 
00103     /* copy bits into temporary storage. */
00104     usWordBuf = ucByteBuf[usByteOffset];
00105     usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
00106 
00107     /* throw away unneeded bits. */
00108     usWordBuf >>= usNPreBits;
00109 
00110     /* mask away bits above the requested bitfield. */
00111     usWordBuf &= usMask;
00112 
00113     return ( UCHAR ) usWordBuf;
00114 }
00115 
00116 eMBException
00117 prveMBError2Exception( eMBErrorCode eErrorCode )
00118 {
00119     eMBException    eStatus;
00120 
00121     switch ( eErrorCode )
00122     {
00123         case MB_ENOERR :
00124             eStatus = MB_EX_NONE;
00125             break;
00126 
00127         case MB_ENOREG :
00128             eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
00129             break;
00130 
00131         case MB_ETIMEDOUT :
00132             eStatus = MB_EX_SLAVE_BUSY;
00133             break;
00134 
00135         default:
00136             eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
00137             break;
00138     }
00139 
00140     return eStatus;
00141 }