Symbian
Symbian OS Library

SYMBIAN OS V9.3

[Index] [Spacer] [Previous] [Next]



IRPrinting: infrared printing


Example code

The source code for this example application can be found in the directory:

examples\SerialComms\ServerClientSide\IRPrinting

These are the main files contained in the examples. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// IRPrinting.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//


#include <e32base.h>
#include <e32test.h>
#include <e32svr.h>
#include <c32comm.h>
#include "f32file.h"

#include "CommonFiles.h"

// Device driver names
#if defined (__WINS__)
_LIT(PDD_NAME,"ECDRV");
_LIT(LDD_NAME,"ECOMM");
#else
_LIT(PDD_NAME,"EUART1");
_LIT(LDD_NAME,"ECOMM");
#endif

_LIT8(DATA_STRING,"Symbian OS infra red printing\n\r");

LOCAL_C void Init();

// Do the example 
LOCAL_C void doExampleL()
    {
    _LIT(KStatus0,"Connect to file server\n");
    _LIT(KStatus1,"Connect to comm server\n");
    _LIT(KStatus2,"Load IrCOMM.CSY\n");
    _LIT(KStatus3,"Open IrCOMM::0\n");
    _LIT(KStatus4,"Write to IrCOMM::0\n");
    _LIT(KStatus5,"Close IrCOMM::0\n");
    _LIT(KStatus6,"Close server connection\n");
    _LIT(KIrCOMM,"IrCOMM");
    _LIT(KIrCOMM0,"IrCOMM::0");

    const TTimeIntervalMicroSeconds32 KTimeOut(4000000);
    //time-out value

    console->Printf(KStatus0);
                // force a link to the file server
                // so that we're sure the loader 
                // will be present

    RFs f;
    User::LeaveIfError(f.Connect());
    f.Close();
                // Initialisation

    Init();
                
    RCommServ server;

                // Connect to the comm server
    console->Printf(KStatus1);
    server.Connect();

                // Load the IrCOMM comm module
                // C32 will automatically search \System\Libs
                // on all drives for IrCOMM.CSY
    console->Printf(KStatus2);
    TInt ret=server.LoadCommModule(KIrCOMM);
    
    //test(ret==KErrNone);
    User::LeaveIfError(ret);
        
    RComm commPort;
                // Open the IrCOMM port unit 0 (the only one supported)
                // Open port in exclusive mode because we don't 
                // have any access control code.
    console->Printf(KStatus3);
    ret=commPort.Open(server,KIrCOMM0,ECommExclusive);
    //test(ret==KErrNone);
    User::LeaveIfError(ret);

    TRequestStatus status;
                // Write to the IrCOMM port - the first write 
                // takes a long time as the IrDA connection is 
                // set up in response to this request. Subsequent 
                // writes to IrCOMM are very fast.
    console->Printf(KStatus4);
    commPort.Write(status,KTimeOut,DATA_STRING);
    User::WaitForRequest(status);

    //test(status.Int()==KErrNone);
    User::LeaveIfError(status.Int());
                // Close port
    console->Printf(KStatus5);
    commPort.Close();

    console->Printf(KStatus6);
    server.Close();
    }


LOCAL_C void Init()
//
// Initialisation code - loads the serial LDD and PDD
// starts the comm subsystem (for EPOC32 builds)
// On a full Symbian OS implementation, this code would not
// be required because higher level GUI components  
// automatically start the services.
//
    {
    // Load the physical device driver
    // The OS will automatically append .PDD and 
    // search /System/Libs on all drives.

    TInt r=User::LoadPhysicalDevice(PDD_NAME);
    if (r != KErrNone && r!= KErrAlreadyExists)
        User::Leave(r);
    //test(r==KErrNone || r==KErrAlreadyExists);
    // Similarly for the Logical device driver
    r=User::LoadLogicalDevice(LDD_NAME);
    if (r != KErrNone && r != KErrAlreadyExists)
        User::Leave(r);
    //test(r==KErrNone|| r==KErrAlreadyExists);

#if defined (__EPOC32__)
    // For EPOC builds we need to start the comms subsystem
    // This call actually starts the comms server process
    r=StartC32();
    if (r != KErrAlreadyExists)
        User::LeaveIfError(r);
#endif
    }

// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

IRPrinting.mmp

// GlassTerm.MMP
//
// Copyright (c) 2000-2005 Symbian Software Ltd.  All rights reserved.

// using relative paths for sourcepath and user includes

TARGET        GlassTerm.exe
TARGETTYPE    exe
UID           0
VENDORID 0x70000001
CAPABILITY   All -TCB

SOURCEPATH    .
SOURCE        GlassTerm.cpp

USERINCLUDE   .
USERINCLUDE   ..\CommonFiles
SYSTEMINCLUDE \Epoc32\include

LIBRARY       euser.lib efsrv.lib c32.lib

// CommonFiles.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.


#ifndef __CommonFiles_H
#define __CommonFiles_H

#include <e32cons.h>

// public
LOCAL_D CConsoleBase* console; // write all your messages to this
LOCAL_C void doExampleL(); // code this function for the real example

// private
LOCAL_C void callExampleL(); // initialize with cleanup stack, then do example

GLDEF_C TInt E32Main() // main function called by E32
    {
    _LIT(KSymbianEx,"SymbianEx");
    CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
    TRAPD(error,callExampleL()); // more initialization, then do example
    __ASSERT_ALWAYS(!error,User::Panic(KSymbianEx,error));
    delete cleanup; // destroy clean-up stack
    return 0; // and return
    }

LOCAL_C void callExampleL() // initialize and call example code under cleanup stack
    {
    _LIT(KStatus1,"Symbian OS Example Code (comms)");
    _LIT(KStatus2,"failed: leave code=%d");
    _LIT(KStatus3,"ok");
    _LIT(KStatus4," [press any key]");

    console=Console::NewL(KStatus1,TSize(KConsFullScreen,KConsFullScreen));
    //console=Console::NewL(KStatus1, TSize(KDefaultConsWidth,KDefaultConsHeight));
    CleanupStack::PushL(console);
    TRAPD(error,doExampleL()); // perform example function
    if (error) console->Printf(KStatus2, error);
    else console->Printf(KStatus3);
    console->Printf(KStatus4);
    console->Getch(); // get and ignore character
    CleanupStack::Pop(); // close console
    }

#endif

[Top]


Description

IRPrinting illustrates the use of the Serial Communications API for infra-red communications, such as to an IrDA-capable printer. See the IrDA Serial Overview for more information.

The example:

The write operation times out with an error after four seconds if it is not successful, as will occur if no receiver is present.

[Top]


Build

The source code for this example application can be found in the directory:

examples\SerialComms\ServerClientSide\IRPrinting

It may be in the directory in which you installed Symbian OS, or it may be in src\common\developerlibrary\. It includes the two project files needed for building: bld.inf and the .mmp file.

The Symbian OS build process describes how to build this application, which results in an executable called \epoc32\release\<target>\<urel or udeb>\IRPRINTING.EXE.

[Top]


Usage

Run the executable IRPRINTING.EXE.

Executables for the emulator targets wins and winscw can be run on your PC. Executables for ARM targets must be copied to your target platform before being run.

[Top]


Classes used