Hi Everybody,

FYI (In order to get a feeling with the time it takes to send command strings to signal generators via a GPIB bus, I want to send a voltage level high pulse to an oscilloscope. Ones the RF signal arrives via a detector diode, The pulse and the video signal are triggered and I can compare the timing.)

I've got a twofold problem with the code I've been trying to write:

1. I want the pulse (On the serial Com1 port) coming on just before I send the command string to the signal generator (HPIB). The error I get is that ptr hasn't been initialized although I initialized it as a HANDLE. How can I fix this?

2. In order to let the user enter the command string I first tried to use scanf("%s",). Since this function stops when it encounters a whitespace (which is important in the command string), I tried to solve this with fgets. The problem with fgets was that it doesn't wait for the users input like CIN does in C++. I found the solution wich I used on the internet, with the while loop. The problem I had when creating a switch (to ask whether it is a Query or Command), it continues to look for a "C" (the trigger) after you have chosen Query ("Q"). So after entering the query string it continues with asking to enter the command string:

do{trig = getc(stdin);}while(trig =='\n');  /* Huidig probleem is dat de while loop verdergaat en command na query komt*/

   switch (trig){
   case 'Q':
        printf("%s\n","Enter the Querystring:");
		do{ fgets(command,sizeof(command),stdin);} while(command[0]=='\n');
		ipromptf(id, command, "%t", buf);

		printf("%s\n", buf);
   case 'C':
		printf("%s\n","Enter the commandstring:");
		do{ fgets(command,sizeof(command),stdin);} while(command[0]=='\n');

The code above illustrates the problem 2.

Problem 1. :

#include <stdio.h>         
#include "sicl.h"
#include <string.h>
#define FC_DTRDSR      0x01
#define FC_RTSCTS      0x02
#define FC_XONXOFF    0x04

BOOL            bPortReady;
DCB             dcb;
HANDLE SerialInit(char*);

int main()
   INST id;   /*id is hier de session variable*/             
   char buf[256] = { 0 };
   char adress[4] = { 0 };  
   char adressstring[16] = {0};
   char command[256] = { 0 };
   char trig;
   HANDLE my=SerialInit("com1");

   HANDLE *ptr;
   *ptr = my;
   ionerror(I_ERROR_EXIT);	/*Nog op te zoeken in sicl.h*/ 
   printf("%s\n","Enter the device adress:"); /* Hier bepaalt men welk instrument men via de gpib gaat aanspreken. Er kan een subadres bijkomen, via de komma. Dit is nodig om bijvoorbeeld	een bepaalde switch te verplaatsen*/
   scanf("%s",&adress);    /* scanf gaat de userinput inlezen.  Het is belangrijk te weten dat scanf whitespaces negeert. 
						      En in het geval van een string breekt hij de opslag af wanneer hij een whitespace tegenkomt. 
							  De string (in dit geval) waarnaar scanf de input wegschrijft word aangewezen door de reference & */
   sprintf(adressstring,"gpib0,%s",adress); /* Hier plak ik het bovenstaande adres aan de interface.  Aangezien er hier maar 
                                               1 GPIB kaart is, kan ik deze vast instellen*/
   printf("\n"); /* Om overzicht te bewaren */

   id = iopen(adressstring); /* Dit is een functie van sicl.h die de interface met het opgegeven instrument maakt over de GPIB*/ 

   itimeout(id, 1000); /* de I/O timeout wordt op max 1sec gezet*/
	printf("%s\n","Enter the commandstring:");
	do{ fgets(command,sizeof(command),stdin);} while(command[0]=='\n');

   iclose(id); /*Sessie wordt gesloten (Sicl functie)*/
  // dcb.fRtsControl = RTS_CONTROL_DISABLE;
   return 0;

HANDLE SerialInit(char *ComPortName)
    HANDLE hCom;
    hCom = CreateFile(ComPortName,
        0, // exclusive access
        NULL, // no security
        0, // no overlapped I/O
        NULL); // null template
	    bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes
	    bPortReady = GetCommState(hCom, &dcb);
	    dcb.fOutxCtsFlow = FALSE;                    // turn on CTS flow control
	    dcb.fRtsControl = RTS_CONTROL_DISABLE;    //
	    // set DSRDTR
	    dcb.fOutxDsrFlow = FALSE;                   // turn on DSR flow control
	    dcb.fDtrControl = DTR_CONTROL_DISABLE;   //
	    bPortReady = SetCommState(hCom, &dcb);
	    return hCom;

   void DTRON(HANDLE *hCom)
	   dcb.fDtrControl = DTR_CONTROL_ENABLE;


Edited by Kevin_160: [code]

6 Years
Discussion Span
Last Post by Kevin_160

Answer to the first question: Bool EscapeCommFunction(HANDLE,SETDTR)
Answer to the second question: Put a break; after each case of the switch

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.