Device Switching Using PC’s Parallel Port
by Ranjith | November 20th, 2008.SOURCE CODE:
/* PROGRAM TO CONTROL MULTIPLE DEVICES USING PC’S PARALLEL PORT */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<string.h>
FILE *status;
int ch;
char *dev_list[]={“idle”,”TUBELIGHT”,”FAN”,”NIGHTLAMP”,”TELEVISION”,                                           ”AIRCOOLER”,”REFRIGERATOR”,”DVD_PLAYER”,”end” };
int day[]={0,2,4,6,0,1,3,5,7,0};
int night[]={0,1,2,3,5,6,7,0,4,0};
int out[]={0,3,0,1,2,4,5,6,7,0};
int reset[]={0,0,1,2,3,4,5,6,7,0};
int device[256];
int port=0×378;
void activate(int *ptr);
void switch_load(int);
main()
{
int disp_menu();
void disp_load();
void profiles();
void switching();
void load_status();
void exit_code();
int welcome();
int choice;
int hour;
status_file();
for(;;)
{
clrscr();
hour=welcome();
choice=disp_menu();
switch(choice)
{
case 1: disp_load();
printf(“\n\n\n Press any key to continue….”);
getch();
break;
case 2: profiles();
break;
case 3: switching();
break;
case 4: load_status();
break;
case 5: exit_code(hour);
default: printf(“\n\n INVALID CHOICE”);
printf(“\n\n\n\n\n PRESS ANY KEY TO RETURN TO MAINÂ MENU…..”);
getch();
}
}
}
status_file()
{
int i=0;
status=fopen(“status.txt”,”r”);
if(status==NULL)
{
puts(“Unable to open device status file”);
exit(1);
}
while(1)
{
ch=fgetc(status);
if(i>=256)
break;
else
device[i]=ch;
i++;
}
fclose(status);
return(0);
}
welcome()
{
struct time t;
struct date d;
clrscr();
printf(“\n\n\t\t\t $$$ POWER CONTROL USING PC $$$\n\n\n”);
gettime(&t);
getdate(&d);
printf(“\n\n TIME: %2d:%2d”,t.ti_hour,t.ti_min);
printf(“\n\n DATE: %2d/0%d/%2d”,d.da_day,d.da_mon,d.da_year);
if(t.ti_hour>=0×5&&t.ti_hour<0xb)
printf(“\n\n GOOD MORNING….\n”);
if(t.ti_hour>=0xb&&t.ti_hour<0×11)
printf(“\n\n GOOD AFTERNOON….”);
if(t.ti_hour>=0×11&&t.ti_hour<0×16)
printf(“\n\n GOOD EVENING….”);
return(t.ti_hour);
}
int disp_menu()
{
int choice;
printf(“\n 1.LIST OF LOADS & THEIR CODES”);
printf(“\n 2.STANDARD PROFILES(DAY/NIGHT)”);
printf(“\n 3.SWITCHING”);
printf(“\n 4.LOAD’S STATUS”);
printf(“\n 5.EXIT\n”);
printf(“\t\t\t\t ENTER YOUR CHOICE:”);
scanf(“%d”,&choice);
return(choice);
}
void disp_load()
{
int i;
clrscr();
printf(“\n THE LIST OF LOADS WHICH CAN BE CONTROLLEDÂ ARE:\n\n”);
for(i=1;strcmpi(&dev_list[i][0],”end”);i++)
printf(“\n\t %2d.%s”,i,dev_list[i]);
return;
}
void profiles()
{
char p;
for(;;)
{
clrscr();
printf(“\n STANDARD PROFILES:\n”);
printf(“\n\n\t\t 1.DAY MODE”);
printf(“\n\t\t 2.NIGHT MODE”);
printf(“\n\t\t 3.OUT OF STATION”);
printf(“\n\t\t 4.RESET”);
printf(“\n\t\t 5.EXIT”);
printf(“\n\n\n\n ENTER YOUR CHOICE: “);
p=getchar();
switch(p)
{
case ‘d’:
case ‘D’:
case ‘1′:Â activate(day);
return;
case ‘n’:
case ‘N’:
case ‘2′:Â activate(night);
return;
case ‘o’:
case ‘O’:
case ‘3′:Â activate(out);
return;
case ‘r’:
case ‘R’:
case ‘4′:Â activate(reset);
return;
case ‘e’:
case ‘E’:
case ‘5′:Â return;
}
}
}
void activate(int *ptr)
{
int i;
char c;
printf(“\n\nTHE FOLLOWING DEVICES WILL BE SWITCHED ON…..\n”);
for(i=1;ptr[i]!=0;i++)
printf(“\n%d.%s”,ptr[i],dev_list[ptr[i]]);
printf(“\n\nTHE FOLLOWING DEVICES WILL BE SWITCHED OFF…..\n”);
for(i++;ptr[i]!=0;i++)
printf(“\n%d.%s”,ptr[i],dev_list[ptr[i]]);
printf(“\n\n DO YOU WANT TO ACTIVATE THIS PROFILE….(y/n):”);
c=getch();
printf(“%c”,c);
getch();
if(c==’y'||c==’Y')
{
for(i=1;ptr[i]!=0;i++)
if(device[ptr[i]]==0)
switch_load(ptr[i]);
for(i++;ptr[i]!=0;i++)
if(device[ptr[i]]==1)
switch_load(ptr[i]);
}
}
void switching()
{
int i,on_off;
disp_load();
printf(“\n\n\n\nEnter the code of the device to be switched:”);
scanf(“%d”,&i);
printf(“OPTIONS:”);
printf(“\t 0–OFF\t 1–ON\n”);
printf(“\n ENTER YOUR CHOICE:”);
reenter: scanf(“%d”,&on_off) ;
if(on_off==1)
{
if(device[i]==1)
{
printf(“\nThe Device is already ON”);
getch();
}
else
switch_load(i);
}
else if(on_off==0)
{
if(device[i]==0)
{
printf(“\nThe Device is already OFF…”);
getch();
}
else
switch_load(i);
}
else
{
printf(“Invalid option\n Please Re-enter”);
goto reenter;
}
}
void load_status()
{
int i;
clrscr();
printf(“\n\n THE FOLLOWING DEVICES ARE ON:\n”);
for(i=1;strcmpi(&dev_list[i][0],”end”);i++)
if(device[i]==1)
printf(“\n%d.%s”,i,dev_list[i]);
printf(“\n\nTHE FOLLOWING DEVICES ARE OFF:\n”);
for(i=1;strcmpi(&dev_list[i][0],”end”);i++)
if(device[i]==0)
printf(“\n%d.%s”,i,dev_list[i]);
printf(“\n\n\n\nPress any key to continue……”);
getch();
}
void switch_load(int adrr)
{
if(device[adrr]==0)
{
device[adrr]=1;
outportb(port,adrr);
delay(50);
outportb(port,0×00);
}
else
{
evice[adrr]=0;
outportb(port,adrr);
delay(50);
outportb(port,0×00);
}
}
void exit_code(int hour)
{
clrscr();
printf(“\t\t\t THANK YOU FOR USING THIS SOFTWARE \n”);
if(hour>=0×13||hour<=0×4)
printf(“\n\n GOOD NIGHT….\n”);
file_update();
printf(“\n\n\n\n\n\n\n Press any key to EXIT……”);
getch();
exit(0);
}
file_update()
{
int i=0;
status=fopen(“status.txt”,”w+”);
if(status==NULL)
{
puts(“Unable to open device status file”);
exit(1);
}
while(1)
{
ch=device[i];
if(i>=256)
break;
else
fputc(ch,status);
i++;
}
fclose(status);
return 0;
}
CONCLUSION:
This project, can be effectively and conveniently utilized for the control of different appliances. As this project could be extended to control about 255 devices, this could be used for computerization of an office, home, or a firm. Though it is quiet costlier, the circuit is simple and the working mechanism could be easily understood. An added advantage of this project is that we are able to know the status of the device to be controlled. The program to control the appliances is written in C language which is more user friendly and easy to understand than other programming languages.
Disadvantages / Improvements:-
If  at all a new device has to be added to the hardware the software doesn’t support this to achieve which the source code has to be changed manually.
The status of all the devices that are switched are maintained in a database which may not coincide with actual status of the devices as we are not reading the status directly from the HARDWARE.
By
1. Anand Kumar. N. Ilkal
2. Anil Kumar Desai
3. Gara Naveen
4. Umesh. N. Sajjan






July 23rd, 2009 at 5:08 pm
[...] he encontrado esta otra sobre el mismo [...]
July 30th, 2009 at 8:16 am
i want to know the method of switching using microcontrollers
October 17th, 2009 at 12:58 am
PLEASE SAND ME SOURCE CODE OF CONTROLING POWER BY PC IN JAVA LANGUAGE……….
January 13th, 2010 at 10:41 am
it can control device, can it be used to montior voltage and some I/O signal and the same time?
is there RS232 VERSION instead of parallel port?
thank you
January 16th, 2010 at 8:12 am
i want to download the schematic diagram of device switching using pc’s parallel port
January 23rd, 2010 at 3:00 pm
hi.. i want to download the source code of device switching using pc’s parallel port in c#.net sp please forward it to my email-id its urgent require so please please send code to me
February 14th, 2010 at 2:24 am
I have tried the program but it does not give the desired outpt though it prints what you have showed in your screen shots..Kindly help us as it is related to the filnal year project..Please !!!!
March 5th, 2010 at 8:20 pm
[..] A little unrelated, but I quite simply liked this site post [..]