Win32 COM端口数据加扰 我试图通过USB将我的ARDUINO Nano发送数据传送到我的C++脚本,但是我得到的数据似乎不在接收器中。我不知道是什么问题

Win32 COM端口数据加扰 我试图通过USB将我的ARDUINO Nano发送数据传送到我的C++脚本,但是我得到的数据似乎不在接收器中。我不知道是什么问题,c++,winapi,arduino,C++,Winapi,Arduino,这是我的Arduino代码: void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: Serial.begin(9600); Serial.println("Hello World!"); Serial.end(); } 这里是我的C++代码: #include <

这是我的Arduino代码:

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.begin(9600);
  Serial.println("Hello World!");
  Serial.end();
}

这里是我的C++代码:

#include <iostream>
#include <windows.h>

int main()
{
    char Byte;
    DWORD dwBytesTransferred;

    HANDLE hSerial;
    while (true)
    {

        hSerial = CreateFile(L"COM7",
            GENERIC_READ | GENERIC_WRITE,
            0,
            0,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            0);

        if (hSerial == INVALID_HANDLE_VALUE) {
            if (GetLastError() == ERROR_FILE_NOT_FOUND) {
                std::cout << "Serial Port Disconected\n";
                //return 0;
            }
        }
        else
        {
            ReadFile(hSerial, &Byte, 1, &dwBytesTransferred, 0);
            std::cout << Byte;
        }


        CloseHandle(hSerial);
    }

    return 0;
}

我的Arduino正在发送“Hello World!”,顺便说一句。

Arduino代码是正确的

< >我发现C++代码的问题如下:

#include <iostream>
#include <windows.h>

