File transfer via Parrallel Port using C++

Printer-friendly versionPDF version

All my retro college work, or atleast the mentionable ones, I plan to put online. This here is something probably quite useless but nevertheless a nice programming exercise. This is probably from my 4th or 5th semester of bachelors. If you have 2 computers lying around with parrallel ports and of course, more importantly a parrallel port cable, you could probably give it a go. Also I am not sure about C++ code portability i.e. it working fine on linux, this use to work fine on windows 98 machines back than on my pentium 3 or something. According to the filename and my terrific bug management system from back than, it says there is some sort of "filling bug". For the life of me, I don't remember anymore but it was good enough to get me a nice score back than on the assignment. Feel free to use it if you want to with or without credit to me. Download the file for actual code, the editor seems to eat up some lines instead of showing them.

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

void showFiles();
void initialize();
void synchronize();
void sendFile(char *);
void sendChar(char byte);
char recieveChar();
void interface();
void checkInput();
void getFiles(int );
void recieveFile();
void checkPort();
void drawWindows();
void showFiles();
void prompt();


void main()
{
	clrscr();
	initialize();
	interface();
	prompt();
	while(1)
	{
		checkPort();
		checkInput();
	}
}

void checkInput()
{
	char response ;
	char filename[12];

	if(kbhit())
	{
		response = getch();
		if(response == '1')
		{
			outportb(0x378,0x13);
			synchronize();
			recieveFile();
			getFiles(0);
			clrscr();
			interface();
			prompt();
		}
		else if(response == '2')
		{
			outportb(0x378,0x12);
			synchronize();
			gotoxy(26,1);
			cout << "Enter name of file :- ";
			cin >> filename;
			sendFile(filename);
		}
		else if(response == '3')
		{
			outportb(0x378,0x11);
			synchronize();
			cout << "Enter name of file :- ";
			cin >> filename;
			char len = strlen(filename);
			sendChar(len);
			for(int i=0; i < len; i++)
			{
				sendChar(filename[i]);
			}
			recieveFile();
		}
		else if(response == '4')
		{
			exit(0);
		}
	}

}

void checkPort()
{
		int value = 0;
		char byte = 0, *name=NULL;
		value = inportb(0x379);
		value >>= 3;
		value &= 0x1F;
		if(value)
		{
			switch(value)
			{
				case 0x01:    // copy
					outportb(0x378,0x1F);
					while(1)
					{
						value = inportb(0x379);
						value >>= 3;
						value &= 0x1F;
						if(value == 0)
							break;
					}
					initialize();
					byte = recieveChar();
					name = new char[byte+1];
					for( char i=0; i < byte ; i++)
					{
						name[i] = recieveChar();
					}
					name[i] = NULL;
					sendFile(name);
						break;

				case 0x02:        //send
					outportb(0x378,0x1F);
					while(1)
					{
						value = inportb(0x379);
						value >>= 3;
						value &= 0x1F;
						if(value == 0)
							break;
					}
					initialize();
					recieveFile();
					getFiles(0);
						break;


				case 0x03:        //dir
					outportb(0x378,0x1F);
					getFiles(1);
					while(1)
					{
						value = inportb(0x379);
						value >>= 3;
						value &= 0x1F;
						if(value == 0)
							break;
					}
					initialize();
					sendFile("file1.txt");
					interface();
					prompt();
						break;

				default :
						break;

			}

		}


}

void initialize()
{
	getFiles(0);
	outportb(0x378,0x10);
}


void sendChar(char byte)
{
	char sByte = NULL;
	char value = NULL;
	sByte = byte & 0x0F ;
	outportb(0x378, sByte);
	//Acknowledge();
	while(1)
	{
		value = inportb(0x379);
		value >>= 3;
		value &= 0x1F;
		if(value == 0x1F)
			break;
	}

	sByte = byte >> 4 ;
	sByte |= 0x10;
	outportb(0x378, sByte);
	//Acknowledge();
	while(1)
	{
		value = inportb(0x379);
		value >>= 3;
		value &= 0x1F;

		if(value == 0x00)
			break;
	}

}

char recieveChar()
{
	char value = NULL;
	char a = NULL;

	while(1)
	{
		a = inportb(0x379);
		//a &= 0x10;
		a >>= 3;
		a &= 0x1F;

		if(a & 0x10)
			break;
	}

	a &= 0x0F;
	value = a;

	outportb(0x378, 0x0F);

	while(1)
	{
		a = inportb(0x379);
		//a &= 0x10;
		a >>= 3;
		a &= 0x1F;
		if((a & 0x10) == 0)
			break;
	}
	outportb(0x378, 0x10);
	a <<= 4;
	a &= 0xF0;
	value |= a;

	return value;

}

