NO

Author Topic: how to suppress console  (Read 3496 times)

jschoonh

  • Guest
how to suppress console
« on: May 15, 2012, 10:58:37 AM »
I’m new in C but try to write a little program to write and read to and from a virtual serial port. This is really a USB port with a D/A converter on it containing 8 channels. With a driver, they behave as a serial port. The read contends is written in a .CVS file together with a date and time stamp.
I’ll succeeded! But in console mode. This little program is every vew minutes called by an other program and should run in background. But now you see every time for a second or so the console from this little C program.
Question is, how to prevent that. I’ll tried to run it in windows mode but not successful. Is there anyone who can give me a hind to suppress the console or to run my little program in windows mode?

#include <stdio.h>
#include "hb627.h"
#include "time.h"

#define TIMEOUT             5000 //ms

unsigned char Channel_Number=1, Error;
short int Com_Port_Number=5;
short int U;

int main(void)
{
   
FILE *fp;

  //open com port
  if( !HB627_Open( Com_Port_Number, TIMEOUT ) )
  {
    printf("kan de poort niet openen!\n");
    return 1;
  }

//read value
    if( !HB627_Read_Channel(Channel_Number, &U, &Error) )
    {
      printf("Error: %d;\n", Error);
      return 2;
    }

   printf("%d\n", U);

// time and date in buffer
   time_t rawtime;
     struct tm * timeinfo;
   char buffer [80];
     time ( &rawtime );
     timeinfo = localtime ( &rawtime );
   strftime (buffer,80,"\n%x,%X,",timeinfo);
   
   
// write date, time + data to file
   fp = fopen("Y:\\jaap\\meetdata.cvs", "a");
   fprintf (fp,buffer);
   fprintf(fp, "%d",U);
   fclose(fp);


return 0;
}   
 

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: how to suppress console
« Reply #1 on: May 15, 2012, 12:51:11 PM »
Something like this:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include "hb627.h"
#include "time.h"

#define TIMEOUT             5000 //ms

unsigned char Channel_Number=1, Error;
short int Com_Port_Number=5;
short int U;

//int main(void)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
   FILE *fp;
   char szTmp[100];

  //open com port
  if( !HB627_Open( Com_Port_Number, TIMEOUT ) )
  {
    //printf("kan de poort niet openen!\n");
    MessageBox(0, "kan de poort niet openen!", 0, MB_OK);
    return 1;
  }

//read value
    if( !HB627_Read_Channel(Channel_Number, &U, &Error) )
    {
      //printf("Error: %d;\n", Error);
      wsprintf(szTmp, "Error: %d;\n", Error);
      MessageBox(0, szTmp, 0, MB_OK);
      return 2;
    }

   //printf("%d\n", U);

// time and date in buffer
   time_t rawtime;
     struct tm * timeinfo;
   char buffer [80];
     time ( &rawtime );
     timeinfo = localtime ( &rawtime );
   strftime (buffer,80,"\n%x,%X,",timeinfo);
   
   
// write date, time + data to file
   fp = fopen("Y:\\jaap\\meetdata.cvs", "a");
   fprintf (fp,buffer);
   fprintf(fp, "%d",U);
   fclose(fp);


   return 0;
}
May the source be with you

czerny

  • Guest
Re: how to suppress console
« Reply #2 on: May 15, 2012, 01:10:41 PM »
As a second option:

Why do you terminate and restart your proggy every few minutes?
You can change it a little (loop) to keep it running and minimize it.

czerny

CommonTater

  • Guest
Re: how to suppress console
« Reply #3 on: May 15, 2012, 03:34:57 PM »
Timo's code is correct for a windows program but I believe he forgot to mention that you will have to compile it as a windows program...

Project -> Project options -> Linker tab -> subsystem = Windows.

Also let me add a couple of notes here... It is generally unwise to run "invisible" code. 
Should it become stuck or compromised you will have one he** of a time troubleshooting the problem. 
It will not show up in task manager.
And
It might be tagged as a virus by the end user's AV programs because it writes to a file but has no windows. 

If you have access to the parent program's source code, it would be a lot smarter to include your code as a timed function in the main program itself.


CommonTater

  • Guest
Re: how to suppress console
« Reply #4 on: May 15, 2012, 03:40:53 PM »
As a second option:

Why do you terminate and restart your proggy every few minutes?
You can change it a little (loop) to keep it running and minimize it.

czerny

From the description I get the impression it's being run in response to some event in a parent program. 



jschoonh

  • Guest
Re: how to suppress console
« Reply #5 on: May 15, 2012, 05:50:04 PM »
Really fast responses..

@timovjl thks :).
First attempt an error from prolink so I”ll tried it as a windows subsystem in the linker. It’s works fine now.
@commentater, Thnks it was obvious but you reaction was fast.
The main program source is not easy to read for me. It is a existing program with comment in Francs language. “GraphWeather”
@znerny, I’m open for suggestions, but my thought was: I’ll need this only now and then. The main program calls a script if it’s needs data and via this script I’ll start this proggy. The computer available is an old one with quit some processes running on it. I think that by calling for this proggy I save some resources on this computer.

If your interested in the hole picture peek to my Dutch (sorry) site on the weather pages. Some more info will be displayed via this little project. I’m busy to build a pyranometer and graph the global solar radiation in W per M2.  http://home.planet.nl/~jschoonh/weer.htm

Thanks my problem is solved.