×
Namespaces

Variants
Actions

Find IR Ports in S60

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: jimgilmour1 (12 Apr 2007)
Last edited: hamishwillee (31 May 2013)

Contents

Introduction

The purpose of this code is to demonstrate how to use the IR port and therefore the format of code is not as good as commercial code.

Description of Infrared Port

The Infrared port in Symbian S60. The port is used by selecting the port

_LIT(KIrDAPort, "IRCOMM::00");

This is used in the example below. To create the template in Carbide C++ IDE and S60 Platform SDK 3rd Edition FP1 and new "S60 Symbian OS C++ Project", call this project "FindPorts". Select "S60 3.x GUI Application", this will create the new project.

The additional include is FindPortsAppUi.cpp

#include <c32comm.h>

in FindPorts.mmp add the following libary to the section

LIBRARY c32.lib // Added for Serial port support

Modified code

Int errDev;
TInt errCom;
RCommServ IRcommServer; //serial comms
RComm IRcommPort; // comm port


// Define the logical device driver name

// Device driver names
_LIT(KLddName, "ECOMM");

//Physical driver names
_LIT(KPddName, "EUART1");

// Communication modules
_LIT(KRS232, "ECUART");
_LIT(KIrDA, "IRCOMM");
_LIT(KCommPort, "COMM::0");

_LIT(KIrDAPort, "IRCOMM::00");

//Load Physical Device driver
errDev = User::LoadPhysicalDevice(KPddName);
if ((errDev != KErrNone) && (errDev != KErrAlreadyExists)) User::Leave (errDev);

//Load Logical Device driver
errDev = User::LoadLogicalDevice(KLddName);
if ((errDev != KErrNone) && (errDev != KErrAlreadyExists)) User::Leave (errDev);

// starting communication server does not need checking as this will not error if already running
StartC32();
//connect to the comm server
User::LeaveIfError(IRcommServer.Connect());

errCom = IRcommServer.LoadCommModule(KIrDA);
//Load the CSY module

User::LeaveIfError(IRcommServer.LoadCommModule(KIrDAPort));

TSerialInfo portInfo;

User::LeaveIfError(IRcommServer.GetPortInfo(KIrDAPort,portInfo));

_LIT(KColons,"::");
TBuf<KMaxPortName + 4> portName;
portName.Append(portInfo.iName);

TInt errCom = IRcommPort.Open(IRcommServer, KCommPort, ECommExclusive);
if ((errCom != KErrNone) )
{
 IRcommServer.Close();
 User::Leave(errCom);
}

The full modified file FindPortsAppUi.cpp

