Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

串口通信

From Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 27 Sep 2009 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

串口通讯是一个底层的,点对点的技术,用来在两台手机间传输数据,特别是近距离范围。S60支持通过红外和蓝牙进行串口通讯。对S60来说是用串口通讯服务来完成的(也被称为Comms Server或C32)。这个使用Symbian OS客户端/服务器模式来提供串口硬件的访问,并且它更具通用信(也就是说,同样的API可以为红外和蓝牙串口通信服务,注意不同的硬件可以通过额外的插件支持),而且也须有共享性(换句话说,几个客户端线程可以更加安全的同时使用同一个串口)


Contents

S60上所有的串口通信都使用如下步骤完成:

  • 加载串口设备驱动
  • 开启Comms Server
  • 连接Comms Server
  • 加载一个comms模块(通常也叫CSY)这是Comms Server插件,它决定你使用串口的类型(如红外还是蓝牙)
  • 打开串口
  • 配置串口
  • 向串口写输入/读取数据
  • 最后关闭串口

一个红外串口通信的示例

所需重要的类:

RCommServ // serial comms

RComm // comm port

TBuf8<64> Data // data to transmit

第一部分

  • 加载物理和逻辑设备的驱动
  • 连接到Serial comms server
  • 加载CSY模块


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

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);
 }



第三部分

  • 写数据到串口




const TTimeIntervalMicroSeconds32 KTimeOut(4000000);
iCommPort.Write(iStatus, KTimeOut, Data);
This page was last modified on 15 December 2011, at 06:00.
377 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×