Shortcut Menu

Skip

Main Navigation

Choose your language


ActiveXperts Serial Port Component Add serial communication capabilities to any Windows or .NET application

Quicklinks


Using the ActiveXperts Serial Port Component with Visual C++

ActiveXperts Serial Port Component 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.

ActiveXperts Serial Port Component 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 as well as USB cable or Bluetooth connections;
  • Support for Virtual COM ports (i.e. COM ports redirected through the network);
  • Hardware flow control (RTS/CTS, DTR/DSR) and software flowcontrol (XON/XOFF) support;
  • Configurable baudrate/parity/stopbits, full buffered data transfer, text/binary data transfer.

Step 1: Download and install the ActiveXperts Serial Port Component

Download the ActiveXperts Serial Port Component 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 ActiveXperts Serial Port Component Library and declare the objects

A new Project is created now.

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

C:\Program Files\ActiveXperts\Serial Port Component\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 ActiveXperts Serial Port Component is a COM object, you must initialize the COM library before they can call COM library functions (e.g. Serial Port Component 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/serial-port-component.