/*
============================================================================
Name        : FindPortsAppUi.cpp
Author      : 
Copyright   : Your copyright notice
Description : CFindPortsAppUi implementation
============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <FindPorts.rsg>
#include <f32file.h>
#include <s32file.h>

#include <c32comm.h>

#include "FindPorts.pan"
#include "FindPortsAppUi.h"
#include "FindPortsAppView.h"
#include "FindPorts.hrh"
_LIT( KFileName, "C:\\private\\EC4B9E96\\FindPorts.txt" );
_LIT( KText, "Hello World finding port");
// ============================ MEMBER FUNCTIONS     ===============================


// -----------------------------------------------------------------------------
// CFindPortsAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CFindPortsAppUi::ConstructL()
   {
   // Initialise app UI with standard value.
   BaseConstructL();

	// Create view object
	iAppView = CFindPortsAppView::NewL( ClientRect() );

	// Create a file to write the text to
	RFs fsSession;
	User::LeaveIfError(fsSession.Connect());
	CleanupClosePushL( fsSession );

	TInt err = fsSession.MkDirAll(KFileName);
	if ( (KErrNone != err) && (KErrAlreadyExists != err) )
		{
		CleanupStack::PopAndDestroy(1); //  fsSession
		return;
		}
	
	RFile file;
	err = file.Replace(fsSession, KFileName, EFileWrite );
	CleanupClosePushL( file );
	if ( KErrNone != err )
		{
		CleanupStack::PopAndDestroy(2); // file, fsSession
		return;
		}
	
	RFileWriteStream outputFileStream( file );
	CleanupClosePushL( outputFileStream );
	outputFileStream << KText;

	CleanupStack::PopAndDestroy(3); // outputFileStream, file, fsSession

	}
// -----------------------------------------------------------------------------
// CFindPortsAppUi::CFindPortsAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CFindPortsAppUi::CFindPortsAppUi()
   {
   // No implementation required
   }

// -----------------------------------------------------------------------------
// CFindPortsAppUi::~CFindPortsAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CFindPortsAppUi::~CFindPortsAppUi()
   {
   if ( iAppView )
       {
       delete iAppView;
       iAppView = NULL;
       }

   }
// -----------------------------------------------------------------------------
// CFindPortsAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//


void CFindPortsAppUi::HandleCommandL( TInt aCommand )
   {
   switch( aCommand )
       {
       case EEikCmdExit:
       case EAknSoftkeyExit:
           Exit();
           break;

       case ECommand1:
           {
 


Int errDev;
TInt errCom;
RCommServ IRcommServer; //serial comms
RComm IRcommPort; // comm port


// Define the logical device driver name

// Device driver names
_LIT(KLddName, "ECOMM");

//Physical driver names
_LIT(KPddName, "EUART1");

// Communication modules
_LIT(KRS232, "ECUART");
_LIT(KIrDA, "IRCOMM");
_LIT(KCommPort, "COMM::0");

_LIT(KIrDAPort, "IRCOMM::00");

//Load Physical Device driver
errDev = User::LoadPhysicalDevice(KPddName);
if ((errDev != KErrNone) && (errDev != KErrAlreadyExists)) User::Leave (errDev);

//Load Logical Device driver
errDev = User::LoadLogicalDevice(KLddName);
if ((errDev != KErrNone) && (errDev != KErrAlreadyExists)) User::Leave (errDev);

// starting communication server does not need checking as this will not error if already running
StartC32();
//connect to the comm server
User::LeaveIfError(IRcommServer.Connect());

errCom = IRcommServer.LoadCommModule(KIrDA);
//Load the CSY module

User::LeaveIfError(IRcommServer.LoadCommModule(KIrDAPort));

TSerialInfo portInfo;

User::LeaveIfError(IRcommServer.GetPortInfo(KIrDAPort,portInfo));

_LIT(KColons,"::");
TBuf<KMaxPortName + 4> portName;
portName.Append(portInfo.iName);

TInt errCom = IRcommPort.Open(IRcommServer, KCommPort, ECommExclusive);
if ((errCom != KErrNone) )
{
 IRcommServer.Close();
 User::Leave(errCom);
}
 //---- End of code -----------------------------------------------------          
           // Load a string from the resource file and display it
           HBufC* textResource = StringLoader::LoadLC( R_COMMAND1_TEXT );
           CAknInformationNote* informationNote;

           informationNote = new ( ELeave ) CAknInformationNote;

           // Show the information Note with
           // textResource loaded with StringLoader.
           informationNote->ExecuteLD( *textResource);

           // Pop HBuf from CleanUpStack and Destroy it.
           CleanupStack::PopAndDestroy( textResource );
           }
           break;
		case ECommand2:
			{
			
			RFs fsSession;
			RFile rFile;
			
			// Connects a client process to the fileserver
			User::LeaveIfError(fsSession.Connect());
			CleanupClosePushL(fsSession);
			
			//Open file where the stream text is
			User::LeaveIfError(rFile.Open(fsSession,KFileName, EFileStreamText));//EFileShareReadersOnly));// EFileStreamText));
			CleanupClosePushL(rFile);
			
			// copy stream from file to RFileStream object
			RFileReadStream inputFileStream(rFile);
    		CleanupClosePushL(inputFileStream);
   		
   		// HBufC descriptor is created from the RFileStream object.
   		HBufC* fileData = HBufC::NewLC(inputFileStream, 32);

           CAknInformationNote* informationNote;

           informationNote = new ( ELeave ) CAknInformationNote;
           // Show the information Note
           informationNote->ExecuteLD( *fileData);			
			
			// Pop loaded resources from the cleanup stack
			CleanupStack::PopAndDestroy(4); // filedata, inputFileStream, rFile, fsSession
			fsSession.Close();
			}
			break;
       default:
           Panic( EFindPortsUi );
           break;
       }
   }
This page was last modified on 31 May 2013, at 02:34.
49 page views in the last 30 days.
×