You are here:

ActiveXperts.com > ActiveComport > How to Use ActiveComport > Visual C++ 5.x/6.x

ActiveComport Toolkit Add serial communication capabilities to any Windows or .NET application

Quicklinks


Using the ActiveComport Serial Port Toolkit with Visual C++ 5.x/6.x

ActiveComport is a software development kit (SDK) that enables the user to communicate to a device over a serial interface.

Such a device can be: a weight indicator, a modem, a scanner, or any other device that is equiped with a serial port. It can even be another PC, connected via a NULL modem cable.

ActiveComport features the following:

Direct COM port support (like 'COM1'), TAPI (Windows Telephony Device) support (like 'Standard 56000 bps Modem'), support for RS-232/RS422/RS485, up to 256 simultaneous ports, support for all types of Hayes compatible modems, support for serial cable, USB cable or Bluetooth connections, support for GSM/GPRS modems, support for Virtual COM ports (i.e. COM ports redirected through the network), hardware flow control (RTS/CTS, DTR/DSR), software flowcontrol (XON/XOFF), configurable baudrate/parity/stopbits, full buffered data transfer, text/binary data transfer.

ActiveComport can be well integrated into Visual C++ environments.

This document describes how the ActiveComport Toolkit can be integrated into Visual Studio C++ 5.x/6.x projects.

Step 1: Download and install the ActiveComport Toolkit

Download the ActiveComport Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual C++ project

Launch 'Microsoft Visual C++' from the Start menu, and choose 'New' from the 'File Menu'. The 'New' dialog appears.

Select the type of project (for instance: 'Win32 Console Application'), enter a 'Project name' and select the 'Location':

Visual C++

(Click on the picture to enlarge)

Select the kind of project, for instance a 'Hello, world!' application and click 'Finish':

Visual C++

(Click on the picture to enlarge)

Step 3: Refer to the ActiveComport Library and declare the objects

A new Project is created now.

Before you can use ActiveComport, you need to refer to the ActiveComport library. The actually reference files are shipped with the product and are located in the following directory:

C:\Program Files\ActiveXperts\ActiveComport\Examples\Visual C++\Include

Copy all files in the above directory ('AComport.h', 'AComport_i.c' and 'AComportConstants.h') to your project directory.

On top of your code, declare the following object:

IComPort	*pComPort = NULL;

Step 4: Create the objects

Since the ActiveComport Toolkit is a COM object, you must initialize the COM library before they can call COM library functions (e.g. ActiveComport functions):

CoInitialize(NULL);

Create the object in the following way:

CoCreateInstance(CLSID_ComPort, NULL, CLSCTX_INPROC_SERVER, IID_IComPort, (void**) &pComPort );

Step 5: Send AT commands to a connected Hayes compatible modem

You can now send and/or receive data to and/or from a serial port.

The following code shows how to query modem:

#include <comdef.h>
#include <atlbase.h>
#include <windows.h>
#include <stdio.h>

#include "..\include\AComportConstants.h"
#include "..\include\AComport.h"
#include "..\include\AComport_i.c"


VOID MyReadString( IComPort	*pComPort, DWORD dwMaxMSecs );
VOID MyWriteString( IComPort *pComPort, _bstr_t bstrString );

////////////////////////////////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
	IComPort	*pComPort = NULL;

	LONG		lLastError = 0L;
	LONG		lDeviceCount = 0L;
	LONG		l = 0L;
	
	HRESULT		hr;

	_bstr_t bstrCmd1		= "at&f";
	_bstr_t bstrCmd2		= "ate0";
	_bstr_t bstrCmd3		= "at+fclass=?";
	_bstr_t bstrDev		= "Standard 56000 bps Modem";
	_bstr_t bstrLog		= "C:\\AComport.log";


	BSTR bstrDevice = NULL;

	CoInitialize(NULL);

	hr = CoCreateInstance(CLSID_ComPort, NULL, CLSCTX_INPROC_SERVER, IID_IComPort, (void**) &pComPort );
	
	if( ! SUCCEEDED( hr ) )
	{
		pComPort = NULL;
		printf( "Unable to create instance of the object.\n" );
		goto _EndMain;
	}

	
	pComPort->GetDeviceCount ( &lDeviceCount );

	for ( l = 0; l < lDeviceCount;l ++ )
	{
		pComPort->GetDevice ( l, &bstrDevice );

		printf ( "%ls\n" , bstrDevice );
	}

	
	// Open port
	pComPort->put_Device ( bstrDev );
	pComPort->put_LogFile ( bstrLog );
	pComPort->put_BaudRate( 9600 );					// 9600 bps
	pComPort->Open();
	pComPort->get_LastError( &lLastError );

	if( lLastError != 0 )
	{
		BSTR pErrorDescription		= NULL;
		pComPort->GetErrorDescription( lLastError, &pErrorDescription );
		wprintf( L"Open port failed, error #%d : %s\n", lLastError, ( LPCWSTR ) pErrorDescription );
		SysFreeString( pErrorDescription );
		goto _EndMain;
	}

	printf( "Port opened successfully\n" );

	// Write AT&F
	MyWriteString( pComPort, bstrCmd1 );
	MyReadString( pComPort, 2000 );

	// Write ATE0
	MyWriteString( pComPort, bstrCmd2 );
	MyReadString( pComPort, 8000 );

	// Write AT+FCLASS=?
	MyWriteString( pComPort, bstrCmd3 );
	MyReadString( pComPort, 8000 );

	// Close the port
	pComPort->Close();

	printf( "Ready.\n" );

_EndMain:

	if( pComPort != NULL )	pComPort->Release();

	return 0;
}

////////////////////////////////////////////////////////////////////////////////

VOID MyReadString( IComPort *pComPort, DWORD dwMaxMSecs )
{
	DWORD	dwStartTime = GetTickCount();
	BSTR	bstrTemp;
	_bstr_t bstrInputString;
	BOOL	bSomethingRead = FALSE;
	INT		lenInputString;

	do
	{
		Sleep( 200 );
		pComPort->ReadString( &bstrTemp );

		bstrInputString = ( LPCWSTR ) bstrTemp;
		lenInputString	= lstrlen( bstrInputString.operator char *() );

		printf( "<- '%s'\n", bstrInputString.operator char *() );
		SysFreeString( bstrTemp );

		bSomethingRead = lenInputString > 0 ? TRUE : bSomethingRead;

	} 
	while( lenInputString != 0 || ( !bSomethingRead && GetTickCount() < dwStartTime + dwMaxMSecs ) );
}

////////////////////////////////////////////////////////////////////////////////

VOID MyWriteString( IComPort *pComPort, _bstr_t bstrString )
{
	pComPort->WriteString( bstrString );
	printf( "-> %s\n", bstrString.operator char *() );
}

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/acomport.