Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbutils.h@0:7b5d37a81b6b, 2010-10-05 (annotated)
- Committer:
- tecnosys
- Date:
- Tue Oct 05 11:51:06 2010 +0000
- Revision:
- 0:7b5d37a81b6b
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tecnosys | 0:7b5d37a81b6b | 1 | /* |
| tecnosys | 0:7b5d37a81b6b | 2 | * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. |
| tecnosys | 0:7b5d37a81b6b | 3 | * Copyright (c) 2006 Christian Walter <wolti@sil.at> |
| tecnosys | 0:7b5d37a81b6b | 4 | * All rights reserved. |
| tecnosys | 0:7b5d37a81b6b | 5 | * |
| tecnosys | 0:7b5d37a81b6b | 6 | * Redistribution and use in source and binary forms, with or without |
| tecnosys | 0:7b5d37a81b6b | 7 | * modification, are permitted provided that the following conditions |
| tecnosys | 0:7b5d37a81b6b | 8 | * are met: |
| tecnosys | 0:7b5d37a81b6b | 9 | * 1. Redistributions of source code must retain the above copyright |
| tecnosys | 0:7b5d37a81b6b | 10 | * notice, this list of conditions and the following disclaimer. |
| tecnosys | 0:7b5d37a81b6b | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| tecnosys | 0:7b5d37a81b6b | 12 | * notice, this list of conditions and the following disclaimer in the |
| tecnosys | 0:7b5d37a81b6b | 13 | * documentation and/or other materials provided with the distribution. |
| tecnosys | 0:7b5d37a81b6b | 14 | * 3. The name of the author may not be used to endorse or promote products |
| tecnosys | 0:7b5d37a81b6b | 15 | * derived from this software without specific prior written permission. |
| tecnosys | 0:7b5d37a81b6b | 16 | * |
| tecnosys | 0:7b5d37a81b6b | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| tecnosys | 0:7b5d37a81b6b | 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| tecnosys | 0:7b5d37a81b6b | 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| tecnosys | 0:7b5d37a81b6b | 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| tecnosys | 0:7b5d37a81b6b | 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| tecnosys | 0:7b5d37a81b6b | 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| tecnosys | 0:7b5d37a81b6b | 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| tecnosys | 0:7b5d37a81b6b | 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| tecnosys | 0:7b5d37a81b6b | 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| tecnosys | 0:7b5d37a81b6b | 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| tecnosys | 0:7b5d37a81b6b | 27 | * |
| tecnosys | 0:7b5d37a81b6b | 28 | * File: $Id: mbutils.h,v 1.5 2006/12/07 22:10:34 wolti Exp $ |
| tecnosys | 0:7b5d37a81b6b | 29 | */ |
| tecnosys | 0:7b5d37a81b6b | 30 | |
| tecnosys | 0:7b5d37a81b6b | 31 | #ifndef _MB_UTILS_H |
| tecnosys | 0:7b5d37a81b6b | 32 | #define _MB_UTILS_H |
| tecnosys | 0:7b5d37a81b6b | 33 | |
| tecnosys | 0:7b5d37a81b6b | 34 | #ifdef __cplusplus |
| tecnosys | 0:7b5d37a81b6b | 35 | PR_BEGIN_EXTERN_C |
| tecnosys | 0:7b5d37a81b6b | 36 | #endif |
| tecnosys | 0:7b5d37a81b6b | 37 | /*! \defgroup modbus_utils Utilities |
| tecnosys | 0:7b5d37a81b6b | 38 | * |
| tecnosys | 0:7b5d37a81b6b | 39 | * This module contains some utility functions which can be used by |
| tecnosys | 0:7b5d37a81b6b | 40 | * the application. It includes some special functions for working with |
| tecnosys | 0:7b5d37a81b6b | 41 | * bitfields backed by a character array buffer. |
| tecnosys | 0:7b5d37a81b6b | 42 | * |
| tecnosys | 0:7b5d37a81b6b | 43 | */ |
| tecnosys | 0:7b5d37a81b6b | 44 | /*! \addtogroup modbus_utils |
| tecnosys | 0:7b5d37a81b6b | 45 | * @{ |
| tecnosys | 0:7b5d37a81b6b | 46 | */ |
| tecnosys | 0:7b5d37a81b6b | 47 | /*! \brief Function to set bits in a byte buffer. |
| tecnosys | 0:7b5d37a81b6b | 48 | * |
| tecnosys | 0:7b5d37a81b6b | 49 | * This function allows the efficient use of an array to implement bitfields. |
| tecnosys | 0:7b5d37a81b6b | 50 | * The array used for storing the bits must always be a multiple of two |
| tecnosys | 0:7b5d37a81b6b | 51 | * bytes. Up to eight bits can be set or cleared in one operation. |
| tecnosys | 0:7b5d37a81b6b | 52 | * |
| tecnosys | 0:7b5d37a81b6b | 53 | * \param ucByteBuf A buffer where the bit values are stored. Must be a |
| tecnosys | 0:7b5d37a81b6b | 54 | * multiple of 2 bytes. No length checking is performed and if |
| tecnosys | 0:7b5d37a81b6b | 55 | * usBitOffset / 8 is greater than the size of the buffer memory contents |
| tecnosys | 0:7b5d37a81b6b | 56 | * is overwritten. |
| tecnosys | 0:7b5d37a81b6b | 57 | * \param usBitOffset The starting address of the bits to set. The first |
| tecnosys | 0:7b5d37a81b6b | 58 | * bit has the offset 0. |
| tecnosys | 0:7b5d37a81b6b | 59 | * \param ucNBits Number of bits to modify. The value must always be smaller |
| tecnosys | 0:7b5d37a81b6b | 60 | * than 8. |
| tecnosys | 0:7b5d37a81b6b | 61 | * \param ucValues Thew new values for the bits. The value for the first bit |
| tecnosys | 0:7b5d37a81b6b | 62 | * starting at <code>usBitOffset</code> is the LSB of the value |
| tecnosys | 0:7b5d37a81b6b | 63 | * <code>ucValues</code> |
| tecnosys | 0:7b5d37a81b6b | 64 | * |
| tecnosys | 0:7b5d37a81b6b | 65 | * \code |
| tecnosys | 0:7b5d37a81b6b | 66 | * ucBits[2] = {0, 0}; |
| tecnosys | 0:7b5d37a81b6b | 67 | * |
| tecnosys | 0:7b5d37a81b6b | 68 | * // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1) |
| tecnosys | 0:7b5d37a81b6b | 69 | * xMBUtilSetBits( ucBits, 4, 1, 1 ); |
| tecnosys | 0:7b5d37a81b6b | 70 | * |
| tecnosys | 0:7b5d37a81b6b | 71 | * // Set bit 7 to 1 and bit 8 to 0. |
| tecnosys | 0:7b5d37a81b6b | 72 | * xMBUtilSetBits( ucBits, 7, 2, 0x01 ); |
| tecnosys | 0:7b5d37a81b6b | 73 | * |
| tecnosys | 0:7b5d37a81b6b | 74 | * // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A; |
| tecnosys | 0:7b5d37a81b6b | 75 | * xMBUtilSetBits( ucBits, 8, 8, 0x5A); |
| tecnosys | 0:7b5d37a81b6b | 76 | * \endcode |
| tecnosys | 0:7b5d37a81b6b | 77 | */ |
| tecnosys | 0:7b5d37a81b6b | 78 | void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, |
| tecnosys | 0:7b5d37a81b6b | 79 | UCHAR ucNBits, UCHAR ucValues ); |
| tecnosys | 0:7b5d37a81b6b | 80 | |
| tecnosys | 0:7b5d37a81b6b | 81 | /*! \brief Function to read bits in a byte buffer. |
| tecnosys | 0:7b5d37a81b6b | 82 | * |
| tecnosys | 0:7b5d37a81b6b | 83 | * This function is used to extract up bit values from an array. Up to eight |
| tecnosys | 0:7b5d37a81b6b | 84 | * bit values can be extracted in one step. |
| tecnosys | 0:7b5d37a81b6b | 85 | * |
| tecnosys | 0:7b5d37a81b6b | 86 | * \param ucByteBuf A buffer where the bit values are stored. |
| tecnosys | 0:7b5d37a81b6b | 87 | * \param usBitOffset The starting address of the bits to set. The first |
| tecnosys | 0:7b5d37a81b6b | 88 | * bit has the offset 0. |
| tecnosys | 0:7b5d37a81b6b | 89 | * \param ucNBits Number of bits to modify. The value must always be smaller |
| tecnosys | 0:7b5d37a81b6b | 90 | * than 8. |
| tecnosys | 0:7b5d37a81b6b | 91 | * |
| tecnosys | 0:7b5d37a81b6b | 92 | * \code |
| tecnosys | 0:7b5d37a81b6b | 93 | * UCHAR ucBits[2] = {0, 0}; |
| tecnosys | 0:7b5d37a81b6b | 94 | * UCHAR ucResult; |
| tecnosys | 0:7b5d37a81b6b | 95 | * |
| tecnosys | 0:7b5d37a81b6b | 96 | * // Extract the bits 3 - 10. |
| tecnosys | 0:7b5d37a81b6b | 97 | * ucResult = xMBUtilGetBits( ucBits, 3, 8 ); |
| tecnosys | 0:7b5d37a81b6b | 98 | * \endcode |
| tecnosys | 0:7b5d37a81b6b | 99 | */ |
| tecnosys | 0:7b5d37a81b6b | 100 | UCHAR xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, |
| tecnosys | 0:7b5d37a81b6b | 101 | UCHAR ucNBits ); |
| tecnosys | 0:7b5d37a81b6b | 102 | |
| tecnosys | 0:7b5d37a81b6b | 103 | /*! @} */ |
| tecnosys | 0:7b5d37a81b6b | 104 | |
| tecnosys | 0:7b5d37a81b6b | 105 | #ifdef __cplusplus |
| tecnosys | 0:7b5d37a81b6b | 106 | PR_END_EXTERN_C |
| tecnosys | 0:7b5d37a81b6b | 107 | #endif |
| tecnosys | 0:7b5d37a81b6b | 108 | #endif |