求解一个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个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问