Modified version of ModbusTCP
Dependencies: EthernetNetIf mbed
ModBus/portother.cpp@0:62be54b8975d, 2012-03-13 (annotated)
- Committer:
- paleskyjp
- Date:
- Tue Mar 13 09:11:49 2012 +0000
- Revision:
- 0:62be54b8975d
The first modification of ModbusTCP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
paleskyjp | 0:62be54b8975d | 1 | /* |
paleskyjp | 0:62be54b8975d | 2 | * FreeModbus Libary: lwIP Port |
paleskyjp | 0:62be54b8975d | 3 | * Copyright (C) 2006 Christian Walter <wolti@sil.at> |
paleskyjp | 0:62be54b8975d | 4 | * |
paleskyjp | 0:62be54b8975d | 5 | * This library is free software; you can redistribute it and/or |
paleskyjp | 0:62be54b8975d | 6 | * modify it under the terms of the GNU Lesser General Public |
paleskyjp | 0:62be54b8975d | 7 | * License as published by the Free Software Foundation; either |
paleskyjp | 0:62be54b8975d | 8 | * version 2.1 of the License, or (at your option) any later version. |
paleskyjp | 0:62be54b8975d | 9 | * |
paleskyjp | 0:62be54b8975d | 10 | * This library is distributed in the hope that it will be useful, |
paleskyjp | 0:62be54b8975d | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paleskyjp | 0:62be54b8975d | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paleskyjp | 0:62be54b8975d | 13 | * Lesser General Public License for more details. |
paleskyjp | 0:62be54b8975d | 14 | * |
paleskyjp | 0:62be54b8975d | 15 | * You should have received a copy of the GNU Lesser General Public |
paleskyjp | 0:62be54b8975d | 16 | * License along with this library; if not, write to the Free Software |
paleskyjp | 0:62be54b8975d | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
paleskyjp | 0:62be54b8975d | 18 | * |
paleskyjp | 0:62be54b8975d | 19 | * File: $Id: portother.c,v 1.1 2006/08/30 23:18:07 wolti Exp $ |
paleskyjp | 0:62be54b8975d | 20 | */ |
paleskyjp | 0:62be54b8975d | 21 | |
paleskyjp | 0:62be54b8975d | 22 | /* ----------------------- System includes ----------------------------------*/ |
paleskyjp | 0:62be54b8975d | 23 | //#include <sys/types.h> |
paleskyjp | 0:62be54b8975d | 24 | //#include <sys/stat.h> |
paleskyjp | 0:62be54b8975d | 25 | //#include <unistd.h> |
paleskyjp | 0:62be54b8975d | 26 | #include <stdio.h> |
paleskyjp | 0:62be54b8975d | 27 | #include <stdarg.h> |
paleskyjp | 0:62be54b8975d | 28 | #include <string.h> |
paleskyjp | 0:62be54b8975d | 29 | |
paleskyjp | 0:62be54b8975d | 30 | #include "port.h" |
paleskyjp | 0:62be54b8975d | 31 | |
paleskyjp | 0:62be54b8975d | 32 | /* ----------------------- Defines ------------------------------------------*/ |
paleskyjp | 0:62be54b8975d | 33 | #define MB_FRAME_LOG_BUFSIZE 512 |
paleskyjp | 0:62be54b8975d | 34 | |
paleskyjp | 0:62be54b8975d | 35 | /* ----------------------- Start implementation -----------------------------*/ |
paleskyjp | 0:62be54b8975d | 36 | |
paleskyjp | 0:62be54b8975d | 37 | #ifdef MB_TCP_DEBUG |
paleskyjp | 0:62be54b8975d | 38 | void |
paleskyjp | 0:62be54b8975d | 39 | prvvMBTCPLogFrame( const CHAR * pucMsg, UCHAR * pucFrame, USHORT usFrameLen ) |
paleskyjp | 0:62be54b8975d | 40 | { |
paleskyjp | 0:62be54b8975d | 41 | int i; |
paleskyjp | 0:62be54b8975d | 42 | int res; |
paleskyjp | 0:62be54b8975d | 43 | int iBufPos = 0; |
paleskyjp | 0:62be54b8975d | 44 | size_t iBufLeft = MB_FRAME_LOG_BUFSIZE; |
paleskyjp | 0:62be54b8975d | 45 | static CHAR arcBuffer[MB_FRAME_LOG_BUFSIZE]; |
paleskyjp | 0:62be54b8975d | 46 | |
paleskyjp | 0:62be54b8975d | 47 | assert( pucFrame != NULL ); |
paleskyjp | 0:62be54b8975d | 48 | |
paleskyjp | 0:62be54b8975d | 49 | for( i = 0; i < usFrameLen; i++ ) |
paleskyjp | 0:62be54b8975d | 50 | { |
paleskyjp | 0:62be54b8975d | 51 | /* Print some additional frame information. */ |
paleskyjp | 0:62be54b8975d | 52 | switch ( i ) |
paleskyjp | 0:62be54b8975d | 53 | { |
paleskyjp | 0:62be54b8975d | 54 | case 0: |
paleskyjp | 0:62be54b8975d | 55 | /* TID = Transaction Identifier. */ |
paleskyjp | 0:62be54b8975d | 56 | res = snprintf( &arcBuffer[iBufPos], iBufLeft, "| TID = " ); |
paleskyjp | 0:62be54b8975d | 57 | break; |
paleskyjp | 0:62be54b8975d | 58 | case 2: |
paleskyjp | 0:62be54b8975d | 59 | /* PID = Protocol Identifier. */ |
paleskyjp | 0:62be54b8975d | 60 | res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | PID = " ); |
paleskyjp | 0:62be54b8975d | 61 | break; |
paleskyjp | 0:62be54b8975d | 62 | case 4: |
paleskyjp | 0:62be54b8975d | 63 | /* Length */ |
paleskyjp | 0:62be54b8975d | 64 | res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | LEN = " ); |
paleskyjp | 0:62be54b8975d | 65 | break; |
paleskyjp | 0:62be54b8975d | 66 | case 6: |
paleskyjp | 0:62be54b8975d | 67 | /* UID = Unit Identifier. */ |
paleskyjp | 0:62be54b8975d | 68 | res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | UID = " ); |
paleskyjp | 0:62be54b8975d | 69 | break; |
paleskyjp | 0:62be54b8975d | 70 | case 7: |
paleskyjp | 0:62be54b8975d | 71 | /* MB Function Code. */ |
paleskyjp | 0:62be54b8975d | 72 | res = snprintf( &arcBuffer[iBufPos], iBufLeft, "|| FUNC = " ); |
paleskyjp | 0:62be54b8975d | 73 | break; |
paleskyjp | 0:62be54b8975d | 74 | case 8: |
paleskyjp | 0:62be54b8975d | 75 | /* MB PDU rest. */ |
paleskyjp | 0:62be54b8975d | 76 | res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | DATA = " ); |
paleskyjp | 0:62be54b8975d | 77 | break; |
paleskyjp | 0:62be54b8975d | 78 | default: |
paleskyjp | 0:62be54b8975d | 79 | res = 0; |
paleskyjp | 0:62be54b8975d | 80 | break; |
paleskyjp | 0:62be54b8975d | 81 | } |
paleskyjp | 0:62be54b8975d | 82 | if( res == -1 ) |
paleskyjp | 0:62be54b8975d | 83 | { |
paleskyjp | 0:62be54b8975d | 84 | break; |
paleskyjp | 0:62be54b8975d | 85 | } |
paleskyjp | 0:62be54b8975d | 86 | else |
paleskyjp | 0:62be54b8975d | 87 | { |
paleskyjp | 0:62be54b8975d | 88 | iBufPos += res; |
paleskyjp | 0:62be54b8975d | 89 | iBufLeft -= res; |
paleskyjp | 0:62be54b8975d | 90 | } |
paleskyjp | 0:62be54b8975d | 91 | |
paleskyjp | 0:62be54b8975d | 92 | /* Print the data. */ |
paleskyjp | 0:62be54b8975d | 93 | res = snprintf( &arcBuffer[iBufPos], iBufLeft, "%02X", pucFrame[i] ); |
paleskyjp | 0:62be54b8975d | 94 | if( res == -1 ) |
paleskyjp | 0:62be54b8975d | 95 | { |
paleskyjp | 0:62be54b8975d | 96 | break; |
paleskyjp | 0:62be54b8975d | 97 | } |
paleskyjp | 0:62be54b8975d | 98 | else |
paleskyjp | 0:62be54b8975d | 99 | { |
paleskyjp | 0:62be54b8975d | 100 | iBufPos += res; |
paleskyjp | 0:62be54b8975d | 101 | iBufLeft -= res; |
paleskyjp | 0:62be54b8975d | 102 | } |
paleskyjp | 0:62be54b8975d | 103 | } |
paleskyjp | 0:62be54b8975d | 104 | |
paleskyjp | 0:62be54b8975d | 105 | if( res != -1 ) |
paleskyjp | 0:62be54b8975d | 106 | { |
paleskyjp | 0:62be54b8975d | 107 | /* Append an end of frame string. */ |
paleskyjp | 0:62be54b8975d | 108 | res = snprintf( &arcBuffer[iBufPos], iBufLeft, " |\r\n" ); |
paleskyjp | 0:62be54b8975d | 109 | if( res != -1 ) |
paleskyjp | 0:62be54b8975d | 110 | { |
paleskyjp | 0:62be54b8975d | 111 | vMBPortLog( MB_LOG_DEBUG, pucMsg, "%s", arcBuffer ); |
paleskyjp | 0:62be54b8975d | 112 | } |
paleskyjp | 0:62be54b8975d | 113 | } |
paleskyjp | 0:62be54b8975d | 114 | } |
paleskyjp | 0:62be54b8975d | 115 | #endif |
paleskyjp | 0:62be54b8975d | 116 | |
paleskyjp | 0:62be54b8975d | 117 | #ifdef MB_TCP_DEBUG |
paleskyjp | 0:62be54b8975d | 118 | void |
paleskyjp | 0:62be54b8975d | 119 | vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, const CHAR * szFmt, ... ) |
paleskyjp | 0:62be54b8975d | 120 | { |
paleskyjp | 0:62be54b8975d | 121 | va_list args; |
paleskyjp | 0:62be54b8975d | 122 | static const char *arszLevel2Str[] = { "ERROR", "WARN", "INFO", "DEBUG"}; |
paleskyjp | 0:62be54b8975d | 123 | |
paleskyjp | 0:62be54b8975d | 124 | ( void )printf( "%s: %s: ", arszLevel2Str[eLevel], szModule ); |
paleskyjp | 0:62be54b8975d | 125 | va_start( args, szFmt ); |
paleskyjp | 0:62be54b8975d | 126 | vprintf( szFmt, args ); |
paleskyjp | 0:62be54b8975d | 127 | va_end( args ); |
paleskyjp | 0:62be54b8975d | 128 | } |
paleskyjp | 0:62be54b8975d | 129 | #endif |