Tom Martins
/
Probleme_implementation_lwip2
test public
Fork of Probleme_implementation_lwip by
main.cpp@0:0453a0a7e500, 2010-04-15 (annotated)
- Committer:
- cam
- Date:
- Thu Apr 15 12:10:34 2010 +0000
- Revision:
- 0:0453a0a7e500
- Child:
- 1:a3ee8cb24540
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cam | 0:0453a0a7e500 | 1 | /* |
cam | 0:0453a0a7e500 | 2 | * FreeModbus Libary: BARE Demo Application |
cam | 0:0453a0a7e500 | 3 | * Copyright (C) 2006 Christian Walter <wolti@sil.at> |
cam | 0:0453a0a7e500 | 4 | * |
cam | 0:0453a0a7e500 | 5 | * This program is free software; you can redistribute it and/or modify |
cam | 0:0453a0a7e500 | 6 | * it under the terms of the GNU General Public License as published by |
cam | 0:0453a0a7e500 | 7 | * the Free Software Foundation; either version 2 of the License, or |
cam | 0:0453a0a7e500 | 8 | * (at your option) any later version. |
cam | 0:0453a0a7e500 | 9 | * |
cam | 0:0453a0a7e500 | 10 | * This program is distributed in the hope that it will be useful, |
cam | 0:0453a0a7e500 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
cam | 0:0453a0a7e500 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cam | 0:0453a0a7e500 | 13 | * GNU General Public License for more details. |
cam | 0:0453a0a7e500 | 14 | * |
cam | 0:0453a0a7e500 | 15 | * You should have received a copy of the GNU General Public License |
cam | 0:0453a0a7e500 | 16 | * along with this program; if not, write to the Free Software |
cam | 0:0453a0a7e500 | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
cam | 0:0453a0a7e500 | 18 | * |
cam | 0:0453a0a7e500 | 19 | * File: $Id: demo.c,v 1.1 2006/08/22 21:35:13 wolti Exp $ |
cam | 0:0453a0a7e500 | 20 | */ |
cam | 0:0453a0a7e500 | 21 | |
cam | 0:0453a0a7e500 | 22 | /* ----------------------- System includes --------------------------------*/ |
cam | 0:0453a0a7e500 | 23 | |
cam | 0:0453a0a7e500 | 24 | /* ----------------------- Modbus includes ----------------------------------*/ |
cam | 0:0453a0a7e500 | 25 | #include "mb.h" |
cam | 0:0453a0a7e500 | 26 | #include "mbport.h" |
cam | 0:0453a0a7e500 | 27 | |
cam | 0:0453a0a7e500 | 28 | /* ----------------------- Defines ------------------------------------------*/ |
cam | 0:0453a0a7e500 | 29 | #define REG_INPUT_START 1000 |
cam | 0:0453a0a7e500 | 30 | #define REG_INPUT_NREGS 4 |
cam | 0:0453a0a7e500 | 31 | #define SLAVE_ID 0x0A |
cam | 0:0453a0a7e500 | 32 | |
cam | 0:0453a0a7e500 | 33 | /* ----------------------- Static variables ---------------------------------*/ |
cam | 0:0453a0a7e500 | 34 | static USHORT usRegInputStart = REG_INPUT_START; |
cam | 0:0453a0a7e500 | 35 | static USHORT usRegInputBuf[REG_INPUT_NREGS]; |
cam | 0:0453a0a7e500 | 36 | |
cam | 0:0453a0a7e500 | 37 | /* ----------------------- Start implementation -----------------------------*/ |
cam | 0:0453a0a7e500 | 38 | int |
cam | 0:0453a0a7e500 | 39 | main( void ) |
cam | 0:0453a0a7e500 | 40 | { |
cam | 0:0453a0a7e500 | 41 | eMBErrorCode eStatus; |
cam | 0:0453a0a7e500 | 42 | |
cam | 0:0453a0a7e500 | 43 | eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE ); |
cam | 0:0453a0a7e500 | 44 | |
cam | 0:0453a0a7e500 | 45 | /* Enable the Modbus Protocol Stack. */ |
cam | 0:0453a0a7e500 | 46 | eStatus = eMBEnable( ); |
cam | 0:0453a0a7e500 | 47 | |
cam | 0:0453a0a7e500 | 48 | // Initialise some registers |
cam | 0:0453a0a7e500 | 49 | usRegInputBuf[1] = 0x1234; |
cam | 0:0453a0a7e500 | 50 | usRegInputBuf[2] = 0x5678; |
cam | 0:0453a0a7e500 | 51 | usRegInputBuf[3] = 0x9abc; |
cam | 0:0453a0a7e500 | 52 | |
cam | 0:0453a0a7e500 | 53 | for( ;; ) |
cam | 0:0453a0a7e500 | 54 | { |
cam | 0:0453a0a7e500 | 55 | ( void )eMBPoll( ); |
cam | 0:0453a0a7e500 | 56 | |
cam | 0:0453a0a7e500 | 57 | /* Here we simply count the number of poll cycles. */ |
cam | 0:0453a0a7e500 | 58 | usRegInputBuf[0]++; |
cam | 0:0453a0a7e500 | 59 | } |
cam | 0:0453a0a7e500 | 60 | } |
cam | 0:0453a0a7e500 | 61 | |
cam | 0:0453a0a7e500 | 62 | eMBErrorCode |
cam | 0:0453a0a7e500 | 63 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
cam | 0:0453a0a7e500 | 64 | { |
cam | 0:0453a0a7e500 | 65 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 66 | int iRegIndex; |
cam | 0:0453a0a7e500 | 67 | |
cam | 0:0453a0a7e500 | 68 | if( ( usAddress >= REG_INPUT_START ) |
cam | 0:0453a0a7e500 | 69 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
cam | 0:0453a0a7e500 | 70 | { |
cam | 0:0453a0a7e500 | 71 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
cam | 0:0453a0a7e500 | 72 | while( usNRegs > 0 ) |
cam | 0:0453a0a7e500 | 73 | { |
cam | 0:0453a0a7e500 | 74 | *pucRegBuffer++ = |
cam | 0:0453a0a7e500 | 75 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
cam | 0:0453a0a7e500 | 76 | *pucRegBuffer++ = |
cam | 0:0453a0a7e500 | 77 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
cam | 0:0453a0a7e500 | 78 | iRegIndex++; |
cam | 0:0453a0a7e500 | 79 | usNRegs--; |
cam | 0:0453a0a7e500 | 80 | } |
cam | 0:0453a0a7e500 | 81 | } |
cam | 0:0453a0a7e500 | 82 | else |
cam | 0:0453a0a7e500 | 83 | { |
cam | 0:0453a0a7e500 | 84 | eStatus = MB_ENOREG; |
cam | 0:0453a0a7e500 | 85 | } |
cam | 0:0453a0a7e500 | 86 | |
cam | 0:0453a0a7e500 | 87 | return eStatus; |
cam | 0:0453a0a7e500 | 88 | } |
cam | 0:0453a0a7e500 | 89 | |
cam | 0:0453a0a7e500 | 90 | eMBErrorCode |
cam | 0:0453a0a7e500 | 91 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) |
cam | 0:0453a0a7e500 | 92 | { |
cam | 0:0453a0a7e500 | 93 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 94 | int iRegIndex; |
cam | 0:0453a0a7e500 | 95 | |
cam | 0:0453a0a7e500 | 96 | if (eMode == MB_REG_READ) |
cam | 0:0453a0a7e500 | 97 | { |
cam | 0:0453a0a7e500 | 98 | if( ( usAddress >= REG_INPUT_START ) |
cam | 0:0453a0a7e500 | 99 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
cam | 0:0453a0a7e500 | 100 | { |
cam | 0:0453a0a7e500 | 101 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
cam | 0:0453a0a7e500 | 102 | while( usNRegs > 0 ) |
cam | 0:0453a0a7e500 | 103 | { |
cam | 0:0453a0a7e500 | 104 | *pucRegBuffer++ = |
cam | 0:0453a0a7e500 | 105 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
cam | 0:0453a0a7e500 | 106 | *pucRegBuffer++ = |
cam | 0:0453a0a7e500 | 107 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
cam | 0:0453a0a7e500 | 108 | iRegIndex++; |
cam | 0:0453a0a7e500 | 109 | usNRegs--; |
cam | 0:0453a0a7e500 | 110 | } |
cam | 0:0453a0a7e500 | 111 | } |
cam | 0:0453a0a7e500 | 112 | } |
cam | 0:0453a0a7e500 | 113 | |
cam | 0:0453a0a7e500 | 114 | if (eMode == MB_REG_WRITE) |
cam | 0:0453a0a7e500 | 115 | { |
cam | 0:0453a0a7e500 | 116 | if( ( usAddress >= REG_INPUT_START ) |
cam | 0:0453a0a7e500 | 117 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
cam | 0:0453a0a7e500 | 118 | { |
cam | 0:0453a0a7e500 | 119 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
cam | 0:0453a0a7e500 | 120 | while( usNRegs > 0 ) |
cam | 0:0453a0a7e500 | 121 | { |
cam | 0:0453a0a7e500 | 122 | usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); |
cam | 0:0453a0a7e500 | 123 | pucRegBuffer+=2; |
cam | 0:0453a0a7e500 | 124 | iRegIndex++; |
cam | 0:0453a0a7e500 | 125 | usNRegs--; |
cam | 0:0453a0a7e500 | 126 | } |
cam | 0:0453a0a7e500 | 127 | } |
cam | 0:0453a0a7e500 | 128 | } |
cam | 0:0453a0a7e500 | 129 | |
cam | 0:0453a0a7e500 | 130 | return eStatus; |
cam | 0:0453a0a7e500 | 131 | } |
cam | 0:0453a0a7e500 | 132 | |
cam | 0:0453a0a7e500 | 133 | |
cam | 0:0453a0a7e500 | 134 | eMBErrorCode |
cam | 0:0453a0a7e500 | 135 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, |
cam | 0:0453a0a7e500 | 136 | eMBRegisterMode eMode ) |
cam | 0:0453a0a7e500 | 137 | { |
cam | 0:0453a0a7e500 | 138 | return MB_ENOREG; |
cam | 0:0453a0a7e500 | 139 | } |
cam | 0:0453a0a7e500 | 140 | |
cam | 0:0453a0a7e500 | 141 | eMBErrorCode |
cam | 0:0453a0a7e500 | 142 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
cam | 0:0453a0a7e500 | 143 | { |
cam | 0:0453a0a7e500 | 144 | return MB_ENOREG; |
cam | 0:0453a0a7e500 | 145 | } |
cam | 0:0453a0a7e500 | 146 |