mbutils.c 4.9 KB

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