Visual Studio 2010 Arduino serial communication

2019-08-13 02:42发布

问题:

I am using OpenCV in Visual Studio 2010 to track an object, and I am trying to send a value to the Arduino to rotate servos attached to the camera. I am using an Arduino Uno. I have completed the C++ code that tracks the object and determines which direction the camera needs to be rotated, but I am having trouble sending this data to the Arduino. I am currently trying to use an RS-232 cable for this. I am using a Type-B USB cable to program my Arduino and an RS-232 to try to send the data from Visual Studio to the Arduino. Here is my code for the Visual Studio serial communication:

     int portspeed(int centerpix, int xmid)
{HANDLE hDevice = CreateFile(L"COM5",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);
    DCB lpTest;
    GetCommState(hDevice,&lpTest);
    lpTest.BaudRate = CBR_9600;
    lpTest.ByteSize = 8;
    lpTest.Parity = NOPARITY;
    lpTest.StopBits = ONESTOPBIT;
    SetCommState(hDevice,&lpTest);
    DWORD btsIO;

    if (centerpix<xmid)
    {
        char test[] = "2";
        WriteFile(hDevice,test,strlen(test),&btsIO,NULL);
        cout << "Turn right " << test << endl;
    }
    else
    {
        char test[] = "3";
        WriteFile(hDevice,test,strlen(test),&btsIO,NULL);
        cout << "Turn left " << test << endl;
    }

    return 0;
}

On the Arduino code side I have this, which I am using to attempt to light two different LEDS to see if the program is able to correctly communicate which direction it needs to rotate:

int incomingByte = 0;   // For incoming serial data
void setup()
{
    Serial.begin(9600);     // Opens serial port, sets data rate to 9600 bit/s
}

void loop()
{
    // Send data only when you receive data:
    if (Serial.available() > 0)
    {
        incomingByte = Serial.read();
        if (incomingByte==50)   //if =2
            analogWrite(9,100);
        else
            analogWrite(9,0);
        if (incomingByte==51)   //if =3
            analogWrite(10,50);
        else analogWrite(10,0);
            delay(3000);
    }
    else
        analogWrite(9,255);
}

My interpretation is that I need to start the C++ program (which continuously sends the data over the serial communication), and then attach the TX pin from the RS-232 into the RX pin (digital 0) on the Arduino. When I try to upload the program to the Arduino I am given an error,

avrdude: stk500_getsync(): not in sync: resp=0x00

This only occurs when I have a wire going into the RX pin, even if this wire is not connected to anything. I believe that this error occurs because the RX is looking for an input with a baud rate of 9600, but it still gives me this error when the C++ program is running and sending the data with a rate of 9600.

How can I send a value from a Visual Studio project doing real time image processing on a laptop to an Arduino via serial communication?

回答1:

Speaking as someone with limited Win32 experience (more of a .NET guy, really), I think your problem may be in write buffering.

By default, writes to a file or port are buffered in memory. Perhaps the write is never getting sent to the port as you are never closing the file handle nor calling [FlushFileBuffers][3].

Try adding this prior to return 0;:

//After a time, sensitive write
FlushFileBuffers(hDevice);

//or, more properly for the end of the program.
CloseHandle(hDevice);