Device Switching Using PC’s Parallel Port

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=0x378;

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>=0x5&&t.ti_hour<0xb)
printf(“\n\n GOOD MORNING….\n”);
if(t.ti_hour>=0xb&&t.ti_hour<0x11)
printf(“\n\n GOOD AFTERNOON….”);
if(t.ti_hour>=0x11&&t.ti_hour<0x16)
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,0x00);
}
else
{
evice[adrr]=0;
outportb(port,adrr);
delay(50);
outportb(port,0x00);
}
}

void exit_code(int hour)
{
clrscr();
printf(“\t\t\t THANK YOU FOR USING THIS SOFTWARE \n”);
if(hour>=0x13||hour<=0x4)
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

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

26 thoughts on “Device Switching Using PC’s Parallel Port

  1. 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

  2. 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

  3. 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 !!!!

  4. We have taken this project & got selected but now there is a problem…………

    I have tried to make the artwork & layout but it does not succeded.
    Kindly help us as it is related to the third year project as soos as possible..Please send the same on my e-mail id OR give us direction!!!!

    Please help us >>>>>> to download the schematic diagram of device switching using pc’s parallel port.

    we have to submit our project upto end of this month… PLEASE HURRY & HELP US….

  5. I would like to build the circuit and test it. Can you please send me the source code. I also do not have a turbo compiler.
    Regards,
    Iqbal

  6. i want to do the project which is control of electrical appliance such asaaaaaa
    a bulb but i have problem with the programmation how can i sort it?

  7. though i liked the project & wanted to go through the entire project but i could not see the last part of the project..every time i clicked on the page links..only the 1st page is getting re-opened..plzz help…i need it…if possible plzzz send me a copy of the entire project in my email-id

  8. when i run the code, it has a lot of error

    i run it in visual studio
    because i have widow 7 and i don’t know how to download it

  9. I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire
    someone to do it for you? Plz answer back as I’m looking to design my own blog and would like to find out where u got this from. kudos

  10. I think that is one of the such a lot significant info for me.
    And i am satisfied reading your article.
    However should remark on few basic things, The web site taste is great, the articles is in reality nice
    : D. Excellent process, cheers

  11. Ahaa, its fastidious conversation concerning this
    post at this place at this website, I have read all that, so at this
    time me also commenting here.

  12. Every weekend i used to visit this site, for the
    reason that i wish for enjoyment, as this this website conations in fact good funny information too.

  13. Failed to get the source code for device switching using parallel port working.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in