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.

main(){

if( (fp = fopen("COM7:","w+")) == NULL ) { //Arduino is at COM7
printf("\nError\n\n\n");
return 1;
}
Sleep(1000)//ms
while(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 sensor.in 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
fflush(fp);
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;
}
Sleep(500);
fputs(i,ft); //saves valueof temperature in that new file
fclose(ft);
Sleep(50);
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
Sleep(5000);


}
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]

please!

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(){
 Serial.begin(9600); 
 pinMode(13,OUTPUT);
}
char i=0;
void loop(){
  
  if(Serial.available()){
    i=Serial.read();
    Serial.flush();
  }
  
  if(i=='i')
  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