/** * Poll the modbus. * * @param pvParameter */ void * pvPollingThread( void *pvParameter ) { vSetPollingThreadState( RUNNING ); DEBUG_PUTSTRING("Thread started!"); if( eMBEnable( ) == MB_ENOERR ) { do { if( eMBPoll( ) != MB_ENOERR ) break; } while( eGetPollingThreadState( ) != SHUTDOWN ); } ( void )eMBDisable( ); vSetPollingThreadState( STOPPED ); DEBUG_PUTSTRING("Thread stopped!"); cmd_done(BOOT_EXIT); return 0; }
DWORD WINAPI dwPollingThread( LPVOID lpParameter ) { eSetPollingThreadState( RUNNING ); if( eMBEnable( ) == MB_ENOERR ) { do { if( eMBPoll( ) != MB_ENOERR ) break; } while( eGetPollingThreadState( ) != SHUTDOWN ); } ( void )eMBDisable( ); eSetPollingThreadState( STOPPED ); return 0; }
/** * Create polling thread * @return */ BOOL bCreatePollingThread( void ) { BOOL bResult; pthread_t xThread; if( eGetPollingThreadState( ) == STOPPED ) { if( pthread_create( &xThread, NULL, pvPollingThread, NULL ) != 0 ) { bResult = FALSE; } else { bResult = TRUE; } } else { bResult = FALSE; } return bResult; }
BOOL bCreatePollingThread( void ) { BOOL bResult; if( eGetPollingThreadState( ) == STOPPED ) { if( ( hPollThread = CreateThread( NULL, 0, dwPollingThread, NULL, 0, NULL ) ) == NULL ) { /* Can't create the polling thread. */ bResult = FALSE; } else { bResult = TRUE; } } else { bResult = FALSE; } return bResult; }
/* ----------------------- Start implementation -----------------------------*/ int _tmain( int argc, _TCHAR * argv[] ) { int iExitCode; TCHAR cCh; BOOL bDoExit; for (int Num = REG_INPUT_NREGS; Num >= 0; Num--) { usRegInputBuf[Num] = Num; } for (int Num = INPUT_STATUS_NREGS; Num >= 0; Num--) { ucInputStatusBuf[Num] = 1; } ucInputStatusBuf[0] = 255; for (int Num = 0; Num < REG_HOLDING_NREGS; Num++) { usRegHoldingBuf[Num] = Num; } for (int Num = 0; Num < INPUT_COIL_NREGS; Num++) { ucInputCoilBuf[Num] = 0; } ucInputCoilBuf[0] = 255; if( eMBTCPInit( MB_TCP_PORT_USE_DEFAULT ) != MB_ENOERR ) { _ftprintf( stderr, _T( "%s: can't initialize modbus stack!\r\n" ), PROG ); iExitCode = EXIT_FAILURE; } else { /* Create synchronization primitives and set the current state * of the thread to STOPPED. */ InitializeCriticalSection( &hPollLock ); eSetPollingThreadState( STOPPED ); /* CLI interface. */ _tprintf( _T( "Type 'q' for quit or 'h' for help!\r\n" ) ); bDoExit = FALSE; do { _tprintf( _T( "> " ) ); cCh = _gettchar( ); switch ( cCh ) { case _TCHAR( 'q' ): bDoExit = TRUE; break; case _TCHAR( 'd' ): eSetPollingThreadState( SHUTDOWN ); break; case _TCHAR( 'e' ): if( bCreatePollingThread( ) != TRUE ) { _tprintf( _T( "Can't start protocol stack! Already running?\r\n" ) ); } break; case _TCHAR( 's' ): switch ( eGetPollingThreadState( ) ) { case RUNNING: _tprintf( _T( "Protocol stack is running.\r\n" ) ); break; case STOPPED: _tprintf( _T( "Protocol stack is stopped.\r\n" ) ); break; case SHUTDOWN: _tprintf( _T( "Protocol stack is shuting down.\r\n" ) ); break; } break; case _TCHAR( 'h' ): _tprintf( _T( "FreeModbus demo application help:\r\n" ) ); _tprintf( _T( " 'd' ... disable protocol stack.\r\n" ) ); _tprintf( _T( " 'e' ... enabled the protocol stack\r\n" ) ); _tprintf( _T( " 's' ... show current status\r\n" ) ); _tprintf( _T( " 'q' ... quit applicationr\r\n" ) ); _tprintf( _T( " 'h' ... this information\r\n" ) ); _tprintf( _T( "\r\n" ) ); _tprintf( _T( "Copyright 2006 Christian Walter <*****@*****.**>\r\n" ) ); break; default: if( cCh != _TCHAR('\n') ) { _tprintf( _T( "illegal command '%c'!\r\n" ), cCh ); } break; } /* eat up everything untill return character. */ while( cCh != '\n' ) { cCh = _gettchar( ); } } while( !bDoExit ); /* Release hardware resources. */ ( void )eMBClose( ); iExitCode = EXIT_SUCCESS; } return iExitCode; }