Member Avatar for Kevin_160

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*/
   printf("\n");

   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');
		iprintf(id,command);
   }

The code above illustrates the problem 2.

Problem 1. :

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

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


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');
	DTRON(ptr);
	iprintf(id,command);
   //}

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

HANDLE SerialInit(char *ComPortName)
{
    HANDLE hCom;
     
    hCom = CreateFile(ComPortName,
           GENERIC_READ | GENERIC_WRITE,
        0, // exclusive access
        NULL, // no security
        OPEN_EXISTING,
        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;
	   return;

   }
Member Avatar for 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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.