//--------------------------------------------------------------------------- #include #pragma hdrstop #include #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" #define CLEAR_TEXT 1 //the messages are just be pushed to the list box #define AQUIRING 2 //recieving the status of the inputs #define HELLO 3 //attampting to open the POD check for hello MSG #define CONFIRM 4 //we send a command we are awaiting a CR acknowledgement #define SEARCHING 5 //we are attempting to autodetect the POD #define FOUND 6 //POD was located while attempting to autodetect TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Commo = new TCom; Commo->OnReadCom = &ReadCom; toPOD = fromPOD = 0; connected = false; AutoGroup->Visible = false; AddrGroup->Visible = false; SendGroup->Visible = false; } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TForm1::ReadCom(AnsiString Data) { switch(mode) { case AQUIRING: mode = CONFIRM; inReceived(Data); break; case CLEAR_TEXT: Messages->Lines->Add(Data); break; case HELLO: mode = CLEAR_TEXT; connected = true; //need to open up the settings and the send box AddrGroup->Visible = true; SendGroup->Visible = true; break; case CONFIRM: mode = CLEAR_TEXT; break; case SEARCHING: mode = FOUND; connected = true; //need to open up the settings and the send box AddrGroup->Visible = true; SendGroup->Visible = true; break; } } void __fastcall TForm1::ConnectBTNClick(TObject *Sender) { if (ConnectBTN->Caption == "Connect") { Commo->SetBaud(9600); Commo->OpenCom(StrToInt(PortEdit->Text)); mode = HELLO; Commo->WriteCom("H"); ConnectBTN->Caption = "Disconnect"; //need to open up the autodetect box AutoGroup->Visible = true; } else { Commo->CloseCom(); ConnectBTN->Caption = "Connect"; //close the boxes AutoGroup->Visible = false; AddrGroup->Visible = false; SendGroup->Visible = false; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Outpanel0Click(TObject *Sender) { if(connected) { toPOD ^= 0x01; Outpanel0->Color = (Outpanel0->Color == clRed) ? clLime : clRed; if (mode == CLEAR_TEXT) outToPod(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Outpanel1Click(TObject *Sender) { if(connected) { toPOD ^= 0x02; Outpanel1->Color = (Outpanel1->Color == clRed) ? clLime : clRed; if (mode == CLEAR_TEXT) outToPod(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Outpanel2Click(TObject *Sender) { if(connected) { toPOD ^= 0x04; Outpanel2->Color = (Outpanel2->Color == clRed) ? clLime : clRed; if (mode == CLEAR_TEXT) outToPod(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Outpanel3Click(TObject *Sender) { if(connected) { toPOD ^= 0x08; Outpanel3->Color = (Outpanel3->Color == clRed) ? clLime : clRed; if (mode == CLEAR_TEXT) outToPod(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Outpanel4Click(TObject *Sender) { if(connected) { toPOD ^= 0x10; Outpanel4->Color = (Outpanel4->Color == clRed) ? clLime : clRed; if (mode == CLEAR_TEXT) outToPod(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Outpanel5Click(TObject *Sender) { if(connected) { toPOD ^= 0x20; Outpanel5->Color = (Outpanel5->Color == clRed) ? clLime : clRed; if (mode == CLEAR_TEXT) outToPod(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Outpanel6Click(TObject *Sender) { if(connected) { toPOD ^= 0x40; Outpanel6->Color = (Outpanel6->Color == clRed) ? clLime : clRed; if (mode == CLEAR_TEXT) outToPod(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Outpanel7Click(TObject *Sender) { if(connected) { toPOD ^= 0x40; Outpanel7->Color = (Outpanel7->Color == clRed) ? clLime : clRed; if (mode == CLEAR_TEXT) outToPod(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::outToPod() { mode = CONFIRM; Commo->WriteCom("o" + IntToHex(toPOD, 2)); do { Application->ProcessMessages(); }while(mode == CONFIRM); } void __fastcall TForm1::SendBTNClick(TObject *Sender) { Commo->WriteCom(SendEdit->Text); } //--------------------------------------------------------------------------- void __fastcall TForm1::HelloBTNClick(TObject *Sender) { Commo->WriteCom("H"); } //--------------------------------------------------------------------------- void __fastcall TForm1::VersionBTNClick(TObject *Sender) { Commo->WriteCom("V"); } //--------------------------------------------------------------------------- void __fastcall TForm1::ResendBTNClick(TObject *Sender) { Commo->WriteCom("N"); } //--------------------------------------------------------------------------- void __fastcall TForm1::ChangeBTNClick(TObject *Sender) { Commo->WriteCom("POD=" + IntToHex(AddrEdit->Text.ToInt(), 2)); } //--------------------------------------------------------------------------- void __fastcall TForm1::detectBTNClick(TObject *Sender) { int count = 0; char command[5]; DWORD stop; mode = SEARCHING; do { count ++; Commo->WriteCom("!" + IntToHex(count, 2)); AutoLabel->Caption = "Looking for Pod on " + IntToHex(count, 2); stop = GetTickCount() + 300; do Application->ProcessMessages(); while (GetTickCount() < stop); }while (count < 256 && mode == SEARCHING); if (mode == FOUND) AddrEdit->Text = IntToHex(count, 2); mode = CLEAR_TEXT; } //--------------------------------------------------------------------------- void __fastcall TForm1::StopBTNClick(TObject *Sender) { mode = CLEAR_TEXT; } //--------------------------------------------------------------------------- void __fastcall TForm1::StartBTNClick(TObject *Sender) { if (StartBTN->Caption == "Start Aquiring Data") { AutoGroup->Enabled = false; AddrGroup->Enabled = false; SendGroup->Enabled = false; Timer1->Enabled = true; StartBTN->Caption = "Stop Aquiring Data"; } else { AutoGroup->Enabled = true; AutoGroup->Enabled = true; AutoGroup->Enabled = true; Timer1->Enabled = false; mode = CLEAR_TEXT; StartBTN->Caption = "Start Aquiring Data"; } } //--------------------------------------------------------------------------- void __fastcall TForm1::inReceived(AnsiString Data) { if (Data.Length() == 0) return; Data.Insert("0x", 1); fromPOD = Data.ToInt(); InputPanel0->Color = (fromPOD & 0x01) ? clLime : clRed; InputPanel1->Color = (fromPOD & 0x02) ? clLime : clRed; InputPanel2->Color = (fromPOD & 0x04) ? clLime : clRed; InputPanel3->Color = (fromPOD & 0x08) ? clLime : clRed; InputPanel4->Color = (fromPOD & 0x10) ? clLime : clRed; InputPanel5->Color = (fromPOD & 0x20) ? clLime : clRed; InputPanel6->Color = (fromPOD & 0x40) ? clLime : clRed; InputPanel7->Color = (fromPOD & 0x80) ? clLime : clRed; } void __fastcall TForm1::Timer1Timer(TObject *Sender) { //do stuff Timer1->Enabled = false; outToPod(); mode = AQUIRING; Commo->WriteCom("I"); do { Application->ProcessMessages(); }while (mode == AQUIRING); mode = AQUIRING; Timer1->Enabled = true; } //---------------------------------------------------------------------------