⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 irserialengine.cpp

📁 最新官方例子,图形,描述副,基本控件,通讯协议,等等,
💻 CPP
字号:
/**
*
* @brief Definition of CIrSerialEngine
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

// INCLUDE FILES

// Class include
#include "IrSerialEngine.h"

// System includes
#include <e32def.h>
#include <aknnotewrappers.h>

// Physical device driver names
#if defined (__WINS__)
_LIT (KPddName, "ECDRV");
#else
_LIT (KPddName, "EUART1");
#endif

// Logical device driver names
_LIT (KLddName, "ECOMM");

// Comm Port Name
_LIT (KPortName, "IRCOMM::0");

// Comms modules
_LIT (KIrComm, "IRCOMM");


// ================= MEMBER FUNCTIONS =======================

/**
* Symbian OS 2 phase constructor.
* Constructs the CInfraredEngine using the constructor and the ConstructL
* method.
* @param aPriority of the Active Object
* @param aAppUi for which the CInfraredEngine is constructed
* @return The newly constructed CInfraredEngine
*/
CIrSerialEngine* CIrSerialEngine::NewL(TInt aPriority, CIrSerialAppUi* aAppUi)
    {
    CIrSerialEngine* self = new (ELeave) CIrSerialEngine(aPriority, aAppUi);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(); // self
    return self;
    }

/**
* Destructor.
*
*/
CIrSerialEngine::~CIrSerialEngine()
    {
    Close();
    }

/**
* constructor.
* */
CIrSerialEngine::CIrSerialEngine(TInt aPriority, CIrSerialAppUi* aAppUi)
: CActive(aPriority), iAppUi(aAppUi)
    {
    }

/**
* Symbian OS 2nd phase constructor.
* This adds the InfraredEngine Active Object to the Active Scheduler.
*/
void CIrSerialEngine::ConstructL()
    {
    CActiveScheduler::Add(this);
    }

/**
* Initialise method.
* Loads the physical and logical device drivers.
* Connects to the Serial comms server.
* Loads the CSY module.
* If any steps fail a warning note is displayed.
* */
void CIrSerialEngine::InitialiseL()
    {
    TInt err;

#if defined (__WINS__) // File Server required in WINS to enable loading of device drivers
    RFs fileServer;
    User::LeaveIfError(fileServer.Connect());
    fileServer.Close();
#endif

    // Load the physical device driver.
    err = User::LoadPhysicalDevice(KPddName);
    if (err != KErrNone && err != KErrAlreadyExists)
        {
        User::Leave(err);
        }

    // Load the logical device driver.
    err = User::LoadLogicalDevice(KLddName);
    if (err != KErrNone && err != KErrAlreadyExists)
        {
        User::Leave(err);
        }

    // Start the comms server process
    err = StartC32();
    if (err != KErrNone && err != KErrAlreadyExists)
        {
        User::Leave(err);
        }

    // Connect to the Serial comms server.
    User::LeaveIfError(iCommServer.Connect());

    // Load the CSY module.
    User::LeaveIfError(iCommServer.LoadCommModule(KIrComm));
    }

/**
* Open the serial port. Use Shared access mode.
* */
void CIrSerialEngine::OpenL()
    {
    User::LeaveIfError(iCommPort.Open(iCommServer, KPortName, ECommShared));

    // Check port capabilities
    TCommCaps portCapabilities;
    iCommPort.Caps(portCapabilities);

    if (((portCapabilities().iRate & KCapsBps19200) == 0) ||
        ((portCapabilities().iDataBits & KCapsData8) == 0) ||
        ((portCapabilities().iStopBits & KCapsStop1) == 0) ||
        ((portCapabilities().iParity & KCapsParityNone) == 0))
            User::Leave (KErrNotSupported);

    // Configure port
    TCommConfig portSettings;
    iCommPort.Config(portSettings); // Get current configuration

    // Set port characteristics
    portSettings().iRate = EBps19200;
    portSettings().iParity = EParityNone;
    portSettings().iDataBits = EData8;
    portSettings().iStopBits = EStop1;
    portSettings().iFifo = EFifoEnable;
    portSettings().iHandshake = KConfigObeyXoff|KConfigSendXoff; 
    portSettings().iTerminator[0] = 10;  // line feed character
    portSettings().iTerminatorCount = 1;

    User::LeaveIfError(iCommPort.SetConfig(portSettings));

    // Turn on DTR and RTS
    iCommPort.SetSignals(KSignalDTR, 0);
    iCommPort.SetSignals(KSignalRTS, 0);

    // Set buffer size
    const TInt KBufferLength = 4096;
    iCommPort.SetReceiveBufferLength(KBufferLength);
    }

/**
* The Active object's request method.
* Any pending Write() operations are cancelled.
* The data specified in the aTxData parameter is first copied to the iDataBuf member and then
* written to the serial port.
* @param aTxData is the data to be copied to the local tbuf and then written to the serial port.
* */
void CIrSerialEngine::Transmit(const TDesC& aTxData)
    {
    // Timeout Value
    const TTimeIntervalMicroSeconds32 KTimeOut(4000000);
    Cancel();

    iDataBuf.Copy(aTxData);
    iCommPort.Write(iStatus, KTimeOut, iDataBuf);
    SetActive();
    }

/**
* Close the serial port and then close the session with the Serial comms server.
* */
void CIrSerialEngine::Close()
    {
    iCommPort.Close();
    iCommServer.Close();
    }

/**
* The Active object's request complete event.
* The Close() method is called and the iStatus member inspected to determine if the
* request was successful or not.
* */
void CIrSerialEngine::RunL()
    {
    Close();
    TInt err = iStatus.Int();
    iAppUi->PrintResultDialogL(err);

    }

/**
* Cancel any pending Write() operations.
* */
void CIrSerialEngine::DoCancel()
    {
    iCommPort.WriteCancel();
    }


// End of File

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -