This guide shows how to use Gurux Serial component
(GXSerial) in the Visual C++ environment (C++ from now on). Source code is available at sourceforge.net
Adding GXSerial component to the project
- Start Visual C++
- Open the project where you want to add the GXSerial component, or create a new one
- Select Options from the Tools menu
- On Directories tab, select Executable Files to Show directories for:
- 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 "GuruxSerial.dll" raw_interfaces_only using namespace GuruxSerial; #include <afxctl.h> // Needed for AfxConnectionAdvise
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<IGXSerial2> GXSerial1;
// Create Media Component
HRESULT hr = ::CoCreateInstance(__uuidof(GXSerial), 0, CLSCTX_INPROC_SERVER,
__uuidof(IGXSerial2), (void**) &GXSerial1);
/////////////////////////////////////////////////////////////
// Establish a connection between source and sink.
// m_dwCookie is a cookie identifying the connection, and is needed
// to terminate the connection.
LPUNKNOWN pUnk = GetIDispatch(FALSE);
if (!AfxConnectionAdvise(m_GXSerial1, __uuidof(IGXSerialEvents), pUnk, FALSE, &m_dwCookie))
{
// Error.
MessageBox("AfxConnectionAdvise Failed", "Gurux Media Sample", MB_OK | MB_ICONSTOP);
return FALSE;
}
Setting Media Properties
You can either select appropriate settings in the Properties dialog or edit the settings programmatically.
//Set port hr = m_GXSerial1->put_Port(L"Com1"); //Make Error check hr = GXSerial1->Open(); //Make Error check
Sending and receiving packets
All data is sent asynchronously. Response is received as a Received event message.
Receiving event messages
To receive event messages, notify interface must be created. Add following code to header
file where notify interface is implemented.
//Add support for GXMedia component notifies DECLARE_DISPATCH_MAP() DECLARE_INTERFACE_MAP()
Add following code to the .cpp file where notify interface is implemented.
//Add support for GXMedia component notifies
BEGIN_INTERFACE_MAP(CGuruxMediaSampleDlg, CDialog)
INTERFACE_PART(CGuruxMediaSampleDlg, __uuidof(IGXSerialEvents), Dispatch)
END_INTERFACE_MAP()
BEGIN_DISPATCH_MAP(CGuruxMediaSampleDlg, CCmdTarget)
DISP_FUNCTION_ID(CGuruxMediaSampleDlg, "Received", 1, OnReceived, VT_EMPTY, VTS_PVARIANT VTS_WBSTR)
DISP_FUNCTION_ID(CGuruxMediaSampleDlg, "OnError", 2, OnError, VT_EMPTY, VTS_WBSTR)
END_DISPATCH_MAP()
/////////////////////////////////////////////////////////////////////////////
//Handle received data
/////////////////////////////////////////////////////////////////////////////
HRESULT CGuruxMediaSampleDlg::Received(VARIANT* Data, BSTR SenderInfo)
{
// Get Data size
long lSize = 0;
if(FAILED(SafeArrayGetUBound(Data->parray, 1, &lSize)))
{
return S_OK;
}
CComVariant data;
for (long pos = 0; pos < lSize; pos++)
{
if (FAILED(SafeArrayGetElement(Data->parray, &pos, &data)))
{
return S_OK;
}
m_dataReceived += data.bVal;
}
m_sender = SenderInfo;
UpdateData(FALSE);
return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
// Show occurred error
/////////////////////////////////////////////////////////////////////////////
HRESULT CGuruxMediaSampleDlg::OnError(BSTR ErrorInfo)
{
USES_CONVERSION;
MessageBox(W2T(ErrorInfo), "Gurux Media Sample", MB_OK | MB_ICONERROR);
return S_OK;
}
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 Serial 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.
CString 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;
}
////////////////////////////////////////////////////////////////////////////////
// Create Media Components
void CreateComponents()
{
if(FAILED(::CoCreateInstance(__uuidof(GXSerial), 0,
CLSCTX_INPROC_SERVER,
__uuidof(IGXSerial),
(void**) &m_GXSerial1)))
{
//Show Error if any
MessageBox(GetGXError(m_GXSerial1), "Gurux Media Sample", MB_OK | MB_ICONERROR);
}
/////////////////////////////////////////////////////////////
//Establish a connection between source and sink.
//m_dwCookie is a cookie identifying the connection, and is needed
//to terminate the connection.
LPUNKNOWN pUnk = GetIDispatch(FALSE);
if (!AfxConnectionAdvise(m_GXSerial1, __uuidof(IGXSerialEvents), pUnk, FALSE, &m_dwCookie))
{
// Error. MessageBox("AfxConnectionAdvise Failed", "Gurux Media Sample", MB_OK | MB_ICONSTOP);
return FALSE;
}
}
////////////////////////////////////////////////////////////////////////////////
// Close Media Components
void CloseComponents()
{
LPUNKNOWN pUnk = GetIDispatch(FALSE);
/////////////////////////////////////////////////////////////
//Release sink.
//Terminate a connection between source and sink.
//m_pUnkSrc is IUnknown of server obtained by CoCreateInstance().
//m_dwCookie is a value obtained through AfxConnectionAdvise().
if (!AfxConnectionUnadvise(m_GXSerial1 , __uuidof(IGXSerialEvents), pUnk, FALSE, m_dwCookie))
{
// Error.
MessageBox("AfxConnectionAdvise Failed", "CPP Sample", MB_OK | MB_ICONSTOP);
}
//Release Components when application is closing.
m_GXSerial1 = NULL;
CoUninitialize();
}
Using the components from development environment
Using from .NET | Using from Visual Basic | Using from C++ | Parameter Types