image001_small.png

Introduction

This article describes a security software system. Suppose you have no skills or soldering equipment, but you want to provide security for the apartments. We just mount button to the door and connect it directly to the COM port of the PC (2nd and 3rd pins).

image003.png

We start the program and press the “Start” button. In case the door is opened, speakers will provide a siren sound. The project is written as some kind of “toy software”.

Background

Once as a student living in a hostel, the door lock suddenly failed. Since at that time I stayed alone in the room, I'd have to sleep with the “open” door. At this point, I got the idea to make an open door alarm tool. All I had was a piece of wire and a computer...

Using the Code

The project is written using “Borland C++ Builder 6”. The main idea of this project is in fact that if we are transmitting signals through the COM port TX line to the RX line of the same port, it is possible to catch the transmitted data. So, if contact is broken (button is unpressed, door is opened), data wouldn’t come back.

The algorithm of the program is as follows:

  • Open COM port.
  • Send byte of data to the TX line.
  • Try to receive data from the RX line.
  • Check the received data. If received data differs from the transmitted – turn on siren sound.
void __fastcall TFormMain::ButtonStartClick(TObject *Sender)
{
         FormMain->LabelStatus->Caption="Working";
         FormMain->TimerMain->Enabled=true;
 
         ComPort_PurgeCom();
 
         ComPort_PutByte(0xff);
 
         if(ComPort_GetByte(1))
         {
                 FormMain->PanelMain->Caption="All right!";
                 FormMain->Color=0x000080FF;
         }
         else
         {
                 FormMain->PanelMain->Caption="ALARM!!!";
 
                 //ALARM!!!
                 DWORD fdwSound = SND_ASYNC | SND_FILENAME;
                 PlaySound("sounds/ALARM.wav",NULL, fdwSound);
 
                 FormMain->Color=clRed;
         }
}

To avoid unauthorized access to the software, I’ve made an authorization routine.

void __fastcall TFormAutentification::ButtonCheckClick(TObject *Sender)
{
        AnsiString login="Piligrim";
        AnsiString password="1111";
 
        if(login==EditLogin->Text)
        {
                if(password==EditPassword->Text)
                {
                        FormAutentification->PanelStatus->Color=clGreen;
                        Access();
                }
                else
                {
                        FormAutentification->PanelStatus->Color=clRed;Decline();
                }
        }
        else
        {             
                 FormAutentification->PanelStatus->Color=clRed;
                 Decline();
        }
 
        FormAutentification->TimerMain->Enabled=true;
}

The default login: Piligrim

The default password: 1111

Since COM port number and the baud rate are changeable values, it was necessary to provide an additional form where the obvious values could (and should) be put on.

void __fastcall TFormSettings::ButtonCheckClick(TObject *Sender)
{        
         //checking of the edits "emptiness"
         if(EditCom->Text.IsEmpty())
         {
                 EditCom->Text="Com1";
         }
 
         if(EditBaud->Text.IsEmpty())
         {
                 EditBaud->Text="9600";
         }
 
         //to close the port
         ComPort_Close();
 
         //entering the number of the comport
         AnsiString ComPort=EditCom->Text;
         char *ComP = ComPort.c_str();
 
         //entering the baudrate of the comport
         AnsiString BaudRate=EditBaud->Text;
         unsigned long Baud = BaudRate.ToDouble();
 
         BOOL bOpened;
         ComPort_PresetParameters(Baud,8,NOPARITY,ONESTOPBIT);// optional function
         bOpened = ComPort_Open(ComP);
 
         if(bOpened!=0)
         {
                 EditStatus->Text="Ready";
 
                 //wellcome note
                 DWORD fdwSound = SND_ASYNC | SND_FILENAME;
                 PlaySound("sounds/wellcome.wav",NULL, fdwSound);
 
                 FormSettings->Close();
         }
         else
         {
                 EditStatus->Text="Bussy";
 
                 //error note
                 DWORD fdwSound = SND_ASYNC | SND_FILENAME;
                 PlaySound("sounds/error.wav",NULL, fdwSound);
         }
}

As we can see from the code, in case the chosen COM port is ready, the “welcome” sound rises, otherwise the “error” sound appears. To get the list of accessible COM ports in your PC, just type “mode” command in the “cmd.exe” shell. You will get something like:

image005.png

Points of Interest

Now we see that it is quite easy to provide a security device like that. Usage of the sound effects is pretty good, but it is not enough. A more interesting thing would be to create a report to the web server (for instance, it could be a mail report). But that will be done in a future article and it seems to me that using C# programming language would be more convenient for such kind of tasks.

History

  • 18th May, 2011: Initial version
推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架