hyw11002233
破晓669
2016-10-22 02:51
采纳率: 0%
浏览 1.8k

window串口和Linux串口通信

问题:我这边连续发送多帧数据时,window串口总是会报告读超时?下面是我写的串口代码麻烦各位高手帮忙看看,谢谢!特别是在清空缓冲区,到底是个什么顺序比较合理。

COMMTIMEOUTS TimeOuts; //超时参数设置
//设定读超时
TimeOuts.ReadIntervalTimeout = 6;
TimeOuts.ReadTotalTimeoutMultiplier = 9;
TimeOuts.ReadTotalTimeoutConstant = time_set;//5S

//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier = 100;
TimeOuts.WriteTotalTimeoutConstant   = 500;
SetCommTimeouts(hcomhand,&TimeOuts); //设置超时  ms为单位

//一字节读串口
int serial::readcombyte(uchar *data)
{
unsigned long fact_RDdata_count = 0; // 实际读到的数据长度
bool bReadStat;

if( isopen( ) )
{
    //PurgeComm(hcomhand, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

    bReadStat = ReadFile(hcomhand, data, 1,&fact_RDdata_count, NULL);
    if (!bReadStat || (fact_RDdata_count != 1))
    {
        return ErrCode_RandLenErr;
    }
    else
    {
        //PurgeComm(hcomhand, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
        return ErrCode_NoErr;
    }
}
else
{
    return ErrCode_ComNotOpen;//串口未打开
}

}

//写串口
int serial::writecom(const uchar *data_buff, uint data_count)
{
unsigned long fact_WRdata_count = 0; //实际读到的长度
bool bWriteStat;
if( isopen( ))
{
//清空收发缓冲区,停止后台读、写操作。
PurgeComm(hcomhand, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

        bWriteStat = WriteFile(hcomhand,data_buff,data_count,&fact_WRdata_count,NULL);
        if (!bWriteStat&& ((unsigned long)fact_WRdata_count != data_count))
        {
            return ErrCode_WriteLenFailed;   //写长度失败
        }
        //PurgeComm(hcomhand, PURGE_TXABORT |  PURGE_TXCLEAR );
        return ErrCode_NoErr;                //成功
    }
    else
    {
            return ErrCode_ComNotOpen;      //串口未打开
    }

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