//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::btnConnectClick(TObject *Sender) { // Cancel by user if (btnConnect->Caption == "Disconnect") { // Close connections cs7188E->Close(); btnConnect->Caption = "Connect"; btnExit->Enabled = True; return; } // Close the last connection cs7188E->Close(); // Setting Host IP cs7188E->Address = eHostIP->Text; // Setting Host Port Number to connect cs7188E->Port = 10000; // Open Connections cs7188E->Open(); } //--------------------------------------------------------------------------- // End of this Program void __fastcall TForm1::btnExitClick(TObject *Sender) { // Close Connections cs7188E->Close(); Close(); } //--------------------------------------------------------------------------- // Connected to the Host void __fastcall TForm1::cs7188EConnect(TObject *Sender, TCustomWinSocket *Socket) { btnConnect->Caption = "Disconnect"; btnExit->Enabled = False; gbData->Enabled = True; cs7188E->Tag = 0; //No jobs Application->ProcessMessages(); tmInitialize->Enabled = True; } //--------------------------------------------------------------------------- // Disconnected from the Host void __fastcall TForm1::cs7188EDisconnect(TObject *Sender, TCustomWinSocket *Socket) { btnConnect->Caption = "Connect"; btnExit->Enabled = True; gbData->Enabled = False; } //--------------------------------------------------------------------------- // Timeout occurs, clear the jobs void __fastcall TForm1::tm7188ETimer(TObject *Sender) { tm7188E->Enabled = False; cs7188E->Tag = 0; // Clear the jobs } //--------------------------------------------------------------------------- //sub-routinue for getting the server's information void __fastcall TForm1::GetServerInformation(void) { // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Get Version cs7188E->Tag = 1; cs7188E->Socket->SendText("01"); tm7188E->Enabled = True; // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Get Server Name cs7188E->Tag = 10; cs7188E->Socket->SendText("10"); tm7188E->Enabled = True; } // Reading Data from Host void __fastcall TForm1::cs7188ERead(TObject *Sender, TCustomWinSocket *Socket) { AnsiString sData; tm7188E->Enabled = False; // Disable the timeout sData = Socket->ReceiveText(); switch (cs7188E->Tag) { case 1: // Get Firmware version eVer->Text = sData; break; case 10: // Get Server name eServerName->Text = sData; break; case 99: // Send/Receive Data/Command eDataRece->Text = sData; break; default: // Unkown/No Job ShowMessage("Unknow message: " + sData ); } if ( sData == "OK" ) ShowMessage( "Process OK!!" ); cs7188E->Tag = 0; Application->ProcessMessages(); } //--------------------------------------------------------------------------- // Send Data/Command void __fastcall TForm1::btnDataSendClick(TObject *Sender) { // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Send Data cs7188E->Tag = 99; cs7188E->Socket->SendText(eDataSend->Text); tm7188E->Enabled = True; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { // Initialize the User-Interface gbData->Enabled = False; } //--------------------------------------------------------------------------- void __fastcall TForm1::tmInitializeTimer(TObject *Sender) { tmInitialize->Enabled = False; GetServerInformation(); } //--------------------------------------------------------------------------- void __fastcall TForm1::cs7188EError(TObject *Sender, TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode) { ErrorCode = 0; } //---------------------------------------------------------------------------