Hello,
I'm writing a test program in C to send data to Arduino Mega.
I'm testing data received by Arduino using Morse Traslator example.
If sending data from Arduino IDE or from a terminal emulator, led blinks as expected.
But I can't figure out why it's not working when sending from my C program.
Environment description : Windows XP. Microsoft C++ visual studio 2010 ( evaluation ). Arduino drivers from V 0021.
I appreciate any help
Below is my code ( I do some prints of serial to check settings )
The test should send the text "SOS" which is easier to recognize in the Morse Traslator example.
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <WinBio.h>
int main (int argc, CHAR* argv[])
{
static HANDLE hComm=NULL ;
static DCB dcb ={0} ;
static COMMTIMEOUTS timeouts ;
DWORD BytesWritten ;
char textoPrueba[4]="SOS" ;
DWORD bytesEscritos ;
DWORD dwLastErrorCode ;
int largo ;
bool bPortReady ;
hComm=CreateFile(TEXT("COM6"),
GENERIC_READ|GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
NULL) ;
bPortReady = SetupComm(hComm, 1, 128);
if ( hComm==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,TEXT("Could not open Com port "),TEXT("ERROR"),0) ;
}
if (!GetCommState(hComm,&dcb))
{
MessageBox(NULL,TEXT("Couldn't get current device control block "),
TEXT("ERROR"),0);
CloseHandle(hComm);
}
dcb.BaudRate=9600;
dcb.fParity=FALSE ;
dcb.ByteSize=8;
dcb.Parity=NOPARITY ;
dcb.StopBits=ONESTOPBIT ;
dcb.fAbortOnError = TRUE;
dcb.fBinary=1 ;
if (!SetCommState(hComm,&dcb))
{
MessageBox(NULL,TEXT("Couldn't update Device Control Block ") ,
TEXT("ERROR"),0);
CloseHandle(hComm) ;
}
timeouts.ReadIntervalTimeout=600 ;
timeouts.ReadTotalTimeoutMultiplier=100 ;
timeouts.ReadTotalTimeoutConstant=600 ;
timeouts.WriteTotalTimeoutMultiplier=100 ;
timeouts.WriteTotalTimeoutConstant=600;
if ( !SetCommTimeouts(hComm,&timeouts))
{
MessageBox(NULL,TEXT("Couldn't update Comm Time outs ") ,
TEXT("ERROR"),0);
CloseHandle(hComm) ;
}
if (!GetCommState(hComm,&dcb))
{
MessageBox(NULL,TEXT("Couldn't get current device control block "),
TEXT("ERROR"),0);
CloseHandle(hComm);
}
printf("Baud Rate : %d \n",dcb.BaudRate ) ;
printf("fParity : %d \n",dcb.fParity) ;
printf("Stop Bits : %d \n",dcb.StopBits ) ;
printf("Parity : %d \n ",dcb.Parity ) ;
printf("Xon: %d \n",dcb.fOutX ) ;
textoPrueba[0]='S' ;
textoPrueba[1]='O' ;
textoPrueba[2]='S' ;
textoPrueba[3]='\0' ;
largo=strlen(textoPrueba) ;
printf("Largo: %d\n",largo ) ;
if (!WriteFile(hComm,textoPrueba,largo,&bytesEscritos,NULL))
{
dwLastErrorCode=GetLastError() ;
if (dwLastErrorCode != ERROR_IO_PENDING)
{
MessageBox(NULL,TEXT("Error on Write"),
TEXT("ERROR WRITING"),MB_OK) ;
}
}
printf("Error code : %d \n" ,dwLastErrorCode=GetLastError() );
printf("Bytes escritos: %d \n" ,bytesEscritos ) ;
printf("Texto : %s \n",textoPrueba) ;
return 0 ;
}