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

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

问题描述

我不能够写使用视觉上我的Arduino字符串C ++ 。我某种程度上能够打开的COM端口,但不能够将数据发送到Arduino。将这个问题实际上是在我的情况怎么办?

  INT的main()
{
    HANDLE hComm;
    hComm = CreateFileA的(\\\\\\\\。\\\\ COM11
            GENERIC_READ | GENERIC_WRITE,
            0,
            0,
            OPEN_EXISTING,
            FILE_FLAG_OVERLAPPED,
            0);    如果(hComm == INVALID_HANDLE_VALUE)
    {
        的printf(COM未打开);
    }
    其他
    {
        的printf(COM开设了);
    }    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;    如果(SetCommState(hComm,&安培;!DCB))
    {
        的printf(HI);
    }    而(1)
    {
        炭包[] =L;
        DWORD读= 0;
        DWORD写= 1; //字节数写串口
        // Decmial值写入到串口
        WriteFile的(hComm,袋,写,和放大器;写,NULL);
    }
}


解决方案

您必须正确设置的的每一个成员DCB 结构。最容易做的事情就是阅读 <$ C现有的设置$ C> GetCommState ,然后更改只是你关心的人。

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

呵呵,你也初始化超时结构,但从未应用于这些设置的端口。

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

解决方案

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天全站免登陆