next up previous
Next: Tutorial 3 Up: Decription of RTDX_Service_2 Previous: The header file RTDX_Service_2.h

The C++-file RTDX_Service_2.cpp



This file contains the C++-code of the RTDX-wrapper for this tutorial. In includes the header files RTDX_Service_2 described above and <string> for the string processing for setting the name of the DSP-board and its CPU.

/*
 * RTDX_Service_2.cpp
 */
#include "RTDX_Service.h"
#include <string.h>

RTDX_Service_2::RTDX_Service_2() {
}

int RTDX_Service_2::Init() {
    HRESULT hr = S_OK;

    ::CoInitialize(NULL);                // initialize COM

    // instanciate RTDX for write channel 'ichan'
    hr = w_pRtdx.CreateInstance(__uuidof(RTDXINTLib::RtdxExp));
    if (!SUCCEEDED(hr)) {
        return 1; // signal instantiation error
    }

    // instanciate RTDX for read channel 'ochan'
    hr = r_pRtdx.CreateInstance(__uuidof(RTDXINTLib::RtdxExp));
    if (!SUCCEEDED(hr)) {
        return 1; // signal instantiation error
    }
    return 0;
}

The creator of RTDX_Service_2 has no parameters and it performs no action. Instead, initialization is done in a separate initialization function Init, which is called after RTDX_Service_2 has been created. Init() first initializes the COM-interface and then creates two instances of the RTDX-interface, one for the read-channel and one for the write channel. Eventual errors are signalled to the calling routine via its return value ( 0 = no error).

int RTDX_Service::SetBoardAndProcessor(wchar_t* board,
                                       wchar_t* cpu) {
    long status;

    BSTR boardName = ::SysAllocString(board);
    BSTR processorName = ::SysAllocString(cpu);

    // Set processor in RTDX for write channel
    status = w_pRtdx->SetProcessor(boardName,processorName);
    if (status != Success)
        return 1;

    // Set processor in RTDX for read channel
    status = r_pRtdx->SetProcessor(boardName,processorName);
    if (status != Success)
        return 1;
    return 0;
}

The function SetBoardAndProcessor identifies the DSP-board and its CPU to the RTDX-interface. Since there are two interfaces instantiated, the board- and CPU-name is written to the read- and to the write-channel. Errors are signalled to the calling routine via its return value.

long RTDX_Service_2::StartRTDX() {
	HRESULT hr = S_OK;
	// only necessary for one instantiation because of global
	// set configuration for 'continuous, buffer size 1024, 4 buffers
	// RTDX can only be configured if RTDX is disabled
	hr = w_pRtdx->DisableRtdx();
	if (!SUCCEEDED(hr)) {
		return 1; // signal rtdx-enable error
	}
	// now configure RTDX
	hr = w_pRtdx->ConfigureRtdx(1,1024,4);
	if (!SUCCEEDED(hr)) {
		return 2; // signal configuration error
	}
	hr = w_pRtdx->EnableRtdx();
	if (!SUCCEEDED(hr)) {
		return 3; // signal rtdx-enable error
	}
	return 0;
}

The standard configuration for the RTDX-interface is specified here as 'continuous mode' (first parameter = 1), buffer size of 1024 bytes (2nd parameter) and 4 buffers. When setting this configuration, the RTDX-interface has to be disabled. Therefore the DisableRtdx-function has to be called first, since the interface may have already been activated before. At the end the RTDX-interface is enabled via the EnableRtdx-function from this service routine. Therefore no action is needed from inside the 'Code Composer Studio'-IDE, which otherwise would have to activate the RTDX-interface via the 'RTDX Cofiguration Control'-tool.

long RTDX_Service_2::OpenWriteChannel() {
    return w_pRtdx->Open("ichan","w");
}

long RTDX_Service_2::OpenReadChannel() {
    return r_pRtdx->Open("ochan","r");
}

long RTDX_Service_2::WriteToClient(long number) {
    long numBytes;
    return w_pRtdx->WriteI4(number,&numBytes);
}

long RTDX_Service_2::ReadFromClient() {
    long result;
    long data;
    result = r_pRtdx->ReadI4(&data);
    if (result == Success) {
        return data;
    } else {
    return result;
    }
}

The write-channel named ichan can be opened via function OpenWriteChannel and the read-channel named ochan via function OpenWriteChannel. A single number of type 'long' (4 byte length) can be written to the client via function WriteToClient. A number of type 'long' can be read from the client using the blocking function ReadFromClient. Inside this function, data from the read-channel are read until the return value of internal RTDX-function ReadI4 signals success. The valid 4-byte integer value is returned as a 'long'-value.


next up previous
Next: Tutorial 3 Up: Decription of RTDX_Service_2 Previous: The header file RTDX_Service_2.h
H. Rogalla 2005-12-07