Visual Studio 2010 Arduino 串行通信 [英] Visual Studio 2010 Arduino serial communication
问题描述
我在 Visual Studio 2010 中使用 OpenCV 来跟踪一个对象,我正在尝试向 Arduino 发送一个值以旋转连接到相机的伺服系统.我正在使用 Arduino Uno.我已经完成了 C++ 代码来跟踪对象并确定相机需要旋转的方向,但我无法将此数据发送到 Arduino.我目前正在尝试为此使用 RS-232 电缆.我正在使用 B 型 USB 电缆对我的 Arduino 和 RS-232 进行编程,以尝试将数据从 Visual Studio 发送到 Arduino.这是我的 Visual Studio 串行通信代码:
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;
}
在 Arduino 代码方面,我有这个,我用它来尝试点亮两个不同的 LED,以查看程序是否能够正确传达它需要旋转的方向:
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);
}
我的解释是我需要启动 C++ 程序(它通过串行通信不断发送数据),然后将 RS-232 的 TX 引脚连接到 Arduino 上的 RX 引脚(数字 0).当我尝试将程序上传到 Arduino 时出现错误,
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():不同步:resp=0x00
avrdude: stk500_getsync(): not in sync: resp=0x00
这只发生在我有一根电线进入 RX 引脚时,即使这根电线没有连接到任何东西.我相信发生这个错误是因为RX正在寻找波特率为9600的输入,但是当C++程序运行并以9600的速率发送数据时,它仍然给我这个错误.
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.
如何通过串行通信将在笔记本电脑上进行实时图像处理的 Visual Studio 项目中的值发送到 Arduino?
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?
推荐答案
以有限的Win32 经验(更多的是 .NET 人,真的),我认为您的问题可能是在写缓冲中.
Speaking as someone with limited Win32 experience (more of a .NET guy, really), I think your problem may be in write buffering.
默认情况下,对文件或端口的写入缓冲在内存中.也许写入永远不会发送到端口,因为您永远不会关闭文件句柄或调用 [FlushFileBuffers][3]
.
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]
.
尝试在 return 0;
之前添加这个:
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);
这篇关于Visual Studio 2010 Arduino 串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!