qq_38099738 2017-03-28 13:15 采纳率: 0%
浏览 1072

求解一个C++串口API的问题

这是我的程序:
程序运行到后面发送数据时就出错了,按照返回的错误代码提示句柄无效。
有没有人能够帮忙解答一下,不胜感激。

 // Serial.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    char SerialNum[5];
    cout << "->->请输入一个串口号:" << endl;
    cin >> SerialNum;
    HANDLE SerialHandle;
    //创建端口3,允许读写,固定为0,安全属性为空,设置为打开而不是创建,使用异步通信
    SerialHandle = CreateFileA(SerialNum,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
    if(SerialHandle == INVALID_HANDLE_VALUE)
    { cout << "->Serial open fail" << endl; }
    else
    {
        cout << "->Serial open successful" << endl;
        SetCommMask(SerialHandle,EV_RXCHAR | EV_TXEMPTY); //??设置事件驱动类型??
        SetupComm(SerialHandle,1024,512);  //设置输入输出缓冲区的大小
        //清空缓冲区并中断所有写操作(即使写操作还并没有完成),中断所有读操作(即使读操作还并没有完成),清除输出缓冲区,清除输入缓冲区
        PurgeComm(SerialHandle,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
        cout << "->Erase buffer" << endl;
        COMMTIMEOUTS SerialTimeOut;  //定义串口超时结构体
        //读取总超时时间 = 时间系数 * 要读/写的字符数 + 时间常量
        SerialTimeOut.ReadIntervalTimeout = 1000;  //设定读间隔超时为1000毫秒
        SerialTimeOut.ReadTotalTimeoutConstant = 5000;  //设定读间隔常数为5000
        SerialTimeOut.ReadTotalTimeoutMultiplier = 500;  //设定读时间超时系数为500
        SerialTimeOut.WriteTotalTimeoutConstant = 500;
        SerialTimeOut.WriteTotalTimeoutMultiplier = 2000;
        SetCommTimeouts(SerialHandle,&SerialTimeOut);  //设置超时时间
        cout << "->Set time out" << endl;
        int SerialBaud = 0;
        cout << "->->请输入波特率:" << endl;
        cin >> SerialBaud;
        DCB SerialDataBlock;  //定义串口数据块
        GetCommState(SerialHandle,&SerialDataBlock);  //读取原本串口的配置
        //配置串口波特率9600,长度8位,没有校验,1位停止位
        SerialDataBlock.BaudRate = SerialBaud;
        SerialDataBlock.ByteSize = 8;
        SerialDataBlock.Parity = NOPARITY;
        SerialDataBlock.StopBits = ONESTOPBIT;
        if(SetCommState(SerialHandle,&SerialDataBlock))  //设置串口
        { cout << "->Set serial option" << endl << endl; }
        while(1)
        {
            DWORD SerialReadCount = 0,SerialWriteCount = 0;
            DWORD SerialReadResult = 0,SerialWriteResult = 0;
            bool SerialReadStatus = 0,SerialWriteStatus = 0;
            OVERLAPPED OS_Read;
            char SerialCmd[512];
            cout << "->Wait command" << endl;
            cin >> SerialCmd;
            if( ! strcmp(SerialCmd,"Send:"))
            {
                PurgeComm(SerialHandle,PURGE_TXABORT | PURGE_TXCLEAR);
                cout << "->";
                cin >> SerialCmd;
                if(WriteFile(SerialHandle,SerialCmd,100,&SerialWriteResult,&OS_Read))
                { cout << "->Send successful" << endl << endl; }
                else
                {
                    cout << "->Send fail" << endl;
                    cout << "Error:" << GetLastError() << endl << endl;
                }
            }
            else if( ! strcmp(SerialCmd,"Receive:"))
            {
                if(ReadFile(SerialHandle,SerialCmd,100,&SerialReadResult,&OS_Read))
                {
                    cout << "Receive successful" << endl;
                    cout << SerialCmd << endl << endl;
                }
                else
                {
                    cout << "Receive fail" << endl;
                    cout << "Error:" << GetLastError() << endl << endl;
                }
                PurgeComm(SerialHandle,PURGE_RXABORT | PURGE_RXCLEAR);
            }
            else if( ! strcmp(SerialCmd,"Close"))
            { CloseHandle(SerialHandle);break; }
            else
            { cout << "Unknow command" << endl << endl; }
        }
    }
    system("pause");
    return 0;
}

  • 写回答

1条回答

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题