mbutils.c 4.9 KB

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