繁体   English   中英

Win32 COM 端口数据乱码

[英]Win32 COM port data scrambled

我试图让我的 Arduino Nano 通过 USB 通过 COM 端口向我的 ZF6F87C9FDCF8B3C3C3F07F93F1EE81 发送数据到我的 ZF6F87C9FDCF8B3C3C3F07F93F1EE87 脚本。 我不知道问题是什么。

这是我的 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;
}

这是我的 C++ 代码显示的内容:

roHdol!!HdWl
l e!ol
l e!ol

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

Arduino 代码是正确的。

我使用 C++ 代码发现的问题如下:

  1. 连续打开和关闭端口是个坏主意(while 循环的执行速度比您获取数据的速度快 - 这只会导致您获取垃圾数据)

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

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

  4. 根据我的经验,当连接了串口设备时,windows 会自动开始缓冲数据。 根据您的情况,您可能希望使用PurgeComm function 清除 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;
}

我已经测试过了,它可以工作,我的 output 是

Hello World!
Hello World!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM