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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top