串口通信使用Visual Studio 2010和C(用于Arduino的) [英] Serial communication (for Arduino) using Visual Studio 2010 and C

查看:884
本文介绍了串口通信使用Visual Studio 2010和C(用于Arduino的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前面临的一个轻微的问题。我目前在做一些视频(图像)检测与跟踪使用Microsoft Visual Studio 2010专业版和 OpenCV的,和(的视觉系统)的结果是通过Arduino的平台,以控制额外的硬件

我的code对视觉系统的工作原理100%,与Arduino的code也适用100%,但我有一个问题,通过C / C ++从PC到Arduino板通信。 (不能使用Visual Basic等)

目前规格:Windows和NBSP; 7平台,Visual Studio 2010中,OpenCV的2.2.0,Arduino的0022米加的Arduino板,2个USB接口为模拟串口转换器

我的code似乎并不甚至可以运作。我是,但是,继#opc0de下面的例子。但要说明我的观点的code以下似乎不工作(从来没有规定的COM开放(是的,我换到写端口))。 (连接两个串口电缆互相插入两个不同的COM端口的超级终端,realterm等方面的工作100%。我的code和code似乎不低于在所有工作)。 (我的code是+ - 800线长,我不觉得自己作为张贴COMS的部分非常相似,下同)。我只想串行commynication是工作。

所有我从COMS的希望是发送一个文本字符串的能力,X = ### Y = ###所以即使是简单的一块code会做的(如如下图)(我想在稍后阶段阅读,但我并不担心它的时刻)

链接到code:<一href=\"http://stackoverflow.com/questions/8123057/serial-communication-for-arduino-using-visual-studio-2008-and-c\">Stack Overflow - opc0de

 的#include&LT;&WINDOWS.H GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;INT _tmain(INT ARGC,_TCHAR *的argv [])
{
    CHAR测试[] =你好;    HANDLE hDevice =的CreateFile(LCOM2,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);    如果(hDevice!= INVALID_HANDLE_VALUE)
    {
        的printf(端口打开\\ n!);
        DCB lpTest;
        GetCommState(hDevice,&安培; lpTest);
        lpTest.BaudRate = CBR_9600;
        lpTest.ByteSize = 8;
        lpTest.Parity = NOPARITY;
        lpTest.StopBits = ONESTOPBIT;
        SetCommState(hDevice,&安培; lpTest);        DWORD btsIO;        WriteFile的(hDevice,测试,strlen的(测试),放大器; btsIO,NULL);        CloseHandle的(hDevice);
    }
    _getch();
    返回0;
}


解决方案

检查code,以确保它独立工作:

首先,使用COM记录器。从MS老和良好的终端是最差的,但我不记得还有一个现在。看看你的C ++ code发送的东西在那里。如果没有,使其工作:)如果是的话,调试commnication问题一个Arduino。就Arduino的一个简单的记录,看看你收到关于另一部分是什么,以及如何处理它。

I'm facing a slight issue at the moment. I am currently doing some video (image) detection and tracking using Microsoft Visual Studio 2010 Professional and OpenCV, and the result (of the vision system) is to control additional hardware via the Arduino platform.

My code for the vision system works 100% and code on Arduino also works 100%, but I'm having an issue communicating via C / C++ from the PC to the Arduino board. (Can not use Visual Basic, etc.)

Specifications at the moment: Windows 7 platform, Visual Studio 2010, OpenCV 2.2.0, Arduino 0022, Arduino Mega board, 2 x USB to serial converters for simulations.

My code does not seem to even be functioning. I am, however, following the example below by #opc0de. But to illustrate my point the code below doesn't seem to work (never states COM open (yes, I change to the write ports)). (Connect two serial cables to each other plugged into two different COM ports hyperterminal, realterm, etc. work 100%. My code and the code below don't seem to work at all). (My code is +- 800 lines long and I don't feel like posting as the COMs section is very similar to below). I just want serial commynication to be working.

All I want from the COMs is the ability to send a text string, "x=### y=###" so even a simple piece of code will do (like below) (I want to read at a later stage, but I am not worried it at the moment)

Link to code: Stack Overflow - opc0de

#include <Windows.h>
#include <stdio.h>
#include <conio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    char test[] = "Hello";

    HANDLE hDevice = CreateFile(L"COM2",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);

    if (hDevice !=INVALID_HANDLE_VALUE)
    {
        printf("Port opened! \n");
        DCB lpTest;
        GetCommState(hDevice,&lpTest);
        lpTest.BaudRate = CBR_9600;
        lpTest.ByteSize = 8;
        lpTest.Parity = NOPARITY;
        lpTest.StopBits = ONESTOPBIT;
        SetCommState(hDevice,&lpTest);

        DWORD btsIO;

        WriteFile(hDevice,test,strlen(test),&btsIO,NULL);

        CloseHandle(hDevice);
    }
    _getch();
    return 0;
}

解决方案

Check your code to make sure it works separately:

First, use a COM logger. The old and good Terminal from MS is the worst, but I cannot remember another one right now. See if your C++ code sends something there. If no, make it work :) If yes, debug commnication problems with an Arduino. Make a simple logger on Arduino to see what you receive on the other part and how you handle it.

这篇关于串口通信使用Visual Studio 2010和C(用于Arduino的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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