//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" struct PORTSTATE { DWORD baud; // 9600 char format[4]; // 8N1 } comport[2]; struct IP { WORD ip[4]; // 192.168.0.1 } sGateway, sMask; TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::ssHostClientConnect(TObject *Sender, TCustomWinSocket *Socket) { lbHost->Items->Add("S>Connect from IP:" + Socket->RemoteAddress); } //--------------------------------------------------------------------------- void __fastcall TForm1::ssCOM1ClientConnect(TObject *Sender, TCustomWinSocket *Socket) { TListBox *plb; if ( IntToStr(Socket->LocalPort) == eCOM1->Text ) plb = lbCOM1; else plb = lbCOM2; plb->Items->Add("S>Connect from IP:" + Socket->RemoteAddress); } //--------------------------------------------------------------------------- void __fastcall TForm1::btnActiveClick(TObject *Sender) { // Stop by user if ( btnActive->Caption == "Stop" ) { // Disconnect ssHost->Close(); ssCOM1->Close(); ssCOM2->Close(); btnActive->Caption = "Active"; btnExit->Enabled = True; return; } // Disconnect ssHost->Close(); ssCOM1->Close(); ssCOM2->Close(); ssHost->Port = StrToInt(eHost->Text); ssCOM1->Port = StrToInt(eCOM1->Text); ssCOM2->Port = StrToInt(eCOM2->Text); // Connect ssHost->Open(); ssCOM1->Open(); ssCOM2->Open(); btnActive->Caption = "Stop"; btnExit->Enabled = False; } //--------------------------------------------------------------------------- void __fastcall TForm1::ssHostClientRead(TObject *Sender, TCustomWinSocket *Socket) { AnsiString sRtn; // Reading data from client socket AnsiString sRece = Socket->ReceiveText(); if ( sRece[ sRece.Length() ] == '\r' ) { sRece.Delete( sRece.Length(), 1 ) ; } int CmdIdx, Idx2; lbHost->Items->Add( "C>" + sRece ); // Default to return the original string sRtn = "[" + sRece + "]"; // Get the command CmdIdx = StrToInt( sRece.SubString(1,2) ); // Get the COM port number if ( CmdIdx == 6 || CmdIdx == 7 || CmdIdx == 16 ){ try { Idx2 = StrToInt( sRece.SubString(3,1) ); } catch(...) { lbHost->Items->Add("S:Command Error!!"); return; } if ( Idx2 < 1 || Idx2 > 2 ) { lbHost->Items->Add("S:Invalid COM port number!!"); return; } } switch (CmdIdx) { case 1: sRtn = "v2.4"; break; // Get version number case 2: case 3: case 6: case 7: case 9: sRtn = "OK"; break; case 8: case 12: case 14: sRtn = "OK..Reconnect"; break; case 10: sRtn = "7188E2"; break; // Get Server Name case 11: sRtn = sRece.SubString(3, sRece.Length()-2); sRtn = "<" + sRtn + ">"; break; case 13: sRtn = ""; // Get Gateway IP for (int i=0; i<4; i++) { sRtn = sRtn + IntToStr(sGateway.ip[i]); if ( i<3 ) sRtn = sRtn + "."; } break; case 15: sRtn = ""; // Get SubNet-Mask for (int i=0; i<4; i++) { sRtn = sRtn + IntToStr(sMask.ip[i]); if ( i<3 ) sRtn = sRtn + "."; } break; case 16: sRtn = IntToStr(comport[Idx2-1].baud) + "," + (AnsiString)comport[Idx2-1].format[0] + "," + (AnsiString)comport[Idx2-1].format[1] + "," + (AnsiString)comport[Idx2-1].format[2]; break; } try { if ( CmdIdx == 6 ) // Setting baudrate comport[Idx2-1].baud = StrToInt( sRece.SubString(4, sRece.Length()-3) ); if ( CmdIdx == 7 ) // setting data format strcpy(comport[Idx2-1].format, sRece.SubString(4, 3).c_str()); if ( CmdIdx == 12 ) // setting Gateway for (int i=0; i<4; i++) sGateway.ip[i] = (WORD) StrToInt( sRece.SubString(i*3+3, 3) ); if ( CmdIdx == 14 ) // setting Mask for (int i=0; i<4; i++) sMask.ip[i] = (WORD) StrToInt( sRece.SubString(i*3+3, 3) ); } catch (...) { lbHost->Items->Add("S:Command Error!!"); return; } Socket->SendText( sRtn ); lbHost->Items->Add( "S>" + sRtn ); } //--------------------------------------------------------------------------- void __fastcall TForm1::ssCOM1ClientRead(TObject *Sender, TCustomWinSocket *Socket) { TListBox *plb; if ( IntToStr(Socket->LocalPort) == eCOM1->Text ) plb = lbCOM1; else plb = lbCOM2; AnsiString sRece = Socket->ReceiveText(); plb->Items->Add("C>" + sRece ); sRece = "[" + sRece + "]"; Socket->SendText( sRece ); plb->Items->Add("S>" + sRece ); } //--------------------------------------------------------------------------- void __fastcall TForm1::btnExitClick(TObject *Sender) { ssHost->Close(); ssCOM1->Close(); ssCOM2->Close(); Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ssHostClientError(TObject *Sender, TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode) { ShowMessage("Host Error:" + IntToStr(ErrorCode) ); ErrorCode = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::ssCOM1ClientError(TObject *Sender, TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode) { ShowMessage("Host Error:" + IntToStr(ErrorCode) ); ErrorCode = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::ssHostListen(TObject *Sender, TCustomWinSocket *Socket) { lbHost->Items->Add("S>Listening..."); } //--------------------------------------------------------------------------- void __fastcall TForm1::ssCOM1Listen(TObject *Sender, TCustomWinSocket *Socket) { TListBox *plb; if ( IntToStr(Socket->LocalPort) == eCOM1->Text ) plb = lbCOM1; else plb = lbCOM2; plb->Items->Add("S>Listening..."); } //--------------------------------------------------------------------------- void __fastcall TForm1::ssHostClientDisconnect(TObject *Sender, TCustomWinSocket *Socket) { lbHost->Items->Add("S>Disconnect IP:" + Socket->RemoteAddress); } //--------------------------------------------------------------------------- void __fastcall TForm1::ssCOM1ClientDisconnect(TObject *Sender, TCustomWinSocket *Socket) { TListBox *plb; if ( IntToStr(Socket->LocalPort) == eCOM1->Text ) plb = lbCOM1; else plb = lbCOM2; plb->Items->Add("S>Disconnect IP:" + Socket->RemoteAddress); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { for (int i=0; i<2; i++) { comport[i].baud = 9600; strcpy(comport[i].format, "8N1"); } sGateway.ip[0] = 192; sGateway.ip[1] = 168; sGateway.ip[2] = 0; sGateway.ip[3] = 1; sMask.ip[0] = 255; sMask.ip[1] = 255; sMask.ip[2] = 0; sMask.ip[3] = 0; } //---------------------------------------------------------------------------