Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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++ 4.9.9.2. 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

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

C++
#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()
{

// OPEN AND CONF COM PORT

    HANDLE hCom;
    char c;
    DWORD nbytes;

// VARS
BOOL bPortReady;
DCB dcb;
COMMTIMEOUTS CommTimeouts;

//Config
hCom = CreateFile(MODEMDEVICE,
GENERIC_READ | GENERIC_WRITE,
0, // Exclusive acces
NULL, // sem seguranca
OPEN_EXISTING,
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);
system("pause");

}
 
Share this answer
 
v2
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
C
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... :-)
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900