int main()
{
    char Byte;
    DWORD dwBytesTransferred;

    HANDLE hSerial;

    hSerial = CreateFile("COM7",                                    //Placed outside while loop also, check if 'L"COM7"' is correct.
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);

    PurgeComm(hSerial, PURGE_RXCLEAR);                              //Clear the RX buffer in windows

    while (hSerial != INVALID_HANDLE_VALUE)                         //Executes if the serial handle is not invalid
    {

        ReadFile(hSerial, &Byte, 1, &dwBytesTransferred, 0);
        
        if(dwBytesTransferred == 1)                 
            std::cout << Byte;

    }

    if (hSerial == INVALID_HANDLE_VALUE)                            //Throw an error if the serial handle is invalid
    {
        std::cout << "Serial Port Not Available\n";
    }

    CloseHandle(hSerial);                                           //Close the handle (handle is also automatically closed when the program is closed)

    return 0;
}
  • 连续打开和关闭端口是一个坏主意(while循环的执行速度快于您获取数据的速度-这只会导致您获取垃圾数据)

  • 由于您试图读取单个字节的数据,
    ReadFile
    的参数3应该是1,而不是10。相反,如果将大小为10的数组传递给参数2,则代码是有效的

  • 您没有使用返回值
    dwbytesttransfered
    来检查具有正确字节数的读取操作是否成功。在读取文件之后,您可以检查参数2和3是否匹配

  • 根据我的经验,当串行设备连接时,windows会自动开始缓冲数据。根据您的情况,您可能需要使用
    purgecom
    功能清除windows中的接收缓冲区

  • 更正代码如下:

    #include <iostream>
    #include <windows.h>
    
    int main()
    {
        char Byte;
        DWORD dwBytesTransferred;
    
        HANDLE hSerial;
    
        hSerial = CreateFile("COM7",                                    //Placed outside while loop also, check if 'L"COM7"' is correct.
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);
    
        PurgeComm(hSerial, PURGE_RXCLEAR);                              //Clear the RX buffer in windows
    
        while (hSerial != INVALID_HANDLE_VALUE)                         //Executes if the serial handle is not invalid
        {
    
            ReadFile(hSerial, &Byte, 1, &dwBytesTransferred, 0);
            
            if(dwBytesTransferred == 1)                 
                std::cout << Byte;
    
        }
    
        if (hSerial == INVALID_HANDLE_VALUE)                            //Throw an error if the serial handle is invalid
        {
            std::cout << "Serial Port Not Available\n";
        }
    
        CloseHandle(hSerial);                                           //Close the handle (handle is also automatically closed when the program is closed)
    
        return 0;
    }
    

    Arduino代码是正确的

    < >我发现C++代码的问题如下:

    #include <iostream>
    #include <windows.h>
    
    int main()
    {
        char Byte;
        DWORD dwBytesTransferred;
    
        HANDLE hSerial;
    
        hSerial = CreateFile("COM7",                                    //Placed outside while loop also, check if 'L"COM7"' is correct.
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);
    
        PurgeComm(hSerial, PURGE_RXCLEAR);                              //Clear the RX buffer in windows
    
        while (hSerial != INVALID_HANDLE_VALUE)                         //Executes if the serial handle is not invalid
        {
    
            ReadFile(hSerial, &Byte, 1, &dwBytesTransferred, 0);
            
            if(dwBytesTransferred == 1)                 
                std::cout << Byte;
    
        }
    
        if (hSerial == INVALID_HANDLE_VALUE)                            //Throw an error if the serial handle is invalid
        {
            std::cout << "Serial Port Not Available\n";
        }
    
        CloseHandle(hSerial);                                           //Close the handle (handle is also automatically closed when the program is closed)
    
        return 0;
    }
    
  • 连续打开和关闭端口是一个坏主意(while循环的执行速度快于您获取数据的速度-这只会导致您获取垃圾数据)

  • 由于您试图读取单个字节的数据,
    ReadFile
    的参数3应该是1,而不是10。相反,如果将大小为10的数组传递给参数2,则代码是有效的

  • 您没有使用返回值
    dwbytesttransfered
    来检查具有正确字节数的读取操作是否成功。在读取文件之后,您可以检查参数2和3是否匹配

  • 根据我的经验,当串行设备连接时,windows会自动开始缓冲数据。根据您的情况,您可能需要使用
    purgecom
    功能清除windows中的接收缓冲区

  • 更正代码如下:

    #include <iostream>
    #include <windows.h>
    
    int main()
    {
        char Byte;
        DWORD dwBytesTransferred;
    
        HANDLE hSerial;
    
        hSerial = CreateFile("COM7",                                    //Placed outside while loop also, check if 'L"COM7"' is correct.
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);
    
        PurgeComm(hSerial, PURGE_RXCLEAR);                              //Clear the RX buffer in windows
    
        while (hSerial != INVALID_HANDLE_VALUE)                         //Executes if the serial handle is not invalid
        {
    
            ReadFile(hSerial, &Byte, 1, &dwBytesTransferred, 0);
            
            if(dwBytesTransferred == 1)                 
                std::cout << Byte;
    
        }
    
        if (hSerial == INVALID_HANDLE_VALUE)                            //Throw an error if the serial handle is invalid
        {
            std::cout << "Serial Port Not Available\n";
        }
    
        CloseHandle(hSerial);                                           //Close the handle (handle is also automatically closed when the program is closed)
    
        return 0;
    }
    

    试图将10个字节读入1个字节是未定义的行为。
    ReadFile
    的参数3是要读取的字节数(10),但您只传递一个1字节的缓冲区。我将其设置为1,这很有帮助,但输出仍然有点混乱:Hello World!Hllo世界!Hllo世界!世界啊!是否有一个函数可以等待com有新数据读取它?您已经阅读了Microsoft的文档了吗。特别是,C++和IVE的章节都读了很多,是的,我可以经常阅读更多的内容,但是大多数的旧的C++窗口窗体过时了,把10个字节读进1个字节是不确定的行为。
    ReadFile
    的参数3是要读取的字节数(10),但您只传递一个1字节的缓冲区。我将其设置为1,这很有帮助,但输出仍然有点混乱:Hello World!Hllo世界!Hllo世界!世界啊!是否有一个函数可以等待com有新数据读取它?您已经阅读了Microsoft的文档了吗。特别是,我和我的文章已经阅读了很多,是的,我可以经常阅读更多的内容,但是大部分的旧的C++窗口窗体的AppScript已经过时了。如果串行端口未成功打开,代码仍会调用
    CloseHandle(hSerial)
    。是。循环不会终止。用户需要终止程序(与问题中的
    while(true)
    几乎相同)。您是对的,调用
    CloseHandle()
    是多余的。在描述句柄只有在未成功打开时才尝试关闭的情况时,“多余”有点委婉。如果串行端口成功打开,则
    while(hSerial!=无效的\u handle\u值)
    循环从不终止。如果串行端口未成功打开,代码仍会调用
    CloseHandle(hSerial)
    。是。循环不会终止。用户需要终止程序(与问题中的
    while(true)
    几乎相同)。您是对的,调用
    CloseHandle()
    是多余的。在描述句柄只有在未成功打开时才尝试关闭的情况时,“多余”有点委婉。