Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbutils.cpp@0:7b5d37a81b6b, 2010-10-05 (annotated)
- Committer:
- tecnosys
- Date:
- Tue Oct 05 11:51:06 2010 +0000
- Revision:
- 0:7b5d37a81b6b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tecnosys | 0:7b5d37a81b6b | 1 | /* |
tecnosys | 0:7b5d37a81b6b | 2 | * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. |
tecnosys | 0:7b5d37a81b6b | 3 | * Copyright (c) 2006 Christian Walter <wolti@sil.at> |
tecnosys | 0:7b5d37a81b6b | 4 | * All rights reserved. |
tecnosys | 0:7b5d37a81b6b | 5 | * |
tecnosys | 0:7b5d37a81b6b | 6 | * Redistribution and use in source and binary forms, with or without |
tecnosys | 0:7b5d37a81b6b | 7 | * modification, are permitted provided that the following conditions |
tecnosys | 0:7b5d37a81b6b | 8 | * are met: |
tecnosys | 0:7b5d37a81b6b | 9 | * 1. Redistributions of source code must retain the above copyright |
tecnosys | 0:7b5d37a81b6b | 10 | * notice, this list of conditions and the following disclaimer. |
tecnosys | 0:7b5d37a81b6b | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
tecnosys | 0:7b5d37a81b6b | 12 | * notice, this list of conditions and the following disclaimer in the |
tecnosys | 0:7b5d37a81b6b | 13 | * documentation and/or other materials provided with the distribution. |
tecnosys | 0:7b5d37a81b6b | 14 | * 3. The name of the author may not be used to endorse or promote products |
tecnosys | 0:7b5d37a81b6b | 15 | * derived from this software without specific prior written permission. |
tecnosys | 0:7b5d37a81b6b | 16 | * |
tecnosys | 0:7b5d37a81b6b | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
tecnosys | 0:7b5d37a81b6b | 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
tecnosys | 0:7b5d37a81b6b | 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
tecnosys | 0:7b5d37a81b6b | 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
tecnosys | 0:7b5d37a81b6b | 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
tecnosys | 0:7b5d37a81b6b | 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
tecnosys | 0:7b5d37a81b6b | 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
tecnosys | 0:7b5d37a81b6b | 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
tecnosys | 0:7b5d37a81b6b | 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
tecnosys | 0:7b5d37a81b6b | 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
tecnosys | 0:7b5d37a81b6b | 27 | * |
tecnosys | 0:7b5d37a81b6b | 28 | * File: $Id: mbutils.c,v 1.6 2007/02/18 23:49:07 wolti Exp $ |
tecnosys | 0:7b5d37a81b6b | 29 | */ |
tecnosys | 0:7b5d37a81b6b | 30 | |
tecnosys | 0:7b5d37a81b6b | 31 | /* ----------------------- System includes ----------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 32 | #include "stdlib.h" |
tecnosys | 0:7b5d37a81b6b | 33 | #include "string.h" |
tecnosys | 0:7b5d37a81b6b | 34 | |
tecnosys | 0:7b5d37a81b6b | 35 | /* ----------------------- Platform includes --------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 36 | #include "port.h" |
tecnosys | 0:7b5d37a81b6b | 37 | |
tecnosys | 0:7b5d37a81b6b | 38 | /* ----------------------- Modbus includes ----------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 39 | #include "mb.h" |
tecnosys | 0:7b5d37a81b6b | 40 | #include "mbproto.h" |
tecnosys | 0:7b5d37a81b6b | 41 | |
tecnosys | 0:7b5d37a81b6b | 42 | /* ----------------------- Defines ------------------------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 43 | #define BITS_UCHAR 8U |
tecnosys | 0:7b5d37a81b6b | 44 | |
tecnosys | 0:7b5d37a81b6b | 45 | /* ----------------------- Start implementation -----------------------------*/ |
tecnosys | 0:7b5d37a81b6b | 46 | void |
tecnosys | 0:7b5d37a81b6b | 47 | xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits, |
tecnosys | 0:7b5d37a81b6b | 48 | UCHAR ucValue ) |
tecnosys | 0:7b5d37a81b6b | 49 | { |
tecnosys | 0:7b5d37a81b6b | 50 | USHORT usWordBuf; |
tecnosys | 0:7b5d37a81b6b | 51 | USHORT usMask; |
tecnosys | 0:7b5d37a81b6b | 52 | USHORT usByteOffset; |
tecnosys | 0:7b5d37a81b6b | 53 | USHORT usNPreBits; |
tecnosys | 0:7b5d37a81b6b | 54 | USHORT usValue = ucValue; |
tecnosys | 0:7b5d37a81b6b | 55 | |
tecnosys | 0:7b5d37a81b6b | 56 | assert( ucNBits <= 8 ); |
tecnosys | 0:7b5d37a81b6b | 57 | assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 ); |
tecnosys | 0:7b5d37a81b6b | 58 | |
tecnosys | 0:7b5d37a81b6b | 59 | /* Calculate byte offset for first byte containing the bit values starting |
tecnosys | 0:7b5d37a81b6b | 60 | * at usBitOffset. */ |
tecnosys | 0:7b5d37a81b6b | 61 | usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR ); |
tecnosys | 0:7b5d37a81b6b | 62 | |
tecnosys | 0:7b5d37a81b6b | 63 | /* How many bits precede our bits to set. */ |
tecnosys | 0:7b5d37a81b6b | 64 | usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR ); |
tecnosys | 0:7b5d37a81b6b | 65 | |
tecnosys | 0:7b5d37a81b6b | 66 | /* Move bit field into position over bits to set */ |
tecnosys | 0:7b5d37a81b6b | 67 | usValue <<= usNPreBits; |
tecnosys | 0:7b5d37a81b6b | 68 | |
tecnosys | 0:7b5d37a81b6b | 69 | /* Prepare a mask for setting the new bits. */ |
tecnosys | 0:7b5d37a81b6b | 70 | usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 ); |
tecnosys | 0:7b5d37a81b6b | 71 | usMask <<= usBitOffset - usByteOffset * BITS_UCHAR; |
tecnosys | 0:7b5d37a81b6b | 72 | |
tecnosys | 0:7b5d37a81b6b | 73 | /* copy bits into temporary storage. */ |
tecnosys | 0:7b5d37a81b6b | 74 | usWordBuf = ucByteBuf[usByteOffset]; |
tecnosys | 0:7b5d37a81b6b | 75 | usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR; |
tecnosys | 0:7b5d37a81b6b | 76 | |
tecnosys | 0:7b5d37a81b6b | 77 | /* Zero out bit field bits and then or value bits into them. */ |
tecnosys | 0:7b5d37a81b6b | 78 | usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue ); |
tecnosys | 0:7b5d37a81b6b | 79 | |
tecnosys | 0:7b5d37a81b6b | 80 | /* move bits back into storage */ |
tecnosys | 0:7b5d37a81b6b | 81 | ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF ); |
tecnosys | 0:7b5d37a81b6b | 82 | ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR ); |
tecnosys | 0:7b5d37a81b6b | 83 | } |
tecnosys | 0:7b5d37a81b6b | 84 | |
tecnosys | 0:7b5d37a81b6b | 85 | UCHAR |
tecnosys | 0:7b5d37a81b6b | 86 | xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits ) |
tecnosys | 0:7b5d37a81b6b | 87 | { |
tecnosys | 0:7b5d37a81b6b | 88 | USHORT usWordBuf; |
tecnosys | 0:7b5d37a81b6b | 89 | USHORT usMask; |
tecnosys | 0:7b5d37a81b6b | 90 | USHORT usByteOffset; |
tecnosys | 0:7b5d37a81b6b | 91 | USHORT usNPreBits; |
tecnosys | 0:7b5d37a81b6b | 92 | |
tecnosys | 0:7b5d37a81b6b | 93 | /* Calculate byte offset for first byte containing the bit values starting |
tecnosys | 0:7b5d37a81b6b | 94 | * at usBitOffset. */ |
tecnosys | 0:7b5d37a81b6b | 95 | usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR ); |
tecnosys | 0:7b5d37a81b6b | 96 | |
tecnosys | 0:7b5d37a81b6b | 97 | /* How many bits precede our bits to set. */ |
tecnosys | 0:7b5d37a81b6b | 98 | usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR ); |
tecnosys | 0:7b5d37a81b6b | 99 | |
tecnosys | 0:7b5d37a81b6b | 100 | /* Prepare a mask for setting the new bits. */ |
tecnosys | 0:7b5d37a81b6b | 101 | usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 ); |
tecnosys | 0:7b5d37a81b6b | 102 | |
tecnosys | 0:7b5d37a81b6b | 103 | /* copy bits into temporary storage. */ |
tecnosys | 0:7b5d37a81b6b | 104 | usWordBuf = ucByteBuf[usByteOffset]; |
tecnosys | 0:7b5d37a81b6b | 105 | usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR; |
tecnosys | 0:7b5d37a81b6b | 106 | |
tecnosys | 0:7b5d37a81b6b | 107 | /* throw away unneeded bits. */ |
tecnosys | 0:7b5d37a81b6b | 108 | usWordBuf >>= usNPreBits; |
tecnosys | 0:7b5d37a81b6b | 109 | |
tecnosys | 0:7b5d37a81b6b | 110 | /* mask away bits above the requested bitfield. */ |
tecnosys | 0:7b5d37a81b6b | 111 | usWordBuf &= usMask; |
tecnosys | 0:7b5d37a81b6b | 112 | |
tecnosys | 0:7b5d37a81b6b | 113 | return ( UCHAR ) usWordBuf; |
tecnosys | 0:7b5d37a81b6b | 114 | } |
tecnosys | 0:7b5d37a81b6b | 115 | |
tecnosys | 0:7b5d37a81b6b | 116 | eMBException |
tecnosys | 0:7b5d37a81b6b | 117 | prveMBError2Exception( eMBErrorCode eErrorCode ) |
tecnosys | 0:7b5d37a81b6b | 118 | { |
tecnosys | 0:7b5d37a81b6b | 119 | eMBException eStatus; |
tecnosys | 0:7b5d37a81b6b | 120 | |
tecnosys | 0:7b5d37a81b6b | 121 | switch ( eErrorCode ) |
tecnosys | 0:7b5d37a81b6b | 122 | { |
tecnosys | 0:7b5d37a81b6b | 123 | case MB_ENOERR: |
tecnosys | 0:7b5d37a81b6b | 124 | eStatus = MB_EX_NONE; |
tecnosys | 0:7b5d37a81b6b | 125 | break; |
tecnosys | 0:7b5d37a81b6b | 126 | |
tecnosys | 0:7b5d37a81b6b | 127 | case MB_ENOREG: |
tecnosys | 0:7b5d37a81b6b | 128 | eStatus = MB_EX_ILLEGAL_DATA_ADDRESS; |
tecnosys | 0:7b5d37a81b6b | 129 | break; |
tecnosys | 0:7b5d37a81b6b | 130 | |
tecnosys | 0:7b5d37a81b6b | 131 | case MB_ETIMEDOUT: |
tecnosys | 0:7b5d37a81b6b | 132 | eStatus = MB_EX_SLAVE_BUSY; |
tecnosys | 0:7b5d37a81b6b | 133 | break; |
tecnosys | 0:7b5d37a81b6b | 134 | |
tecnosys | 0:7b5d37a81b6b | 135 | default: |
tecnosys | 0:7b5d37a81b6b | 136 | eStatus = MB_EX_SLAVE_DEVICE_FAILURE; |
tecnosys | 0:7b5d37a81b6b | 137 | break; |
tecnosys | 0:7b5d37a81b6b | 138 | } |
tecnosys | 0:7b5d37a81b6b | 139 | |
tecnosys | 0:7b5d37a81b6b | 140 | return eStatus; |
tecnosys | 0:7b5d37a81b6b | 141 | } |