Symbian串口通信——设备上USB串口通信

 在模拟器上的串口通信这里不再说了,SDK中的Example里有实例程序。我最开始是学习《基于Symbian OS的手机开发与应用实践》这本书,但这本书对应的SDK是2.0版本的,3.0原理上是相同的,但是驱动程序模块名称不同。

 

参考Nokia开发论坛上的文章http://wiki.forum.nokia.com/index.php/TSS000601_-_Serial_communication_over_USB_on_S60_3rd_Edition_devices 。内容如下:

 

The RComm API can be used to transfer data over USB serial connection. On S60 3rd Edition, loading of physical (PDD) and logical (LDD) device drivers is not required. Only the CSY module ECACM should be loaded.
The port name available for USB communication is usually "ACM::1". Some devices may support multiple ACM ports. The first port (ACM::0) is reserved for fax/modem process, but ACM::1 and onwards can be used in other applications.
A simplified example for initializing the serial port:
    _LIT(CSYMOD, "ECACM");
    _LIT(KACMPort1, "ACM::1");
    // RComm is a client to the RCommServ Comms server
    // Start this service before any connections are made.
    TInt ret = StartC32();
    if ( ret != KErrNone && ret != KErrAlreadyExists )
        {
        User::Leave ( ret );
        }
    // Connect to CommServer
    RCommServ server;
    User::LeaveIfError( server.Connect() );
    // Load CSY Module
    User::LeaveIfError( server.LoadCommModule( CSYMOD ) );
    TBuf16<KMaxPortName> portName;
    portName.Copy( KACMPort1 );
    // Open the comm. port
    RComm commPort;
    User::LeaveIfError( commPort.Open( server, portName, ECommShared ) );
    // Verify capabilities of the port and configure it
    TCommCaps portCaps;
    commPort.Caps( portCaps );
    if (((portCaps().iRate & KCapsBps115200) == 0) |
        ((portCaps().iDataBits & KCapsData8) == 0) |
        ((portCaps().iStopBits & KCapsStop1) == 0) |
        ((portCaps().iParity & KCapsParityNone) == 0))
       {
       User::Leave( KErrNotSupported );
       }
    TCommConfig portCfg;
    commPort.Config( portCfg );
    portCfg().iRate     =   EBps115200;
    portCfg().iParity   =   EParityNone;
    portCfg().iDataBits =   EData8;
    portCfg().iStopBits =   EStop1;
    portCfg().iHandshake =  0;
    User::LeaveIfError( commPort.SetConfig( portCfg ) );
After this you can read from and write to the port. An application listening to the port should be running on the host (PC).

 

 

 上面这段代码可以直接使用,运行没有问题,我总结一下自己在开发中遇到的问题。

1.  在执行到RComm::Open函数时,系统总是返回“由于安全问题,无法执行此文件”。

    虽然在论坛里没有找到正确的解决办法,但是也了解到很多知识。比如程序签名的Capability。有关与Capability,不在此文中赘述。

    分析可能原因:

    1) 加载的CSY模块不正确。我加载的是“ECUART”

    2) 打开的端口不正确。我打开的端口名称是“COMM::0”

    3) 需要LocalServices的Capability,这个在自签名证书中支持。

 

2.  初始化串口通信成功,但是Send失败,程序退出。

    失败分为两种情况:

    第一种: Device上程序退出

    第二种: PC端的调试程序收不到数据。(我使用超级终端调试,监听端口是PC Suit生成的COM口,我机器是COM3)

    第一种情况产生的原因可能有:

    1) 程序中代码有问题,注意RComm对象。

    2) 我们在使用设备调试时,都需要启动TRK程序,但是TRK程序如果连接时如果使用USB方式,也就占用了Device上的串口,所以要把Trk程序Disconnect。(其实程序在初始化串口时,LoadPhysicalDevice函数会返回KErrAlreadyExists。)

    第二种情况产生的原因:串口通信两端的程序配置的参数要完全一样,自己设置一下就OK了。

 

    就说这么多,以后有新的理解再补充。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值