Click here to Skip to main content
13,458,837 members
Rate this:
Please Sign up or sign in to vote.
Dear all, im a begginer in programming and I have an arduino board, coded to receive data trhough serial, if it receives "1", an action will be triggered but if it receives a "0" its another action, pretty simple. With the serial monitor that comes with the arduino program it works, but i would like to do it trhough a C program.

My problem is, after reading a lot of related material, i cant put it to work. I cant compile any of the sample programs with Dev C++ Theres always problems with missing libraries and functions.

So my question is, what compiler do i need to make it work? If Dev C++ is not the problem, could someone paste a code that could do it, send 0 or 1 to the serial port?

Im using Windows 7 64bits... is this a problem too?
Posted 27-Nov-12 9:35am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Why don't you use Visual C++ (you may even use C#) Express Edition?
You may use the following code sample for configuring correctly the COM device:
"Configuring a Communications Resource (Windows)"[^] and the simply issue
CHAR c='1'; // change to CHAR c=1; if appropriate
DWORD dwWritten;

if ( ! WriteFile(hCom, &c, 1, &dwWritten, NULL))
  // handle error

to syncrhonously write the byte '1' (or 1).

Of course writing '0' (or 0) is very similar... :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Found it at last.. it compiles and works with dev c++ / Windows

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>

#define BAUDRATE 9600
#define MODEMDEVICE "com3"

int main()


    HANDLE hCom;
    char c;
    DWORD nbytes;

BOOL bPortReady;
DCB dcb;

hCom = CreateFile(MODEMDEVICE,
0, // Exclusive acces
NULL, // sem seguranca
0, // no overlapped I/O
NULL); // null template
bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes
bPortReady = GetCommState(hCom, &dcb);
dcb.BaudRate = BAUDRATE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
// dcb.Parity = EVENPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fAbortOnError = TRUE;
// set XON/XOFF
dcb.fOutX = FALSE; // XON/XOFF off for transmit
dcb.fInX = FALSE; // XON/XOFF off for receive
// set RTSCTS
dcb.fOutxCtsFlow = FALSE; // turn off CTS flow control
dcb.fRtsControl = RTS_CONTROL_DISABLE; //
// set DSRDTR
dcb.fOutxDsrFlow = FALSE; // turn off DSR flow control
dcb.fDtrControl = DTR_CONTROL_DISABLE; //
bPortReady = SetCommState(hCom, &dcb);
// Communication timeouts are optional
bPortReady = GetCommTimeouts (hCom, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
bPortReady = SetCommTimeouts (hCom, &CommTimeouts);

//Sending data
WriteFile(hCom, "0", 1, &nbytes,NULL);
WriteFile(hCom, "1", 1, &nbytes,NULL);


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180323.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100