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