Tom Martins / Mbed OS Nucleo_RS485_Modbus

Fork of Modbus by Cam Marshall

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers portother.cpp Source File

portother.cpp

00001 /*
00002  * FreeModbus Libary: lwIP Port
00003  * Copyright (C) 2006 Christian Walter <wolti@sil.at>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * File: $Id: portother.c,v 1.2 2006/09/04 14:39:20 wolti Exp $
00020  */
00021 
00022 /* ----------------------- System includes ----------------------------------*/
00023 #include <stdio.h>
00024 #include <stdarg.h>
00025 #include <string.h>
00026 
00027 #include "port.h"
00028 
00029 /* ----------------------- Defines ------------------------------------------*/
00030 #define MB_FRAME_LOG_BUFSIZE    512
00031 
00032 /* ----------------------- Start implementation -----------------------------*/
00033 
00034 #ifdef MB_TCP_DEBUG
00035 void
00036 prvvMBTCPLogFrame( UCHAR * pucMsg, UCHAR * pucFrame, USHORT usFrameLen )
00037 {
00038     int             i;
00039     int             res = 0;
00040     int             iBufPos = 0;
00041     size_t          iBufLeft = MB_FRAME_LOG_BUFSIZE;
00042     static CHAR     arcBuffer[MB_FRAME_LOG_BUFSIZE];
00043 
00044     assert( pucFrame != NULL );
00045 
00046     for( i = 0; i < usFrameLen; i++ )
00047     {
00048         /* Print some additional frame information. */
00049         switch ( i )
00050         {
00051         case 0:
00052             /* TID = Transaction Identifier. */
00053             res = snprintf( &arcBuffer[iBufPos], iBufLeft, "| TID = " );
00054             break;
00055         case 2:
00056             /* PID = Protocol Identifier. */
00057             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | PID = " );
00058             break;
00059         case 4:
00060             /* Length */
00061             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | LEN = " );
00062             break;
00063         case 6:
00064             /* UID = Unit Identifier. */
00065             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | UID = " );
00066             break;
00067         case 7:
00068             /* MB Function Code. */
00069             res = snprintf( &arcBuffer[iBufPos], iBufLeft, "|| FUNC = " );
00070             break;
00071         case 8:
00072             /* MB PDU rest. */
00073             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | DATA = " );
00074             break;
00075         default:
00076             res = 0;
00077             break;
00078         }
00079         if( res == -1 )
00080         {
00081             break;
00082         }
00083         else
00084         {
00085             iBufPos += res;
00086             iBufLeft -= res;
00087         }
00088 
00089         /* Print the data. */
00090         res = snprintf( &arcBuffer[iBufPos], iBufLeft, "%02X", pucFrame[i] );
00091         if( res == -1 )
00092         {
00093             break;
00094         }
00095         else
00096         {
00097             iBufPos += res;
00098             iBufLeft -= res;
00099         }
00100     }
00101 
00102     if( res != -1 )
00103     {
00104         /* Append an end of frame string. */
00105         res = snprintf( &arcBuffer[iBufPos], iBufLeft, " |\r\n" );
00106         if( res != -1 )
00107         {
00108             vMBPortLog( MB_LOG_DEBUG, (const char*)pucMsg, "%s", arcBuffer );
00109         }
00110     }
00111 }
00112 #endif
00113 
00114 void
00115 vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, const CHAR * szFmt, ... )
00116 {
00117     va_list         args;
00118     static const char *arszLevel2Str[] = { "DEBUG", "INFO", "WARN", "ERROR" };
00119 
00120     ( void )printf( "%s: %s: ", arszLevel2Str[eLevel], szModule );
00121     va_start( args, szFmt );
00122     vprintf( szFmt, args );
00123     va_end( args );
00124 }
00125