Thread: need help understanding SleepEx in the code

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    need help understanding SleepEx in the code

    Hey guys, I have the following function:
    Code:
    int BstCommand(int bat, int port, short line) 
    {
     	int card;
     	int status;
    
    	printf("OUT: %x\n",line);	
    //sets switch low
    	status = DIG_Out_Line(card, port, line, 0);
    	SleepEx(100, FALSE);
    //sets switch high
    	status = DIG_Out_Line(card, port, line, 1);
    	
    	return 0;
    }
    ...
    short  DIG_Out_Line (short device, 
                                   short port, 
                                   short line, 
                                   short lineState)
    {	 
    		   int li=(int)line;
    		   int d =(int)&device;
    			d=d %10;
    		 GblDeviceLineState[d][li]=(long)lineState;
    	
    
    	*GblDeviceData[d]=lineState;
    	return 0;
    }
    BstCommand is called everytime a switch is toggled. Dig_out_line sets the state of the switch as high or low. The problem is I don't exactly understand how sleepex here works. It prevents the switch to high line from running depending on I/O and APC calls. But right now the last line always runs, so I always get high for the switch.
    I'm trying to emulate a virtual switch here using code that deals with real switches. I can only modify dig_out_line, but not Bstcommand. What can I do?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    Doesn't National Instrument supply the appropriate documentation

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    SleepEx Function (Windows)

    It's amazing what you can find on a search engine with the right mysterious keywords. (*cough* sleepex *cough*)


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    for national instruments documentation, yes DIG_Out_Line is well documented, I have no problem with what it does. However, I am writing a dummy version of the same function that takes in the input and splits out data as if the national instrument hardware is connected. Thus, I can do whatever I want in this dummy function. The problem is with BstCommand.

    I have seen the documentations for sleepex, but I just don't understand it. I don't see how APC or I/O calls work here, especially in a virtualized environment.

    BstCommand is not a function by national instrument, but rather something someone else wrote without any docs. I'm simply trying to deal with understanding his code.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Does this code run on a Windows machine, or did someone else use the SleepEx name for their very own?

    If it does run on Windows, then it does what it says on the tin: it makes the process go away for 100 ms, and since the second parameter is FALSE, it won't wake up for I/O or for anything other than the timer going off.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    this code runs on a windows machine. and sleepex is the same windows function, unchanged. This code worked for the actual hardware, so I'm just not understanding how.

    Everytime the switch is toggled, the Bst changes from
    BstCommand(bat,4,6) to BstCommand(bat,5,7), so the line and port alternates between those values. There is not actual high or low input for the BstCommand so I assume that the sleepex plays a great role here in deciding whether or not to continue to the switch to high line.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    SleepEx cannot change the values of the parameters that were passed in to a completely different function. The four/six and five/seven come from the place where BstCommand was called; they cannot come from inside the function.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> int d =(int)&device;
    Probably not what you intended.

    gg

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tianshiz View Post
    Hey guys, I have the following function:
    [BstCommand is called everytime a switch is toggled. Dig_out_line sets the state of the switch as high or low. The problem is I don't exactly understand how sleepex here works. It prevents the switch to high line from running depending on I/O and APC calls. But right now the last line always runs, so I always get high for the switch.
    I'm trying to emulate a virtual switch here using code that deals with real switches. I can only modify dig_out_line, but not Bstcommand. What can I do?
    This code...
    Code:
    	status = DIG_Out_Line(card, port, line, 0);
    	SleepEx(100, FALSE);
    	status = DIG_Out_Line(card, port, line, 1);
    Forms a one-shot multivibrator where for each transition of the switch (entry into the function) the ouput line is toggled low for 100 milliseconds then reset to high... equivalent to the q-not line on a 74hc123 logic chip. When the procedure is entered the bit is forced low (0), SleepEx() incurs a time delay of 100 milliseconds then the bit is forced high (1), where it will stay until the next pass through the function...

    So SleepEx() dispite it's somewhat cryptic description on MSDN is just a time delay, in this case.
    Last edited by CommonTater; 09-06-2011 at 07:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help understanding the following code
    By GTTDi in forum C Programming
    Replies: 1
    Last Post: 10-15-2010, 07:28 AM
  2. Help understanding a Code
    By shiroaisu in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2010, 10:05 AM
  3. code understanding
    By elwad in forum C Programming
    Replies: 5
    Last Post: 04-18-2009, 06:57 AM
  4. Help understanding a code
    By lolguy in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-15-2009, 04:13 PM
  5. help me understanding this code
    By apfelsaft in forum C Programming
    Replies: 2
    Last Post: 01-14-2004, 04:46 PM