Ejemplo n.º 1
0
/* ==============================================
 main task routine
 ============================================== */
int main(void) {
	pool_memadd((uint32_t) pool, sizeof(pool));

#ifdef DEBUG
	dbg.start();
#endif

	eMBErrorCode eStatus;
//	dbg.waitToDebugMode();

#if MB_TCP_ENABLED == 1
	eStatus = eMBTCPInit( MB_TCP_PORT_USE_DEFAULT );
#endif

	if (eStatus != MB_ENOERR)
		dbg.println("can't initialize modbus stack!");

	/* Enable the Modbus Protocol Stack. */
	eStatus = eMBEnable();
	if (eStatus != MB_ENOERR)
		dbg.println("can't enable modbus stack!");

	// Initialise some registers
	usRegInputBuf[1] = 0x1234;
	usRegInputBuf[2] = 0x5678;
	usRegInputBuf[3] = 0x9abc;

	// debug LED
	CPin led(LED1);
	CTimeout tm;

	// Enter an endless loop
	while (1) {
		if ( tm.read()>0.5 ) {
			led = !led;
			tm.reset();
		}

	    eStatus = eMBPoll(  );

	    /* Here we simply count the number of poll cycles. */
	    usRegInputBuf[0]++;
	}
	return 0;
}
Ejemplo n.º 2
0
/* ----------------------- 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;
}