How do you see Tx/Rx Bus data in trace window via the COM interface in CANalyzer in C++?

247 Views Asked by At

I am completely new to this topic and I've been asked to create an application that shows the data exchange happening on a bus in CANalyzer via the COM interface, with the exact virtual time point when that data is being sent.

So far, what I have is this :

// DemoTool.cpp : Defines the entry point for the application.
//


#include "DemoTool.h"
#include "atlbase.h"
#include "CANalyzer.h"
#include "comdef.h"



class VApplication
{
public:
    VApplication() : mApplication(NULL) {}
    ~VApplication() {}
    bool Connect();
    bool StartMeasurement(int timeOut=5); // seconds
    bool SendData();
    bool StopMeasurement();
    void ShutDown();
private:
    CComPtr<IApplication> mApplication;
    CComPtr<IMeasurement5> mMeasurement;
    CComPtr<IConfiguration14> mConfiguration;
    CComPtr<IBus3> mBus;
};

bool VApplication::Connect()
{

    HRESULT hr = E_FAIL;
    if (mApplication != NULL) return true;

    CLSID clsId;
    hr = CLSIDFromProgID(L"CANalyzer.Application", &clsId);
    if (SUCCEEDED(hr))
    {
        hr = ::CoInitialize(NULL);
        if (SUCCEEDED(hr))
        {
            hr = CoCreateInstance(clsId, NULL, CLSCTX_LOCAL_SERVER | CLSCTX_ACTIVATE_64_BIT_SERVER, __uuidof(IApplication10), (void**)&mApplication);
            
            if (SUCCEEDED(hr))
            {
                CComPtr<IDispatch> dispatchMeasurement;
                hr = mApplication->get_Measurement(&dispatchMeasurement);
                if (FAILED(hr))
                {
                    std::cout << "Failed to get measurement pointer.\n";
                }
                else
                {
                    hr = dispatchMeasurement->QueryInterface(__uuidof(IMeasurement5), (void**)&mMeasurement);
                    if (FAILED(hr))
                    {
                        std::cout << "Failed to dispatch measurement pointer.\n";
                    }
                }


                CComPtr<IDispatch> dispatchConfiguration;
                hr = mApplication->get_Configuration(&dispatchConfiguration);
                if (FAILED(hr))
                {
                    std::cout << "Failed to get Configuration pointer.\n";
                }
                else
                {
                    hr = dispatchConfiguration->QueryInterface(__uuidof(IConfiguration14), (void**)&mConfiguration);
                    if (FAILED(hr))
                    {
                        std::cout << "Failed to dispatch Configuration pointer.\n";
                    }
                }
                CComPtr<IDispatch> dispatchBus;
                hr = mApplication->get_Bus(L"CAN", &dispatchBus);
                if (FAILED(hr))
                {
                    std::cout << "Failed to get Bus pointer.\n";
                }
                else
                {
                    hr = dispatchBus->QueryInterface(__uuidof(IBus3), (void**)&mBus);
                    if (FAILED(hr))
                    {
                        std::cout << "Failed to dispatch Bus pointer.\n";
                    }
                }
            }
        }
    }
    return SUCCEEDED(hr);
}

bool VApplication::StartMeasurement(int timeOut)
{
    HRESULT hr = 0x0;
    int attempt = 0;
    VARIANT_BOOL toolIsRun = 0;

    hr = mMeasurement->Start();
    if (SUCCEEDED(hr))
    {
        while (toolIsRun != -1)
        {
            attempt++;
            Sleep(1);
            hr = mMeasurement->get_Running(&toolIsRun);
            if (attempt > timeOut) break;
        };
        if (toolIsRun == -1)
        {
            return true;
        }
    }
    return SUCCEEDED(hr);
}

bool VApplication::SendData()
{
    CComPtr<IMessage2> msg;
}

bool VApplication::StopMeasurement()
{
    HRESULT hr = 0x0;
    int attempt = 0;
    VARIANT_BOOL toolIsRunning = 0;

    hr = mMeasurement->StopEx();
    Sleep(100);
    hr = mMeasurement->get_Running(&toolIsRunning);
    if (toolIsRunning == -1)
    {
        Sleep(1000);
        hr = mMeasurement->get_Running(&toolIsRunning);

        if (toolIsRunning == -1)
        {
            hr = mMeasurement->StopEx();
            Sleep(100);
        }
        else
        {
            std::cout << "CANalyzer is stopped.\n";
        }

        hr = mMeasurement->get_Running(&toolIsRunning);
        if (toolIsRunning)
        {
            std::cout << "Failed to get status from CANalyzer. Tool may still be running!\n";
        }
    }
    return SUCCEEDED(hr);
}

void VApplication::ShutDown()
{
    if (mMeasurement)
    {
        mMeasurement.Release();
    }
    if (mApplication)
    {
        mApplication.Release();
    }
}

static CComModule module;
static VApplication* app;

int main()
{
    std::cout << "Hello CMake." << std::endl;
    ::CoInitialize(NULL);
    module.Init(NULL, ::GetModuleHandle(NULL));

    app = new VApplication();
    bool conval = app->Connect();
    std::cout << "Connection " << (conval ? "established.\n" : "not established.\n");
    bool mesval = app->StartMeasurement();
    std::cout << "Measurement " << (mesval ? "started.\n" : "not started.\n");
    int wait;
    std::cin >> wait; // just to verify manually that the connection has started
    bool stoval = app->StopMeasurement();
    std::cout << "Measurement " << (stoval ? "stopped.\n" : "not stopped.\n");
    app->ShutDown();
    delete app;
    app = nullptr;
    module.Term();
    ::CoUninitialize();
    return 0;
}

But I cannot figure out a single way in which I can see the data inside the tool itself. I thought maybe it would be variables but no, they are system variables and not meant for network signals at all. I thought maybe via Channel object I could make the tool tap into the data but turns out the Channel component interface is not available in CANalyzer - it is meant for transmission of data into CANoe.

Also, I can't find a way to increment the simulation time in the tool as well, because the simulation pointer is not available.

Is there any way in which the above two points can be handled?

My end goal is to watch the Tx and Rx data exchanged between two nodes on a bus getting listed in the trace window of CANalyzer(like CANoe).

0

There are 0 best solutions below