//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" AnsiString IP2Str(AnsiString sIP); AnsiString Str2IP(AnsiString sStr); 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; btnSetIP->Enabled = True; gbData->Enabled = True; gbConfig->Enabled = True; gbCOM1->Enabled = True; gbCOM2->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; btnSetIP->Enabled = False; gbData->Enabled = False; gbConfig->Enabled = False; gbCOM1->Enabled = False; gbCOM2->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; // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Get Gateway cs7188E->Tag = 13; cs7188E->Socket->SendText("13"); tm7188E->Enabled = True; // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Get SubNet-Mask cs7188E->Tag = 15; cs7188E->Socket->SendText("15"); tm7188E->Enabled = True; // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Get COM1 State cs7188E->Tag = 161; cs7188E->Socket->SendText("161"); tm7188E->Enabled = True; // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Get COM2 State cs7188E->Tag = 162; cs7188E->Socket->SendText("162"); 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 61: // Set COM1 Baud Rate //return OK case 62: // Set COM2 Baud Rate //return OK break; case 71: // Set DataBits,ParityBits,StopBits // return OK case 72: // Set DataBits,ParityBits,StopBits //return OK break; case 8: // Set Host IP and Disconnect btnConnectClick(this); break; case 10: // Get Server name eServerName->Text = sData; break; case 12: // Set Gateway and Disconnect btnConnectClick(this); break; case 13: // Get Gateway IP eGateway->Text = sData; break; case 14: // Set SubNet-Mask and Disconnect btnConnectClick(this); break; case 15: // Get subNet-Mask eMask->Text = sData; break; case 161: // get COM1 state SetCOMString(sData, 1); break; case 162: // get COM2 state SetCOMString(sData, 2); 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 btnSetIP->Enabled = False; gbData->Enabled = False; gbConfig->Enabled = False; gbCOM1->Enabled = False; gbCOM2->Enabled = False; } //--------------------------------------------------------------------------- //Converts the IP "192.168.31.1" to string "192168031001" AnsiString IP2Str(AnsiString sIP) { AnsiString sStr, sTmp; int iPos, iLen, i; sStr = ""; for (i=0; i<3; i++) { iPos = sIP.Pos("."); sTmp = "00" + sIP.SubString(1,iPos-1); iLen = sTmp.Length(); sStr = sStr + sTmp.SubString(iLen-2, 3); //iLen = sIP.Length(); sIP.Delete(1,iPos); } sTmp = "00" + sIP; iLen = sTmp.Length(); sStr = sStr + sTmp.SubString(iLen-2, 3); return sStr; } //Converts the string "192168031001" to IP "192.168.031.001" AnsiString Str2IP(const AnsiString sStr) { return ( sStr.SubString(0,3) + "." + sStr.SubString(3,3) + "." + sStr.SubString(6,3) + "." + sStr.SubString(9, 3) ); } // Setting the Host IP void __fastcall TForm1::btnSetIPClick(TObject *Sender) { AnsiString sStr; sStr = IP2Str( eHostIP->Text ); // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Set Host IP cs7188E->Tag = 8; cs7188E->Socket->SendText("08"+sStr); tm7188E->Enabled = True; } //--------------------------------------------------------------------------- // Setting the Gateway IP void __fastcall TForm1::btnSetGatewayClick(TObject *Sender) { AnsiString sStr; sStr = IP2Str( eGateway->Text ); // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Set Gateway cs7188E->Tag = 12; cs7188E->Socket->SendText("12"+sStr); tm7188E->Enabled = True; } //--------------------------------------------------------------------------- // Setting the SubNet-Mask void __fastcall TForm1::btnSetMaskClick(TObject *Sender) { AnsiString sStr; sStr = IP2Str( eMask->Text ); // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); // Set SubNet-Mask cs7188E->Tag = 14; cs7188E->Socket->SendText("14"+sStr); tm7188E->Enabled = True; } //--------------------------------------------------------------------------- // Setting the COM port states void __fastcall TForm1::btnSetCOM1Click(TObject *Sender) { AnsiString sData, sIdx; int index; index = ((TButton*)Sender)->Tag; sIdx = IntToStr(index); // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); //Set Baud-Rate 9600 cs7188E->Tag = 60+index; sData = "06" + sIdx + ((TEdit*)FindComponent("eBaud"+sIdx))->Text; cs7188E->Socket->SendText(sData); tm7188E->Enabled = True; // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); //Set DataBits, ParityBits, StopBits 8N1 cs7188E->Tag = 70+index; sData = "07" + sIdx + ((TComboBox*)FindComponent("cbDataBits"+sIdx))->Text + ((TComboBox*)FindComponent("cbParityBits"+sIdx))->Text[1] + ((TComboBox*)FindComponent("cbStopBits"+sIdx))->Text ; cs7188E->Socket->SendText(sData); tm7188E->Enabled = True; // Waiting for the end of previous job while (cs7188E->Tag != 0) Application->ProcessMessages(); } //--------------------------------------------------------------------------- //Setting the COM port string of User-Interface void __fastcall TForm1::SetCOMString(AnsiString sData, int index) { AnsiString sText, sIdx; int iPos; sIdx = IntToStr( index ); // Baud Rate iPos = sData.Pos(","); if (iPos == 0) { ((TEdit*)FindComponent("eBaud"+sIdx))->Text = "Error"; return; } sText = sData.SubString(1, iPos - 1); ((TEdit*)FindComponent("eBaud"+sIdx))->Text = sText; // DataBits sText = sData.SubString(iPos + 1, 1); ((TComboBox*)FindComponent("cbDataBits"+sIdx))->ItemIndex = StrToInt(sText) - 5; // ParityBits sText = sData.SubString(iPos + 3, 1); if (sText == "N") ((TComboBox*)FindComponent("cbParityBits"+sIdx))->ItemIndex = 0; else if (sText == "E") ((TComboBox*)FindComponent("cbParityBits"+sIdx))->ItemIndex = 1; else if (sText == "O") ((TComboBox*)FindComponent("cbParityBits"+sIdx))->ItemIndex = 2; // StopBits sText = sData.SubString(iPos + 5, 1); ((TComboBox*)FindComponent("cbStopBits"+sIdx))->ItemIndex = StrToInt(sText); } //--------------------------------------------------------------------------- void __fastcall TForm1::tmInitializeTimer(TObject *Sender) { tmInitialize->Enabled = False; GetServerInformation(); } //--------------------------------------------------------------------------- void __fastcall TForm1::cs7188EError(TObject *Sender, TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode) { ErrorCode = 0; } //---------------------------------------------------------------------------