Parallel Port I/O in Windows XP - my program crashes when I run it

This is a discussion on Parallel Port I/O in Windows XP - my program crashes when I run it within the C Programming forums, part of the General Programming Boards category; Hi to anyone reading this thread. My program is to read data from and output data to the Parallel port ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Leicester, England
    Posts
    2

    Question Parallel Port I/O in Windows XP - my program crashes when I run it

    Hi to anyone reading this thread.

    My program is to read data from and output data to the Parallel port in Windows XP, using inpout32.dll since it is not possible to access the port directly.

    The program compiles and links with no errors, but as soon as I run the .exe file, it crashes.

    Can anyone please suggest what I might be doing wrong, and how I might go about fixing it?


    Code:
    //Wireless.c
    //Third Year Project
    //Written by G. Woodhead, with help from www.cprogramming.com and www.asciitable.com
    //12/02/08
    //
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <windows.h>
    //
    //global constants declarations
    #define OutPort    0x378        //port to output data
    #define InPort     0x379        //port to receive data
    #define Control    0x37A        //port to control devices
    
    #define Tx         0            //Transmitter
    #define Rx         1            //Receiver
    #define clockR     3            //Receiver + clock pulse
    #define load       4            //load AND gates of PISO shift registers
    #define clockT     8            //Transmitter + clock pulse
    #define start      0x80         //start bit
    
    //Function prototype declarations
    int Input_keyboard (void);
    int Transmit (void);
    int Receive (void);
    int Display (const int);
    
    /* Definitions in the build of inpout32.dll are:            
         short _stdcall Inp32(short PortAddress);               
         void _stdcall Out32(short PortAddress, short data);
    
    
       prototype (function typedef) for DLL function Inp32: */
    
         typedef short _stdcall (*inpfuncPtr)(short portaddr);
         typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum);
    
    char buf[BUFSIZ];               //string to hold keyboard input
    char *p;                        //pointer to string holding keyboard input
    
    //array to hold hex equivalents of ASCII input from keyboard
    int hex [63] = {0x20, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 
    				0x46, 0x47 ,0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 
    				0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 
    				0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A};
    
    //string to hold ASCII codes
    char ascii [63] = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    int main(void)
    {
    	int signal = 0;
    	int data = 0;
    	int a = 0;
    	HINSTANCE hLib;
         inpfuncPtr inp32;
         oupfuncPtr oup32;
    
         hLib = LoadLibrary("inpout32.dll");                     // Load the library
    
         // get the address of the function
         inp32 = (inpfuncPtr) GetProcAddress(hLib, "Inp32");
         oup32 = (oupfuncPtr) GetProcAddress(hLib, "Out32");
     
    	while (a < 100000)                                       //run for a while                                                  
    	{
    		  while (!kbhit())                                   //while no keyboard input
    		  {
    				signal = (inp32) (InPort);                   //check for start bit
    				signal = (signal & 0x08);
    				if (signal == 0x08)
    				{
    					   data = Receive();                     //read in data
    					   Display (data) ;                      //display received text on screen
    				}
    		  }      
    		  Input_keyboard();                                  //call function to read text from keyboard 
    		  (oup32) (Control, Tx);                             //select transmitter
    		  Transmit();                                        //call function to transmit text
    		  a++;                                               //increment a
    	}
        FreeLibrary(hLib);      
    	return 0;
    }
    
    //==========================================================================================================================================================
    //Function: Input_keyboard - to get data from keyboard
    
    int Input_keyboard (void)
    {
    	printf("Please enter a line of text, using only alphanumeric keys and the spacebar, max %d characters\n", sizeof(buf));         //read in text from keyboard
    	fgets(buf, sizeof(buf), stdin);
    	if ((p = strchr(buf, '\n')) != NULL)            //if there is a 'newline' character at the end of buf, change it to a full stop
    	{
    		   *p = '.';
    	}
    	return 0;
    }
    
    //==========================================================================================================================================================
    //Function: Transmit - send data from keyboard buffer to parallel port for transmission and control 8-bit output shift register
    
    int Transmit (void)
    {
        HINSTANCE hLib;
         inpfuncPtr inp32;
         oupfuncPtr oup32;
    
         hLib = LoadLibrary("inpout32.dll");                     // Load the library
    
         // get the address of the function
         inp32 = (inpfuncPtr) GetProcAddress(hLib, "Inp32");
         oup32 = (oupfuncPtr) GetProcAddress(hLib, "Out32");
     
    	int b = 0;
    	int c = 0;
    	int d = 0;
    	int e = 0;
    	char character;
    	for (e = 0; e < BUFSIZ; e++)
    	{
    		(oup32) (OutPort, start);                 //send start bit
    		(oup32) (Control, load);                  //load flip-flops
    		(oup32) (Control, clockT);                //send transmitter clock output high
    		sleep (1);                                //wait 1ms
    		(oup32) (Control, 0);                     //send transmitter clock output low
    		sleep (1);                                //wait 1ms
    		character = buf[d];                       //get next character from string
    		if (character == '.')
    		{
    				break;
    		}
    		for (c = 0; c < 63; c++)
    		{
    			if (character == ascii[c])
    			{
    				(oup32) (OutPort, hex[c]);
    				break;
    			}
    		}
    		d++;
    		(oup32) (Control, load);
    		for (b = 0; b < 8; b++)
    		{
    			(oup32) (Control, clockT);
    			sleep (1);
    			(oup32) (Control, 0);
    			sleep (1);
    		}
    	}
    	return 0;
    }    
    
    
    //==========================================================================================================================================================
    //Function: Receive - read in data from parallel port and control 4-bit input shift register
    
    int Receive (void)
    {
        HINSTANCE hLib;
         inpfuncPtr inp32;
         oupfuncPtr oup32;
    
         hLib = LoadLibrary("inpout32.dll");                     //Load the library
    
         //get the address of the function
         inp32 = (inpfuncPtr) GetProcAddress(hLib, "Inp32");
         oup32 = (oupfuncPtr) GetProcAddress(hLib, "Out32");
     
    	int nibble_L = 0;
    	int nibble_H = 0;
    	int byte = 0;
    	int f = 0;
    	int g = 0;
    	
    	for(f = 0; f < 4; f++)                               //clock shift register 4 times to receive 1 nibble
    	{
    		  (oup32) (Control, clockR);                     //send clock output high
    		  sleep (1);                                     //wait 1ms
    		  (oup32) (Control, Rx);                         //send clock output low
    		  sleep (1);                                     //wait 1ms
    	}
    	nibble_L = (inp32) (InPort);                         //read in lower nibble of current character
    	nibble_L = nibble_L >> 4;                            //shift right 4 times since nibble read in from upper 4 lines of input port
    	for (g = 0; g > 4; g++)                              //clock shift register 4 times to receive 1 nibble
    	{
    		  (oup32) (Control, clockR);                     //send clock output high
    		  sleep (1);                                     //wait 1ms
    		  (oup32) (Control, Rx);                         //send clock output low
    		  sleep (1);                                     //wait 1ms
    	}
    	nibble_H = (inp32) (InPort);                         //read in upper nibble of current character
    	byte = (nibble_H & 0xF0) | (nibble_L & 0x0F);        //form byte from upper and lower nibbles
        return byte;
    }
    
    //==========================================================================================================================================================
    //Function: Display - convert received binary data to ASCII and display on screen
    
    int Display (const int value)
    {
    	int h;
    	for (h = 0; h < 63; h++)                  
    	{
    		if (value == hex[h])                  //find code in hex array that corresponds to received code
            {
    			 printf ("%s", ascii[h]);         //print ASCII equivalent of received code to screen
    			 break;                           //exit from loop when correct code is found
    		}
    	}    
    	return 0;
    }
    I've no clue what I could've done wrong, so any help would be much appreciated
    Thanks in advance

    Z'Ha'Dum

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    What device are you trying to communicating with?

    gg

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    I don't understand why you have to open the library again for each transmit/receive operation.
    Shouldn't it be sufficient to open it once in main, and load the pointers to the functions you need there to some global fields, as a quick and dirty approach?

    You also are making a mistake in Display()
    Code:
    int Display (const int value)
    {
    	int h;
    	for (h = 0; h < 63; h++)                  
    	{
    		if (value == hex[h])                  //find code in hex array that corresponds to received code
            {
    			 printf ("&#37;s", ascii[h]);         //print ASCII equivalent of received code to screen
    			 break;                           //exit from loop when correct code is found
    		}
    	}    
    	return 0;
    }
    %s should be %c.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Location
    Leicester, England
    Posts
    2
    @Codeplug
    Its a board I built myself for my project, with a PISO register connecting to the data port and a SIPO shift register connecting to the status port, which then go to radio transmitters and receivers respectively, as its supposed to communicat via radio with another PC.

    @xuftugulus
    Ah cheers I'll change that, I think I was initially planning to print the whole received message as a string, but then decided it would be easier to print it a character at a time as it was received.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  3. DEV-C++ made program crashes when run from outside the ide
    By rainmanddw in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 10:27 PM
  4. Windows 98/2000 programming in Windows XP
    By Bill83 in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 03:16 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21