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

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

问题描述

我目前遇到了一个小问题.我目前正在使用 Microsoft Visual Studio 2010 Professional 和 OpenCV 进行一些视频(图像)检测和跟踪,而(视觉系统的)结果是通过 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.

我的视觉系统代码可以 100% 运行,Arduino 上的代码也可以 100% 运行,但是我在通过 C/C++ 从 PC 到 Arduino 板进行通信时遇到问题.(不能使用Visual Basic等)

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.)

目前的规格:Windows 7 平台、Visual Studio 2010、OpenCV 2.2.0、Arduino 0022、Arduino Mega 板、2 个用于模拟的 USB 到串行转换器.

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.

我的代码似乎甚至无法运行.但是,我正在遵循#opc0de 的以下示例.但是为了说明我的观点,下面的代码似乎不起作用(从不声明 COM 打开(是的,我更改为写入端口)).(将两根串行电缆相互连接并插入两个不同的 COM 端口超级终端、realterm 等.工作 100%.我的代码和下面的代码似乎根本不起作用).(我的代码是 +- 800 行,我不想发帖,因为 COM 部分与下面非常相似).我只是想让串行通信正常工作.

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.

我希望 COM 能够发送文本字符串x=### y=###",因此即使是一段简单的代码也可以(如下所示)(以后想看,但现在不担心)

代码链接: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:

首先,使用 COM 记录器.MS 的旧终端是最糟糕的,但我现在想不起来另一个终端了.看看你的 C++ 代码是否在那里发送了一些东西.如果不是,让它工作:) 如果是,用 Arduino 调试通信问题.在 Arduino 上制作一个简单的记录器,以查看您在另一部分收到的内容以及您如何处理它.

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