mbutils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.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. */
  29. #ifndef _MB_UTILS_H
  30. #define _MB_UTILS_H
  31. #ifdef __cplusplus
  32. PR_BEGIN_EXTERN_C
  33. #endif
  34. /*! \defgroup modbus_utils Utilities
  35. *
  36. * This module contains some utility functions which can be used by
  37. * the application. It includes some special functions for working with
  38. * bitfields backed by a character array buffer.
  39. *
  40. */
  41. /*! \addtogroup modbus_utils
  42. * @{
  43. */
  44. /*! \brief Function to set bits in a byte buffer.
  45. *
  46. * This function allows the efficient use of an array to implement bitfields.
  47. * The array used for storing the bits must always be a multiple of two
  48. * bytes. Up to eight bits can be set or cleared in one operation.
  49. *
  50. * \param ucByteBuf A buffer where the bit values are stored. Must be a
  51. * multiple of 2 bytes. No length checking is performed and if
  52. * usBitOffset / 8 is greater than the size of the buffer memory contents
  53. * is overwritten.
  54. * \param usBitOffset The starting address of the bits to set. The first
  55. * bit has the offset 0.
  56. * \param ucNBits Number of bits to modify. The value must always be smaller
  57. * than 8.
  58. * \param ucValues Thew new values for the bits. The value for the first bit
  59. * starting at <code>usBitOffset</code> is the LSB of the value
  60. * <code>ucValues</code>
  61. *
  62. * \code
  63. * ucBits[2] = {0, 0};
  64. *
  65. * // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
  66. * xMBUtilSetBits( ucBits, 4, 1, 1 );
  67. *
  68. * // Set bit 7 to 1 and bit 8 to 0.
  69. * xMBUtilSetBits( ucBits, 7, 2, 0x01 );
  70. *
  71. * // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
  72. * xMBUtilSetBits( ucBits, 8, 8, 0x5A);
  73. * \endcode
  74. */
  75. void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
  76. UCHAR ucNBits, UCHAR ucValues );
  77. /*! \brief Function to read bits in a byte buffer.
  78. *
  79. * This function is used to extract up bit values from an array. Up to eight
  80. * bit values can be extracted in one step.
  81. *
  82. * \param ucByteBuf A buffer where the bit values are stored.
  83. * \param usBitOffset The starting address of the bits to set. The first
  84. * bit has the offset 0.
  85. * \param ucNBits Number of bits to modify. The value must always be smaller
  86. * than 8.
  87. *
  88. * \code
  89. * UCHAR ucBits[2] = {0, 0};
  90. * UCHAR ucResult;
  91. *
  92. * // Extract the bits 3 - 10.
  93. * ucResult = xMBUtilGetBits( ucBits, 3, 8 );
  94. * \endcode
  95. */
  96. UCHAR xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
  97. UCHAR ucNBits );
  98. /*! @} */
  99. #ifdef __cplusplus
  100. PR_END_EXTERN_C
  101. #endif
  102. #endif