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

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

问题描述

我想使用 Visual Studio 和 C 将数据发送到我的 Arduino 板.我特别需要使用 C,因为我使用 ARToolKit 来获取标记并相应地发送数据.

I want to send data to my Arduino board using Visual Studio and C. I specifically need to use C since I am using ARToolKit to get markers and sending the data accordingly.

我正在尝试代码

#include<stdio.h>
#include<stdlib.h>
void main()
{
    system( "MODE COM9: BAUD=9600 PARITY=n DATA=8 STOP=1" ) ;
    FILE port = fopen( "COM9:", "wb" ) ;
    printf("hello");
    fprintf( port, "s" ) ;
    fclose( port ) ;
}

但它没有被编译.

我只需要发送数据.

推荐答案

我从您的帖子中了解到,您需要使用 Visual Studio 发送数据,而不是将程序写入 Flash 或任何其他东西.这是我在我的机器上为您制作的示例,它可以发送文本测试.

I understood from your post that you need to send data using visual studio not to write a program to flash or any other stuff. Here is an example i made for you on my machine it works sending the text test.

#include "stdafx.h"
#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;
}

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

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