tangtangllll
tangtangllll
2017-05-05 07:48

使用API函数编写串口上位机,只能发送第一组数据

1
  • 上位机
  • api
  • 串口通讯

在此贴出程序,希望实现按下回车发送一次angle。但是现在只能发送第一个数据。是因为有标志位没有恢复吗?

 bool  Initserial()
{
    hCom = CreateFile("COM7", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
    SetupComm(hCom, 5, 5);//输入/输出缓冲区大小
    COMMTIMEOUTS TimeOuts;
    TimeOuts.ReadIntervalTimeout = 1000;
    TimeOuts.ReadTotalTimeoutMultiplier = 500;
    TimeOuts.ReadTotalTimeoutConstant = 5000;
    TimeOuts.WriteTotalTimeoutMultiplier = 500;
    TimeOuts.WriteTotalTimeoutConstant = 2000;
    SetCommTimeouts(hCom, &TimeOuts); 
    DCB dcb;
    GetCommState(hCom, &dcb);
    dcb.BaudRate = 9600; //波特率为9600
    dcb.ByteSize = 8; //每个字节有8位
    dcb.Parity = NOPARITY; //无奇偶校验位
    dcb.StopBits = ONESTOPBIT; //一个停止位
    SetCommState(hCom, &dcb);
    PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
    return true;
}

 int main(int argc, char** argv)
{
    Initserial();
    cout<<"DCB2"<<endl;
    namedWindow("a",CV_WINDOW_AUTOSIZE);
    angle=40;
    while (1)
    {

    int key = cvWaitKey(30);
    if (key==13)//按下回撤键执行
        {
        angle=angle+20;

        d[0]=0x30+angle/100;
        d[1]=0x30+(angle-(angle/100)*100)/10;
        d[2]=0x30+angle%10;
        d[3]=0x0D;
        d[4]=0x0A;


        WriteFile(hCom, &d, dwBytesWritten, &dwBytesWritten, &m_osWrite);
        cout<<"d"<<d<<endl;

        } }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答