Sending data to Arduino Mega from C code

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 ;

}

You may be facing the problem described here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292647583/3#3

Cheers,

-br

http://entropymouse.com

Thanks for your help.

I have added an Sleep(5000) after CreateFile and after setting up dcb.

Unfortunatelly it is not working as expected.

I'm wondering about flow control, hand shaking and so on.

But not sure how to setup it accordingly in dcb structure.

Rather than fiddle with the Windows API I suggest using something that is known to work...

Hi All

Finally I have found the cause ( and the solution ).

Some default values for DCB structure enables some flow control which are not appropriate for communcation with arduino.

Tests performed using a terminal emulator showed that XON-XOFF or NONE values were appropriate.

But some default values for DCB sctructure enables CTS/RTS

So, the solution is to explicit disable those flow controls

dcb.BaudRate=9600;
dcb.fParity=false ;
dcb.ByteSize=8;
dcb.Parity=0 ;
dcb.StopBits=0 ;
dcb.fAbortOnError = true;
dcb.fOutX=true ;
dcb.fOutxDsrFlow = 0;
** dcb.fOutxCtsFlow = 0;**
** dcb.fRtsControl=0 ;**
** dcb.fOutxCtsFlow=0 ;**
** dcb.fOutxDsrFlow=0 ;**
** dcb.fDtrControl=0 ;**

I would like to thank the support received from other members of this forum, which gave me the necessary enthusiasm to keep working to find a fix.

Regards