Menu

C Serial Driver tutorial sources

Thierry CHOMAUD vmacari

main.c

#include <stdio.h>
#include <memory.h>
#ifdef _WINDOWS
#include <time.h>
#else
#include <time.h>
#include <sys/time.h>
#endif
// serial driver api include
#include <serial_driver_event.h>
#include <serial_driver_status.h>
// domain include
#include <loader_serial_driver.h>
#include <serial_driver_types.h>

SerialDriverLoader driverHandler;
void*              rs232DriverInstance;

char portId[256] = "COM4";
SerialExchangeData serialExchangedData;

int receiveFrameListener( void* param, const pSerialExchangeData i_frame );
int serialDriverEventListener( void* param, int notifyCode );
const char* GetCurrentTimeString();

int main(void) {
    printf( "%s csrs232driver example BEGIN. \n", GetCurrentTimeString() );
    if ( OK == load_serial_driver( "csrs232driver", &driverHandler) )
    {
        rs232DriverInstance = driverHandler.create();
        if ( rs232DriverInstance )
        {
            printf( "%s csrs232driver creation success. \n",
                GetCurrentTimeString() );
            int status = OK_SERIALDRIVER_SUCCESS;
            status = driverHandler.init( rs232DriverInstance );
            printf( "%s csrs232driver init status: %i \n",
                GetCurrentTimeString(), status );
            driverHandler.set_port_id( rs232DriverInstance, portId);
            driverHandler.add_listener( rs232DriverInstance,
                receiveFrameListener, serialDriverEventListener, 0 );
            status = driverHandler.open( rs232DriverInstance );
            printf( "%s csrs232driver open status: %i \n",
                GetCurrentTimeString(), status );
            // data frame creation
            serialExchangedData = create_serial_exchange_data ((SerialFrame){ {0x50, 0x05}, 2 });
            // data frame sending
            status = driverHandler.send( rs232DriverInstance, &serialExchangedData );
            char hexframe[MAX_SERIALFRAME_LEN*2+1];
            printf( "%s csrs232driver send status for frame '%s': %s \n",
                GetCurrentTimeString(),
                get_hex_serialexchangeddata(hexframe, sizeof(hexframe), &serialExchangedData),
                get_sending_status_name(status));
            driverHandler.destroy( rs232DriverInstance );
        }

        free_serial_driver( &driverHandler );
    } else {
        printf( "%s csrs232driver loading failure. \n", GetCurrentTimeString() );
    }
    printf( "%s csrs232driver example END. \n", GetCurrentTimeString() );
    return 0;
}

int receiveFrameListener( void* param, const pSerialExchangeData serialData )
{
    char hexframe[MAX_SERIALFRAME_LEN*2+1];
    printf( "%s receiveFrame: %s, \n",
            GetCurrentTimeString(),
            get_hex_serialexchangeddata(hexframe, sizeof(hexframe), serialData) );
    return 0;
}

int serialDriverEventListener( void* param, int notifyCode )
{
    return 0;
}

const char* GetCurrentTimeString()
{
    static char currTime[100] = "";
    currTime[0] = 0;

#ifdef _WINDOWS
    SYSTEMTIME st = {0};
    GetLocalTime(&st);
    sprintf( currTime, "[%02d:%02d:%02d:%03d]", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds );
#else
    struct timeval currentTime;
    gettimeofday( &currentTime, 0 );
    struct tm *tm = localtime(&currentTime.tv_sec);
    sprintf( currTime, "[%02d:%02d:%02d:%03d] ", tm->tm_hour, tm->tm_min, tm->tm_sec, (int)currentTime.tv_usec / 1000 );
#endif

    return currTime;
}

Related

Wiki: C-CPP_SerialDriver_UserGuide

MongoDB Logo MongoDB