Search code examples
carduinoserial-communication

Serial communication (for Arduino) using Visual Studio 2008 and C


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.

I was trying the code

#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 ) ;
}

but it is not getting compiled.

I just need to send data.


Solution

  • 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;
    }