Hi,I'm trying to send 1's and 0's from a C program on my PC(Windows 7) to my Arduino Diciemillia(ATMega 168) to light up and turn off the LED on pin 13.The C code compiles and executes successfully and turns on the LED when I input 1 but crashes immediately after that and i get a write access violation error and some random address after that.This is a real problem because in the end I intend to use the arduino to control 2 servos with the values of the angles coming as a continuous stream from the C code.

//FillMemory(&dcb,sizeof(dcb),0);dcb.DCBlength=sizeof(dcb);if(!BuildCommDCB("9600,n,8,1", &dcb)){printf("Error Building DCB. Rectify and restart");}else if(!SetCommState(h,&dcb)){printf("Error setting state to port. Rectify and restart");}

printf("Enter 1 to turn on the LED, 0 to turn it off and 2 to exit");//for(i=0;i<10;i++)//{ch=getchar();data_buf[0]=ch;while(ch!=2){if(!WriteFile(h,data_buf,1,NULL,NULL)){printf("Error writing to device. Will ext");exit(1);}}//}

while(ch!=2){ if(!WriteFile(h,data_buf,1,NULL,NULL)) { printf("Error writing to device. Will ext"); exit(1); }}In this code, the value in ch will never change. so, whatever value is read in will be streamed out forever.

Quote

Serial.println(inp);

Is you C app expecting a response? If not, why are you providing one? If so, why are you not reading it?

How long do you suppose it will take to fill up the serial buffer on the PC side? The Arduino resets when the C app opens the serial port. You immediately begin bombarding with with data. The Arduino's serial buffer will overflow long before the Arduino can process the incoming data. After each byte that is processed, you send a reply to the PC, but never process that reply. Perhaps the C app doesn't just silently discard data, like the Arduino does.

Thanks a lot! Got it to work! There was a null pointer error bcause of the last two NULL's passed to WriteFile()!Even i dont like C the only reason we are using it because we are usinga software called ARToolKit for motion control and its in C!!!