Simple problem connecting to a windows .c program... not simple for me :(

Hi, i created in .C a program on a windows pc to connect to arduino just like the serial monitor, the problem is that it only works after i open at least once the arduino serial monitor everytime i power off arduino.

If i do not open arduino serial monitor first and then close it i cannot make my program work...

Here is my code, not big.


if( (fp = fopen("COM7:","w+")) == NULL ) { //Arduino is at COM7
return 1;

fputs("i",fp); //send byte to arduino, where i have if (serial.avaiable) waiting, when it recieves data it will execute a serial.print with some value i want from a this case it is a temperature.
fflush( fp );
Sleep(2000);//waits to recieve data
fread(i,sizeof(char),2,fp); // read data, 2 digit temperature. if i dont open serial monitor first it freezes here because if erase this line it continues doing the rest
if( (ft = fopen("C:/Users/Public/Documents/temp.txt","w")) == NULL ) { // Opens a new file to save the temperature, opens a new one everycicle and erases the old one because of the "w+"
printf("\nerror \n\n\n");
return 1;
fputs(i,ft); //saves valueof temperature in that new file
system("C:/Putty/pscp -pw ###### C:/Users/Public/Documents/temp.txt servidor:WWW");//sends files using pscp to server.
printf("Temperatura:\t%s\n",i); //prints temperature value

return 0;

The problem only happens if i power off arduino and then do not open the serial monitor first, before i run myprog.exe

Maybe the serial monitor configures the PORT to transfer data ? and thats why i cannot connect if i dont open it first and then close.?

Does anyone nows how to fix this ? I would be really gratefull!!!

I tested this in 2 diff. computers and it happens the same thing.

Thanks for reading guys !!!

You are opening the serial port as a file, but you are not setting any of the serial port attributes, such as baud rate, stop bits, etc. The serial monitor does that.

There are C functions that do set the serial port properties. You need to dig into how to do that.

[ code] your code [ /code]


On windows cmd : mode com7: baud=9600 parity=n data=8 rts=off

First i typed mode com7 and it came

baud 1200 1 parity 1 stop bit 7 data bits, and dtr and rts off

So i started serial monitor, closed it and ran the same command and came up with

baud 9600 parity none 8 data bits dtr on and rts off.

After a little search i found that command and added it to my .c program :D using system("mode com7: baud=9600 parity=n data=8 rts=off") and all not .works now

and all works now

Great. Thanks for posting your solution.

Sorry my bad i really thought it was working but i had opened the serial monitor before !!! dam im stupid sometimes...

I did some testing and i created a simple program for arduino like this It just waits for data and when it recieves an " i " it lights led

void setup(){
char i=0;
void loop(){
  digitalWrite(13, HIGH);

And a simple code in c++ that just sends every second a char via fputs(fp) -- fp is the pointer to the COM7 its basicaly the same as first program. It really only lights the led if i send an " i " so it must be correct configured... i gess

So all this fancy code actually Worked and arduino can read what i write but not the otherway arround !!!

I cannot read what arduino sends via serial.print !!!! only if i start serial monitor first and close it.

So dunno what it can be more, settings cant be wrong because it is actually recieving something so now i am out of options.

I do not want to keep opening/closing serial monitor everytime i