Tom Martins
/
Probleme_implementation_lwip2
test public
Fork of Probleme_implementation_lwip by
main.cpp@2:9e76d51d9fb6, 2018-07-02 (annotated)
- Committer:
- TomTom83
- Date:
- Mon Jul 02 15:18:18 2018 +0000
- Revision:
- 2:9e76d51d9fb6
- Parent:
- 1:a3ee8cb24540
probl?mes d'adaptation
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 ----------------------------------*/ |
TomTom83 | 1:a3ee8cb24540 | 25 | #include "mbed.h" |
TomTom83 | 1:a3ee8cb24540 | 26 | #include "EthernetInterface.h" |
cam | 0:0453a0a7e500 | 27 | #include "mb.h" |
cam | 0:0453a0a7e500 | 28 | #include "mbport.h" |
TomTom83 | 1:a3ee8cb24540 | 29 | #include "netconf.h" |
cam | 0:0453a0a7e500 | 30 | |
cam | 0:0453a0a7e500 | 31 | /* ----------------------- Defines ------------------------------------------*/ |
cam | 0:0453a0a7e500 | 32 | #define REG_INPUT_START 1000 |
TomTom83 | 1:a3ee8cb24540 | 33 | #define REG_INPUT_NREGS 5 |
cam | 0:0453a0a7e500 | 34 | #define SLAVE_ID 0x0A |
cam | 0:0453a0a7e500 | 35 | |
cam | 0:0453a0a7e500 | 36 | /* ----------------------- Static variables ---------------------------------*/ |
cam | 0:0453a0a7e500 | 37 | static USHORT usRegInputStart = REG_INPUT_START; |
cam | 0:0453a0a7e500 | 38 | static USHORT usRegInputBuf[REG_INPUT_NREGS]; |
cam | 0:0453a0a7e500 | 39 | |
cam | 0:0453a0a7e500 | 40 | /* ----------------------- Start implementation -----------------------------*/ |
TomTom83 | 1:a3ee8cb24540 | 41 | |
TomTom83 | 1:a3ee8cb24540 | 42 | DigitalIn mybutton(USER_BUTTON); |
TomTom83 | 1:a3ee8cb24540 | 43 | DigitalOut myled2(LED2); |
TomTom83 | 1:a3ee8cb24540 | 44 | |
cam | 0:0453a0a7e500 | 45 | int |
cam | 0:0453a0a7e500 | 46 | main( void ) |
cam | 0:0453a0a7e500 | 47 | { |
cam | 0:0453a0a7e500 | 48 | eMBErrorCode eStatus; |
cam | 0:0453a0a7e500 | 49 | |
TomTom83 | 1:a3ee8cb24540 | 50 | //eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE ); |
TomTom83 | 1:a3ee8cb24540 | 51 | |
TomTom83 | 1:a3ee8cb24540 | 52 | EthernetInterface eth; |
TomTom83 | 1:a3ee8cb24540 | 53 | eth.set_network("169.254.178.1","255.255.0.0","169.254.178.3"); |
TomTom83 | 1:a3ee8cb24540 | 54 | eth.connect(); |
TomTom83 | 1:a3ee8cb24540 | 55 | |
TomTom83 | 1:a3ee8cb24540 | 56 | /* inicializacion del Stack de Modbus */ |
TomTom83 | 1:a3ee8cb24540 | 57 | eMBTCPInit(502); |
TomTom83 | 1:a3ee8cb24540 | 58 | |
cam | 0:0453a0a7e500 | 59 | /* Enable the Modbus Protocol Stack. */ |
cam | 0:0453a0a7e500 | 60 | eStatus = eMBEnable( ); |
TomTom83 | 1:a3ee8cb24540 | 61 | myled2=1; |
cam | 0:0453a0a7e500 | 62 | |
cam | 0:0453a0a7e500 | 63 | // Initialise some registers |
TomTom83 | 1:a3ee8cb24540 | 64 | usRegInputBuf[1] = 83; |
TomTom83 | 1:a3ee8cb24540 | 65 | usRegInputBuf[2] = 65; |
TomTom83 | 1:a3ee8cb24540 | 66 | usRegInputBuf[3] = 76; |
TomTom83 | 1:a3ee8cb24540 | 67 | usRegInputBuf[4] = 85; |
cam | 0:0453a0a7e500 | 68 | |
cam | 0:0453a0a7e500 | 69 | for( ;; ) |
cam | 0:0453a0a7e500 | 70 | { |
cam | 0:0453a0a7e500 | 71 | ( void )eMBPoll( ); |
TomTom83 | 1:a3ee8cb24540 | 72 | |
TomTom83 | 2:9e76d51d9fb6 | 73 | if (ETH_CheckFrameReceived()) //Fonctions que j'essaye d'utiliser |
TomTom83 | 2:9e76d51d9fb6 | 74 | { |
TomTom83 | 2:9e76d51d9fb6 | 75 | /* process received ethernet packet */ |
TomTom83 | 2:9e76d51d9fb6 | 76 | LwIP_Pkt_Handle(); //Traite le packet ethernet reçu |
TomTom83 | 2:9e76d51d9fb6 | 77 | } |
TomTom83 | 2:9e76d51d9fb6 | 78 | |
cam | 0:0453a0a7e500 | 79 | |
cam | 0:0453a0a7e500 | 80 | /* Here we simply count the number of poll cycles. */ |
cam | 0:0453a0a7e500 | 81 | usRegInputBuf[0]++; |
cam | 0:0453a0a7e500 | 82 | } |
cam | 0:0453a0a7e500 | 83 | } |
cam | 0:0453a0a7e500 | 84 | |
cam | 0:0453a0a7e500 | 85 | eMBErrorCode |
cam | 0:0453a0a7e500 | 86 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
cam | 0:0453a0a7e500 | 87 | { |
cam | 0:0453a0a7e500 | 88 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 89 | int iRegIndex; |
cam | 0:0453a0a7e500 | 90 | |
cam | 0:0453a0a7e500 | 91 | if( ( usAddress >= REG_INPUT_START ) |
cam | 0:0453a0a7e500 | 92 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
cam | 0:0453a0a7e500 | 93 | { |
cam | 0:0453a0a7e500 | 94 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
cam | 0:0453a0a7e500 | 95 | while( usNRegs > 0 ) |
cam | 0:0453a0a7e500 | 96 | { |
cam | 0:0453a0a7e500 | 97 | *pucRegBuffer++ = |
cam | 0:0453a0a7e500 | 98 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
cam | 0:0453a0a7e500 | 99 | *pucRegBuffer++ = |
cam | 0:0453a0a7e500 | 100 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
cam | 0:0453a0a7e500 | 101 | iRegIndex++; |
cam | 0:0453a0a7e500 | 102 | usNRegs--; |
cam | 0:0453a0a7e500 | 103 | } |
cam | 0:0453a0a7e500 | 104 | } |
cam | 0:0453a0a7e500 | 105 | else |
cam | 0:0453a0a7e500 | 106 | { |
cam | 0:0453a0a7e500 | 107 | eStatus = MB_ENOREG; |
cam | 0:0453a0a7e500 | 108 | } |
cam | 0:0453a0a7e500 | 109 | |
cam | 0:0453a0a7e500 | 110 | return eStatus; |
cam | 0:0453a0a7e500 | 111 | } |
cam | 0:0453a0a7e500 | 112 | |
cam | 0:0453a0a7e500 | 113 | eMBErrorCode |
cam | 0:0453a0a7e500 | 114 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) |
cam | 0:0453a0a7e500 | 115 | { |
cam | 0:0453a0a7e500 | 116 | eMBErrorCode eStatus = MB_ENOERR; |
cam | 0:0453a0a7e500 | 117 | int iRegIndex; |
cam | 0:0453a0a7e500 | 118 | |
cam | 0:0453a0a7e500 | 119 | if (eMode == MB_REG_READ) |
cam | 0:0453a0a7e500 | 120 | { |
cam | 0:0453a0a7e500 | 121 | if( ( usAddress >= REG_INPUT_START ) |
cam | 0:0453a0a7e500 | 122 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
cam | 0:0453a0a7e500 | 123 | { |
cam | 0:0453a0a7e500 | 124 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
cam | 0:0453a0a7e500 | 125 | while( usNRegs > 0 ) |
cam | 0:0453a0a7e500 | 126 | { |
cam | 0:0453a0a7e500 | 127 | *pucRegBuffer++ = |
cam | 0:0453a0a7e500 | 128 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
cam | 0:0453a0a7e500 | 129 | *pucRegBuffer++ = |
cam | 0:0453a0a7e500 | 130 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
cam | 0:0453a0a7e500 | 131 | iRegIndex++; |
cam | 0:0453a0a7e500 | 132 | usNRegs--; |
cam | 0:0453a0a7e500 | 133 | } |
cam | 0:0453a0a7e500 | 134 | } |
cam | 0:0453a0a7e500 | 135 | } |
cam | 0:0453a0a7e500 | 136 | |
cam | 0:0453a0a7e500 | 137 | if (eMode == MB_REG_WRITE) |
cam | 0:0453a0a7e500 | 138 | { |
cam | 0:0453a0a7e500 | 139 | if( ( usAddress >= REG_INPUT_START ) |
cam | 0:0453a0a7e500 | 140 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
cam | 0:0453a0a7e500 | 141 | { |
cam | 0:0453a0a7e500 | 142 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
cam | 0:0453a0a7e500 | 143 | while( usNRegs > 0 ) |
cam | 0:0453a0a7e500 | 144 | { |
cam | 0:0453a0a7e500 | 145 | usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); |
cam | 0:0453a0a7e500 | 146 | pucRegBuffer+=2; |
cam | 0:0453a0a7e500 | 147 | iRegIndex++; |
cam | 0:0453a0a7e500 | 148 | usNRegs--; |
cam | 0:0453a0a7e500 | 149 | } |
cam | 0:0453a0a7e500 | 150 | } |
cam | 0:0453a0a7e500 | 151 | } |
cam | 0:0453a0a7e500 | 152 | |
cam | 0:0453a0a7e500 | 153 | return eStatus; |
cam | 0:0453a0a7e500 | 154 | } |
cam | 0:0453a0a7e500 | 155 | |
cam | 0:0453a0a7e500 | 156 | |
cam | 0:0453a0a7e500 | 157 | eMBErrorCode |
cam | 0:0453a0a7e500 | 158 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, |
cam | 0:0453a0a7e500 | 159 | eMBRegisterMode eMode ) |
cam | 0:0453a0a7e500 | 160 | { |
cam | 0:0453a0a7e500 | 161 | return MB_ENOREG; |
cam | 0:0453a0a7e500 | 162 | } |
cam | 0:0453a0a7e500 | 163 | |
cam | 0:0453a0a7e500 | 164 | eMBErrorCode |
cam | 0:0453a0a7e500 | 165 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
cam | 0:0453a0a7e500 | 166 | { |
cam | 0:0453a0a7e500 | 167 | return MB_ENOREG; |
cam | 0:0453a0a7e500 | 168 | } |
cam | 0:0453a0a7e500 | 169 |