mbutils.h 4.0 KB

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