mbutils.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /* ----------------------- System includes ----------------------------------*/
  30. #include "stdlib.h"
  31. #include "string.h"
  32. /* ----------------------- Platform includes --------------------------------*/
  33. #include "port.h"
  34. /* ----------------------- Modbus includes ----------------------------------*/
  35. #include "mb.h"
  36. #include "mbproto.h"
  37. /* ----------------------- Defines ------------------------------------------*/
  38. #define BITS_UCHAR 8U
  39. /* ----------------------- Start implementation -----------------------------*/
  40. void
  41. xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
  42. UCHAR ucValue )
  43. {
  44. USHORT usWordBuf;
  45. USHORT usMask;
  46. USHORT usByteOffset;
  47. USHORT usNPreBits;
  48. USHORT usValue = ucValue;
  49. assert( ucNBits <= 8 );
  50. assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
  51. /* Calculate byte offset for first byte containing the bit values starting
  52. * at usBitOffset. */
  53. usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
  54. /* How many bits precede our bits to set. */
  55. usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
  56. /* Move bit field into position over bits to set */
  57. usValue <<= usNPreBits;
  58. /* Prepare a mask for setting the new bits. */
  59. usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
  60. usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
  61. /* copy bits into temporary storage. */
  62. usWordBuf = ucByteBuf[usByteOffset];
  63. usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
  64. /* Zero out bit field bits and then or value bits into them. */
  65. usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
  66. /* move bits back into storage */
  67. ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
  68. ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
  69. }
  70. UCHAR
  71. xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
  72. {
  73. USHORT usWordBuf;
  74. USHORT usMask;
  75. USHORT usByteOffset;
  76. USHORT usNPreBits;
  77. /* Calculate byte offset for first byte containing the bit values starting
  78. * at usBitOffset. */
  79. usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
  80. /* How many bits precede our bits to set. */
  81. usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
  82. /* Prepare a mask for setting the new bits. */
  83. usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
  84. /* copy bits into temporary storage. */
  85. usWordBuf = ucByteBuf[usByteOffset];
  86. usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
  87. /* throw away unneeded bits. */
  88. usWordBuf >>= usNPreBits;
  89. /* mask away bits above the requested bitfield. */
  90. usWordBuf &= usMask;
  91. return ( UCHAR ) usWordBuf;
  92. }
  93. eMBException
  94. prveMBError2Exception( eMBErrorCode eErrorCode )
  95. {
  96. eMBException eStatus;
  97. switch ( eErrorCode )
  98. {
  99. case MB_ENOERR:
  100. eStatus = MB_EX_NONE;
  101. break;
  102. case MB_ENOREG:
  103. eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
  104. break;
  105. case MB_ETIMEDOUT:
  106. eStatus = MB_EX_SLAVE_BUSY;
  107. break;
  108. default:
  109. eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
  110. break;
  111. }
  112. return eStatus;
  113. }