串口通讯Arduino VC++ [英] Serial port communication Arduino VC++

查看:29
本文介绍了串口通讯Arduino VC++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 Visual 将字符串写入我的 ArduinoC++.我以某种方式能够打开 COM 端口,但无法将数据发送到 Arduino.就我而言,问题实际上是什么?

I am not able to write a string on to my Arduino using Visual C++. I am somehow able to open the COM port but not able to send data to the Arduino. What would the problem actually be in my case?

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);
    }
}

推荐答案

您必须正确设置 DCB 结构的 every 成员.最简单的方法是使用 阅读现有设置GetCommState,然后只更改您关心的那些.

You have to set every member of the DCB structure correctly. The easiest thing to do is to read the existing settings with GetCommState, then change just the ones you care about.

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

Right now your flow control is most likely wrong.

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

Oh, you also initialized a timeout structure, but never applied those settings to the port.

这篇关于串口通讯Arduino VC++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