WinCE下调试RS485串口有感

 
WinCE下调试RS485串口有感
(串口类库实现的源程序在: http://download.csdn.net/detail/dijkstar/4072407 下载,2012-02-16添加;使用微软串口控件mscomm实现的源程序在: http://download.csdn.net/detail/dijkstar/4199667下载,2012-04-04添加)

此篇是《RS485半双工软件编程实现》的继续。
前面说过,RS485是半双工协议,有的工控主板厂家在做RS485口时,完整的实现了自动RTS,则我们编程人员不用关心底层硬件变化,就像平时使用RS232串口一样使用,这包括我们到电子市场买的232转422或485的“博世头”,都内部自动实现了自动RTS;有的厂家没有实现自动RTS,则需要我们自己实现。前面也说过,在WIN32下,只需要关心 DCB数据结构的 fRtsControl成员,将其值改为RTS_CONTROL_TOGGLE即可,如下面程序:
初始化部分:
    hCom=CreateFile("COM1",//COM1
         GENERIC_READ|GENERIC_WRITE,// 允许读和写
         0, // 独占方式
         NULL,
         OPEN_EXISTING, // 打开而不是创建
         0, // 同步方式
         NULL);
    if(hCom==(HANDLE)-1)
    {
         AfxMessageBox(" 打开COM失败!" );
         return FALSE;
    }
 
    SetupComm(hCom,100,100); // 输入缓冲区和输出缓冲区的大小都是
 
    COMMTIMEOUTS TimeOuts;
    // 设定读超时
    TimeOuts.ReadIntervalTimeout=MAXDWORD;
    TimeOuts.ReadTotalTimeoutMultiplier=0;
    TimeOuts.ReadTotalTimeoutConstant=0;
    // 在读一次输入缓冲区的内容后读操作就立即返回,
    // 而不管是否读入了要求的字符。
 
 
    // 设定写超时
    TimeOuts.WriteTotalTimeoutMultiplier=100;
    TimeOuts.WriteTotalTimeoutConstant=500;
    SetCommTimeouts(hCom,&TimeOuts);// 设置超时
 
    DCB dcb;
    GetCommState(hCom,&dcb);
    dcb.BaudRate=9600; // 波特率为
    dcb.ByteSize=8; // 每个字节有位
    dcb.Parity=NOPARITY; // 无奇偶校验位
    dcb.StopBits=TWOSTOPBITS; // 两个停止位
 
    dcb.fRtsControl = RTS_CONTROL_TOGGLE;
 
    SetCommState(hCom,&dcb);
 
    PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
写部分:
    char lpOutBuffer[7];
    memset(lpOutBuffer,'/0',7);// 前个字节先清零
    lpOutBuffer[0]='/x11'// 发送缓冲区的第个字节为DC1
    lpOutBuffer[1]='0'// 第个字节为字符(30H)
    lpOutBuffer[2]='0';// 第个字节为字符(30H)
    lpOutBuffer[3]='1';// 第个字节为字符(31H)
    lpOutBuffer[4]='0';// 第个字节为字符(30H)
    lpOutBuffer[5]='1';// 第个字节为字符(31H)
    lpOutBuffer[6]='/x03';// 第个字节为字符ETX
    // 从该段代码可以看出,仪表的通讯地址为
   
    DWORD dwBytesWrite=7;
    COMSTAT ComStat;
    DWORD dwErrorFlags;
    BOOL bWriteStat;
    ClearCommError(hCom,&dwErrorFlags,&ComStat);
    bWriteStat=WriteFile(hCom,lpOutBuffer,dwBytesWrite,& dwBytesWrite,NULL);
    if(!bWriteStat)
    {
         AfxMessageBox(" 写串口失败!" );
    }
读部分:
    char str[100];
    memset(str,'/0',100);
    DWORD wCount=100;// 读取的字节数
    BOOL bReadStat;
    bReadStat=ReadFile(hCom,str,wCount,&wCount,NULL);
    if(!bReadStat)
         AfxMessageBox(" 读串口失败!" );
    PurgeComm(hCom, PURGE_TXABORT|
         PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
    m_disp=str;
    UpdateData(FALSE);
上述代码在XP下通过,但是同样程序移植到WinCE下,发送出去的内容的最有一个字节总是0XFF,在MSDN上找了篇文章,解释了原因,如下:
PRB: Using SetCommState() to Toggle DTR/RTS for Flow Control
ID: Q99861

The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) 3.1
SYMPTOMS
When SetCommState() is used to raise the DTR andRTS lines for hardware flow control, the first several incoming characters may be lost.
CAUSE
SetCommState() calls the communications driver's setcom function, which calls $SETCOM. $SETCOM disables interrupts from the specified port by clearing the UART's interrupt enable register (IER). After changing the state of the UART and the DTR and RTS lines, $SETCOM delays while interrupts from the UART are still disabled. In Windows 3.1, this delay is approximately 200 milliseconds; in Windows 3.0, the delay is approximately 55 milliseconds. If the DTR andRTS lines are raised from low to high, any characters that arrive before interrupts from the UART are enabled will be lost.
RESOLUTION
SetCommState() should not be used to toggle the states of the DTR andRTS lines for hardware flow control.

Use EscapeCommFunction() to toggle the the states of the DTR and RTS lines because it does not delay while interrupts are disabled.
Additional query words: 3.00 3.10 comm
Keywords : kb16bitonly
Version : WINDOWS:3.1
Platform : WINDOWS
Issue type :
里面大致意思是使用:
dcb.fRtsControl = RTS_CONTROL_TOGGLE;
SetCommState(hCom,&dcb);
有问题,需要使用:
EscapeCommFunction( hCom, CLRRTS) 来清除RTS位,或 EscapeCommFunction( hCom, SETRTS)来设置RTS位。
       考虑上述程序结构,可以在发送之前总是“SETRTS”(即为允许发送状态),发送完毕后“CLRRTS”(即设为允许接收状态)。如在程序初始化中添加:
    hCom=CreateFile("COM1",//COM1
         GENERIC_READ|GENERIC_WRITE,// 允许读和写
         0, // 独占方式
         NULL,
         OPEN_EXISTING, // 打开而不是创建
         0, // 同步方式
         NULL);
    if(hCom==(HANDLE)-1)
    {
         AfxMessageBox(" 打开COM失败!" );
         return FALSE;
    }
 
    SetupComm(hCom,100,100); // 输入缓冲区和输出缓冲区的大小都是
 
    COMMTIMEOUTS TimeOuts;
    // 设定读超时
    TimeOuts.ReadIntervalTimeout=MAXDWORD;
    TimeOuts.ReadTotalTimeoutMultiplier=0;
    TimeOuts.ReadTotalTimeoutConstant=0;
    // 在读一次输入缓冲区的内容后读操作就立即返回,
    // 而不管是否读入了要求的字符。
 
 
    // 设定写超时
    TimeOuts.WriteTotalTimeoutMultiplier=100;
    TimeOuts.WriteTotalTimeoutConstant=500;
    SetCommTimeouts(hCom,&TimeOuts);// 设置超时
 
    DCB dcb;
    GetCommState(hCom,&dcb);
    dcb.BaudRate=9600; // 波特率为
    dcb.ByteSize=8; // 每个字节有位
    dcb.Parity=NOPARITY; // 无奇偶校验位
    dcb.StopBits=TWOSTOPBITS; // 两个停止位
 
// dcb.fRtsControl = RTS_CONTROL_TOGGLE;
 
    SetCommState(hCom,&dcb);
 
    PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
 
    if(!EscapeCommFunction(hCom,CLRRTS))
    {
         AfxMessageBox(" 不能设置CLRRTS" );
    }
写部分中:
    if(!EscapeCommFunction(hCom,SETRTS))
    {
         AfxMessageBox(" 不能设置SETRTS" );
    }
 
    char lpOutBuffer[7];
    memset(lpOutBuffer,'/0',7);// 前个字节先清零
    lpOutBuffer[0]='/x11'// 发送缓冲区的第个字节为DC1
    lpOutBuffer[1]='0'// 第个字节为字符(30H)
    lpOutBuffer[2]='0';// 第个字节为字符(30H)
    lpOutBuffer[3]='1';// 第个字节为字符(31H)
    lpOutBuffer[4]='0';// 第个字节为字符(30H)
    lpOutBuffer[5]='1';// 第个字节为字符(31H)
    lpOutBuffer[6]='/x03';// 第个字节为字符ETX
    // 从该段代码可以看出,仪表的通讯地址为
   
    DWORD dwBytesWrite=7;
    COMSTAT ComStat;
    DWORD dwErrorFlags;
    BOOL bWriteStat;
    ClearCommError(hCom,&dwErrorFlags,&ComStat);
    bWriteStat=WriteFile(hCom,lpOutBuffer,dwBytesWrite,& dwBytesWrite,NULL);
    if(!bWriteStat)
    {
         AfxMessageBox(" 写串口失败!" );
    }
    Sleep(10);
    if(!EscapeCommFunction(hCom,CLRRTS))
    {
         AfxMessageBox(" 不能设置CLRRTS" );
    }
读部分不用改动,写动作中的Sleep功能是调试中试出的。
       经过上述改动,WinCE下的串口程序可发可收。
 
 
 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值