串口通信Arduino VC++

问题描述 投票:0回答:1

我无法使用 Visual C++ 在我的 Arduino 上写入字符串。我能够以某种方式打开 COM 端口,但无法将数据发送到 Arduino。我的情况实际上会出现什么问题?

int main()
{
    HANDLE hComm;
    hComm = CreateFileA("\\\\.\\COM11",
            GENERIC_READ | GENERIC_WRITE,
            0,
            0,
            OPEN_EXISTING,
            FILE_FLAG_OVERLAPPED,
            0);

    if (hComm == INVALID_HANDLE_VALUE)
    {
        printf("com not opened");
    }
    else
    {
        printf("COM OPENED");
    }

    COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
    DCB dcb;
    memset(&dcb,0,sizeof(dcb));
    dcb.DCBlength = sizeof(dcb);
    dcb.BaudRate = 38400;
    dcb.fBinary = 1;
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;
    dcb.ByteSize = 8;

    if(!SetCommState(hComm,&dcb))
    {
        printf("HI");
    }

    while(1)
    {
        char bag[]="L";
        DWORD read=0 ;
        DWORD write=1; // Number of bytes to write to serial port
        //         Decmial value to write to serial port
        WriteFile(hComm, bag,write,&write, NULL);
    }
}
visual-c++ arduino serial-port
1个回答
2
投票

您必须正确设置 DCB 结构的

every
成员。最简单的方法是使用
GetCommState
读取现有设置,然后仅更改您关心的设置。

现在你的流量控制很可能是错误的。

哦,您还初始化了超时结构,但从未将这些设置应用到端口。

© www.soinside.com 2019 - 2024. All rights reserved.