📄 asyncserialengine.cpp
字号:
/*
* ============================================================================
* Name : CAsyncSerialEngine from AsyncSerialEngine.cpp
* Part of : AsyncSerial
* Created : 23.02.2006 by ToBeReplacedByAuthor
* Implementation notes:
* Initial content was generated by Series 60 Application Wizard.
* Version :
* Copyright: ToBeReplacedByCopyright
* ============================================================================
*/
// INCLUDE FILES
#include "AsyncSerialEngine.h"
// Physical device driver names
#if defined (__WINS__)
_LIT (KPddName, "ECDRV");
#else
_LIT (KPddName, "EUART1");
#endif
// Logical device driver name
_LIT (KLddName, "ECOMM");
// Commus port
_LIT(KCommPort, "COMM::0");
// Comms module
_LIT (KCommModule, "ECUART");
// ================= MEMBER FUNCTIONS =======================
// Two-phased constructor.
CAsyncSerialEngine* CAsyncSerialEngine::NewL(MObserver& aObserver)
{
CAsyncSerialEngine* self = new(ELeave) CAsyncSerialEngine(aObserver);
CleanupStack::PushL( self );
self->ConstructL();
CleanupStack::Pop();
return self;
}
// Destructor
CAsyncSerialEngine::~CAsyncSerialEngine()
{
Close();
}
void CAsyncSerialEngine::Receive(TDes8& aData)
{
Cancel();
iComm.ReadOneOrMore(iStatus, aData);
SetActive();
}
// Functions from CActive
void CAsyncSerialEngine::DoCancel()
{
iComm.Cancel();
}
void CAsyncSerialEngine::RunL()
{
iObserver.OnCompleteL(iStatus.Int());
}
CAsyncSerialEngine::CAsyncSerialEngine(MObserver& aObserver)
: CActive(CActive::EPriorityStandard), iObserver(aObserver)
{
}
// ---------------------------------------------------------
// CAsyncSerialEngine::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CAsyncSerialEngine::ConstructL()
{
CActiveScheduler::Add(this);
InitializeL();
OpenL();
ConfigurateL();
}
// New functions
void CAsyncSerialEngine::InitializeL()
{
// Load the physical device driver.
TInt 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(iCommServ.Connect());
// Load the CSY module.
User::LeaveIfError(iCommServ.LoadCommModule(KCommModule));
}
void CAsyncSerialEngine::OpenL()
{
User::LeaveIfError(iComm.Open(iCommServ, KCommPort, ECommShared));
}
void CAsyncSerialEngine::ConfigurateL()
{
// Check port capabilities
TCommCaps commCaps;
iComm.Caps(commCaps);
if (((commCaps().iRate & KCapsBps19200) == 0) ||
((commCaps().iDataBits & KCapsData8) == 0) ||
((commCaps().iStopBits & KCapsStop1) == 0) ||
((commCaps().iParity & KCapsParityNone) == 0))
User::Leave (KErrNotSupported);
// Get current configuration
TCommConfig commConfig;
iComm.Config(commConfig);
// Set port characteristics
commConfig().iRate = EBps19200;
commConfig().iParity = EParityNone;
commConfig().iDataBits = EData8;
commConfig().iStopBits = EStop1;
commConfig().iFifo = EFifoEnable;
commConfig().iHandshake = KConfigObeyXoff|KConfigSendXoff;
commConfig().iTerminator[0] = 10; // line feed character
commConfig().iTerminatorCount = 1;
User::LeaveIfError(iComm.SetConfig(commConfig));
// Turn on DTR and RTS
iComm.SetSignals(KSignalDTR, 0);
iComm.SetSignals(KSignalRTS, 0);
// Set buffer size
const TInt KBufferLength = 32;
iComm.SetReceiveBufferLength(KBufferLength);
}
void CAsyncSerialEngine::Close()
{
Cancel();
iComm.Close();
iCommServ.Close();
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -