Project

General

Profile

Statistics
| Branch: | Revision:

root / toolbox / freemodbus / modbus / functions / mbutils.c @ ae250f1a

History | View | Annotate | Download (4.98 KB)

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

    
31
/* ----------------------- System includes ----------------------------------*/
32
#include "stdlib.h"
33
#include "string.h"
34

    
35
/* ----------------------- Platform includes --------------------------------*/
36
#include "port.h"
37

    
38
/* ----------------------- Modbus includes ----------------------------------*/
39
#include "mb.h"
40
#include "mbproto.h"
41

    
42
/* ----------------------- Defines ------------------------------------------*/
43
#define BITS_UCHAR      8U
44

    
45
/* ----------------------- Start implementation -----------------------------*/
46
void
47
xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
48
                UCHAR ucValue )
49
{
50
    USHORT          usWordBuf;
51
    USHORT          usMask;
52
    USHORT          usByteOffset;
53
    USHORT          usNPreBits;
54
    USHORT          usValue = ucValue;
55

    
56
    assert( ucNBits <= 8 );
57
    assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
58

    
59
    /* Calculate byte offset for first byte containing the bit values starting
60
     * at usBitOffset. */
61
    usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
62

    
63
    /* How many bits precede our bits to set. */
64
    usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
65

    
66
    /* Move bit field into position over bits to set */
67
    usValue <<= usNPreBits;
68

    
69
    /* Prepare a mask for setting the new bits. */
70
    usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
71
    usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
72

    
73
    /* copy bits into temporary storage. */
74
    usWordBuf = ucByteBuf[usByteOffset];
75
    usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
76

    
77
    /* Zero out bit field bits and then or value bits into them. */
78
    usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
79

    
80
    /* move bits back into storage */
81
    ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
82
    ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
83
}
84

    
85
UCHAR
86
xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
87
{
88
    USHORT          usWordBuf;
89
    USHORT          usMask;
90
    USHORT          usByteOffset;
91
    USHORT          usNPreBits;
92

    
93
    /* Calculate byte offset for first byte containing the bit values starting
94
     * at usBitOffset. */
95
    usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
96

    
97
    /* How many bits precede our bits to set. */
98
    usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
99

    
100
    /* Prepare a mask for setting the new bits. */
101
    usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
102

    
103
    /* copy bits into temporary storage. */
104
    usWordBuf = ucByteBuf[usByteOffset];
105
    usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
106

    
107
    /* throw away unneeded bits. */
108
    usWordBuf >>= usNPreBits;
109

    
110
    /* mask away bits above the requested bitfield. */
111
    usWordBuf &= usMask;
112

    
113
    return ( UCHAR ) usWordBuf;
114
}
115

    
116
eMBException
117
prveMBError2Exception( eMBErrorCode eErrorCode )
118
{
119
    eMBException    eStatus;
120

    
121
    switch ( eErrorCode )
122
    {
123
        case MB_ENOERR:
124
            eStatus = MB_EX_NONE;
125
            break;
126

    
127
        case MB_ENOREG:
128
            eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
129
            break;
130

    
131
        case MB_ETIMEDOUT:
132
            eStatus = MB_EX_SLAVE_BUSY;
133
            break;
134

    
135
        default:
136
            eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
137
            break;
138
    }
139

    
140
    return eStatus;
141
}