CsiTest.cpp

CSI API example.The following example shows how to use the CSI API from application code.

//============================================================================
// (c) Copyright 2013, CETONI GmbH
//
/// \file CsiTest.cpp
/// \author Uwe Kindler (UK)
/// \date 2010/05/27
/// \brief CSI example application
///
/// Example application that shows how to use the CSI API.
//===========================================================================
//===========================================================================
// INCLUDES
//===========================================================================
#include <iostream>
#include <stdint.h>
#include "csi_api.h"
using namespace std;
//===========================================================================
// LOCAL DATA
//===========================================================================
static struct CsiDev CsiDev;
static struct CsiBus CsiBus;
/**
* Print error code and error description via CsiErrorToString() to std::out
*/
long showError(long ErrCode)
{
if (ErrCode < 0)
{
std::cout << "Error code: " << std::hex << (0 - ErrCode) << std::dec
<< " - " << CsiErrorToString(ErrCode) << std::endl;
}
return ErrCode;
}
/**
* Application main entry point
*/
int main(int argc, char *argv[])
{
//
// First open the serial port 1 (on Windows COM1) with a baudrate of
// 38400 and a timeout value of 100 ms
//
long Result = CsiOpen(&CsiBus, "COM10", CSI_BAUD_115200, 100);
showError(Result);
//
// If the port is open, we initialize the device object CsiDev. Normally
// this does not require any serial communication
//
Result = CsiDevInit(&CsiBus, 2, &CsiDev, CSI_SERIAL_PROTOCOL_V1);
showError(Result);
//
// Now read the value of the object dictionary entry with index 0x1000 and
// sub index 0 into the data buffer provided in Data parameter. Then print
// the read value for debugging purposes.
//
uint32_t Data;
Result = CsiDevReadObject(&CsiDev, 0x1000, 0, &Data);
showError(Result);
if (ERR_NOERR == Result)
{
std::cout << "CsiDevReadObject(&CsiDev, 0x1000): "
<< std::hex << Data << std::dec << std::endl;
}
//
// Now read the value of the object dictionary entry with index 0x200C and
// sub index 1 into the data buffer provided in Data parameter. Then print
// the read value for debugging purposes.
//
Result = CsiDevReadObject(&CsiDev, 0x200C, 1, &Data);
showError(Result);
std::cout << "RxData: " << std::hex << Data << std::dec << std::endl;
//
// Now write defined data to the object dictionary entry we have just read
//
Data = 0x12345678;
Result = CsiDevWriteObject(&CsiDev, 0x200C, 1, Data);
showError(Result);
//
// Now read back the value of the object dictionary entry to check if value
// has been written successfully.
//
Result = CsiDevReadObject(&CsiDev, 0x200C, 1, &Data);
showError(Result);
std::cout << "RxData: " << std::hex << Data << std::dec << std::endl;
//
// Before we leave the application we call CsiClose() to close the serial
// connection an free any claimed resources.
//
Result = CsiClose(&CsiBus);
showError(Result);
return 0;
}