GXDN: Gurux Developer Network
Gurux DLMS/COSEM component
This guide shows you how to use Gurux DLMS/COSEM component in the Visual C++ environment (C++ from now on).
Adding DLMS/COSEM component to the project
  1. Start Visual C++
  2. Open the project where you want to add the DLMS/COSEM component, or create a new one
  3. Select Options from the Tools menu
  4. On Directories tab, select Executable Files to Show directories for:
  5. Add new directory to Directories list. New path is "C:\Program Files\Common Files\Gurux\GXCom"


Select StdAfx.h file and add following lines to the bottom of the file.
#include <atlbase.h>
#import "GuruxDLMS.dll" raw_interfaces_only
using namespace GuruxDLMS;
        
Now Gurux components are ready to be used from C++.


Creating components from C++
Before a new component can be used in C++, it must be created as follows:
CComPtr<IGXCOSEM> cosem1;
  // Create DLMS/COSEM Component
  HRESULT hr = ::CoCreateInstance(__uuidof(GXCOSEM), 0, CLSCTX_INPROC_SERVER,
  __uuidof(IGXCOSEM), (void**) &cosem1);
Setting properties
  //Set client and server IDs. Server and client identifier values depend from manufacturer.
  CComVariant clientID = (BYTE) 3;
  CComVariant serverID = (BYTE) 24;
  hr = cosem1->put_ClientID(clientID);
  //Make Error check
  hr = cosem1->put_ServerID(serverID);
  //Make Error check
  //Server do not support IEC 62056-47 standard or serial port connection is used.
  hr = cosem1->put_IntefaceType(GXDLMS_INTERFACETYPE_GENERAL);
  //Make Error check
Error Handling
It's very important to consider error handling when sending and receiving data. The connection can drop down, or something else unexpected may happen. If something unwanted happens, Gurux DLMS/COSEM component returns error. The following example shows how to handle errors in C++.
////////////////////////////////////////////////////////////////////////////////
// This method helps you to see in text-form what error GXCom returned.
BSTR GetGXError(IUnknown* pUnknown)
{
    CString errStr;
    if (pUnknown == NULL)
    {
        return errStr;
    }
    CComPtr<ISupportErrorInfo> pSupportErrorInfo = NULL;
    CComPtr<IErrorInfo> pErrorInfo = NULL;
    HRESULT hr = pUnknown->QueryInterface(IID_ISupportErrorInfo, (void**) &pSupportErrorInfo);
    if (FAILED(hr))
    {
        return errStr;
    }
    
    //If interface doesn't support error info don't go further.
    if (FAILED(pSupportErrorInfo->InterfaceSupportsErrorInfo(IID_IUnknown)))
    {
        return errStr;
    }
    
    // Get the current error object. Return if no error object exists.
    GetErrorInfo(0,&pErrorInfo);
    if (pErrorInfo == NULL) 
    {
        return errStr;
    }
            
    //Get error description
    CComBSTR err;
    pErrorInfo->GetDescription(&err);
    errStr = err;
    return errStr;
}

Using the components from development environment

Using from .NET | Using from Visual Basic | Using from C++