void getFiles(int no)
{

   struct ffblk ffblk;
   int done;
   char filename[12] = "";

   if(no == 0)
	  strcpy(filename,"file.txt");
   else if(no == 1)
	  strcpy(filename,"file1.txt");

   fstream a(filename, ios::out | ios::trunc );
   done = findfirst("*.*",&ffblk,0);

   while (!done)
   {
	  a. write(ffblk.ff_name, strlen(ffblk.ff_name));
	  a. write( "\n", sizeof(char));
	  done = findnext(&ffblk);
   }
   a.close();

}

void sendFile(char * name)
{
	FILE * a = NULL;
	char byte = NULL;
	int handle =  open(name, O_RDONLY);
	long len , size;
	len = size = filelength(handle);
	close(handle);

	initialize();

	a = fopen(name ,"r");
	if(a == NULL)
	{
		cout << "File could not opened";
		return;
	}

	char b = strlen(name);
	sendChar(b);
	for( char i=0; i < b ; i++)
	{
		sendChar(name[i]);
	}

	sendChar(char(len));
	sendChar(char(len >>= 8));
	sendChar(char(len >>= 8));
	sendChar(char(len >>= 8));

	for(long j=0; j < size ; j++)
	{
		fread(&byte,sizeof(char), 1, a);
		sendChar(byte);
	}
	initialize();
	fclose(a);
}

void recieveFile()
{
	FILE * a = NULL;
	char byte = NULL, *name;
	long len = 0;

	initialize();

	byte = recieveChar();
	name = new char[int(byte)+1];
	for( char i=0; i < byte ; i++)
	{
		name[i] = recieveChar();
	}
	name[i] = NULL;

	a = fopen(name ,"w+");
	if(a == NULL)
	{
		cout << "File could not copied";
		return;
	}

	byte = recieveChar();
	len = long(byte) & 0x000000FF;

	byte = recieveChar();
	len |= ((long(byte) << 8) & 0x0000FF00);

	byte = recieveChar();
	len |= ((long(byte) << 8) & 0x00FF0000);

	byte = recieveChar();
	len |= ((long(byte) << 8) & 0xFF000000);

	for(long j=0; j < len ; j++)
	{
		byte = recieveChar();
		fwrite(&byte,sizeof(char), 1, a);
	}
	initialize();
	fclose(a);
}


void synchronize()
{
	char byte = NULL;
	while(1)
	{
		byte = inportb(0x379);
		byte >>= 3;
		byte &= 0x1F;
		if(byte == 0x0F)
			break;
	}
	initialize();
	while(1)
	{
		byte = inportb(0x379);
		byte >>= 3;
		byte &= 0x1F;
		if(byte == 0)
			break;
	}
}

void interface()
{
	clrscr();
	textcolor(1);
	textbackground(7);
	drawWindows();
	textcolor(4);
	textbackground(0);
	showFiles();
	gotoxy(1,19);
}
void drawWindows()
{
	gotoxy(0,0);

	//Top Line
	cprintf("Files");
	for(int i = 5;i<80;i++)
	{
		cprintf("²");
	}
	//Middle Vertical Line Line
	gotoxy(40,2);
	for(i = 0;i<18;i++)
	{
		cprintf("³");
		gotoxy(40,i+2);
	}
	//Left Vertical Line:
	gotoxy(1,2);
	for(i = 0;i<18;i++)
	{
		cprintf("³");
		gotoxy(1,i+2);
	}
	//Right Vertical Line:
	gotoxy(80,2);
	for(i = 0;i<18;i++)
	{
		cprintf("³");
		gotoxy(80,i+2);
	}

	//Bottom Line.
	gotoxy(1,18);
	for(i = 0;i<80;i++)
	{
		cprintf("²");
	}

}


void showFiles()
{
	fstream cur("file.txt",ios::in);
	fstream other("file1.txt",ios::in);
	if(!cur)
	{
		gotoxy(2,2);
		cout<<"File not opened";
	}
	if(!other)
	{
		gotoxy(42,2);
		cout<<"File not opened";
	}

	char * temp;
	temp = new char[20];

	for(int i=0;i<12&&(!cur.eof());i++)
	{
		cur>>temp;
		gotoxy(2,i+2);
		cprintf(temp);
	}
	for(i=0;i<12&&(!cur.eof());i++)
	{
		other>>temp;
		gotoxy(41,i+2);
		cprintf(temp);
	}
	cur.close();
	other.close();

}


void prompt()
{
	gotoxy(1,19);
	delline();
	delline();
	delline();
	delline();
	delline();
	gotoxy(1,19);
	cout<<"1. Move\n2. Copy\n3. Refresh\n4. Exit\n";
	cout<<"Enter choice : ";
}

 

Tags:

Top level category:

Add new comment