mbutils.h 4.1 KB

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