TMemoryStream* er; //глобальная переменнаяvoid __fastcall TFormListenPort::IdTCPServer1Connect(TIdContext *AContext){IdTCPClient1->Port = AContext->Connection->Socket->Binding->PeerPort;IdTCPClient1->Host = AContext->Connection->Socket->Binding->PeerIP;TByteDynArray IDunit;AContext->Connection->IOHandler->ReadBytes(IDunit, 8, false);//int Result = IDunit.Length;if((IDunit[0] == 0xFA && IDunit[1] == 0xF8) || (IDunit[0] == 0xF8 && IDunit[1] == 0xFA)); { try { try { IdTCPClient1->Connect(); IdTCPClient1->IOHandler->Write(IDunit); } catch(...) { TDateTime T(Now()); TStringList * ListWatchCatch = new TStringList(); AnsiString Log = ExtractFilePath(Application->ExeName); Log = Log + "uniterror.txt"; er->Seek(0, soFromEnd); ListWatchCatch->Add("Ошибка отправки эхо: " + AnsiString(IDunit[0]) + " " + AnsiString(IDunit[1]) + " " + AnsiString(IDunit[2]) + " " + AnsiString(IDunit[3]) + " " + T.DateTimeString()); ListWatchCatch->SaveToStream(er); er->SaveToFile(Log); delete ListWatchCatch; } } __finally { IdTCPClient1->Disconnect(); } }}
void __fastcall TFormListenPort::IdTCPServer1Execute(TIdContext *AContext){ //------------- блок кода отвечающий за прием стуктуры ------------------ IdTCPClient2->Port = AContext->Connection->Socket->Binding->PeerPort; IdTCPClient2->Host = AContext->Connection->Socket->Binding->PeerIP; TByteDynArray IDunit; AContext->Connection->IOHandler->ReadBytes(IDunit, sizeof(SyncStruct), false); if((IDunit[0] == 0xFA && IDunit[1] == 0xF8) || (IDunit[0] == 0xF8 && IDunit[1] == 0xFA)) { try { try { IdTCPClient2->Connect(); IdTCPClient2->IOHandler->Write(IDunit);//отправим стуктуру прибору для потвеждения соединения } catch(...) { //запишем в файл ошибку } } __finally { IdTCPClient2->Disconnect(); } } //------------- конец блока кода отвечающий за прием стуктуры -------------- //теперь будут приходит постоянно от прибора строки типа://101000002,20030217132813,121.646060,25.061725,20,157,133,7,0,11,15,0.096,0.000//надо их читать и складывать в базу данных: AnsiString S = AContext->Connection->IOHandler->ReadLn();и т.д. }
void __fastcall TFormListenPort::Button1Click(TObject *Sender){ try { try { IdTCPClient1->Connect(); IdTCPClient1->IOHandler->WriteLn("111,20030217132813,121.646060,25.061725,20,157,133,7,0,11,15,0.096,0.000"); } catch(...) { ShowMessage("Ошибка соединения при отправке"); } } __finally { IdTCPClient1->Disconnect(); } }
void __fastcall TFormListenPort::IdTCPServer1Connect(TIdContext *AContext){ TByteDynArray IDunit; AContext->Connection->IOHandler->ReadBytes(IDunit, sizeof(SyncStruct), false);//тут точка остонова} void __fastcall TFormListenPort::IdTCPServer1Execute(TIdContext *AContext){ TStringList * ListWatch = new TStringList(); //тут точка остонова // далее другой код
void __fastcall TFormListenPort::IdTCPServer1Connect(TIdContext *AContext){ IdTCPClient2->Port = AContext->Connection->Socket->Binding->PeerPort; IdTCPClient2->Host = AContext->Connection->Socket->Binding->PeerIP; TByteDynArray IDunit; AContext->Connection->IOHandler->ReadBytes(IDunit, sizeof(SyncStruct), false); if((IDunit[0] == 0xFA && IDunit[1] == 0xF8) || (IDunit[0] == 0xF8 && IDunit[1] == 0xFA)) { try { try { IdTCPClient2->Connect(); IdTCPClient2->IOHandler->Write(IDunit); } catch(...) { //запишем в файл ошибку } } __finally { IdTCPClient2->Disconnect(); } }}
void __fastcall TFormListenPort::IdTCPServer1Connect(TIdContext *AContext){TByteDynArray IDunit;AContext->Connection->IOHandler->ReadBytes(IDunit, sizeof(SyncStruct), false);//точка останова}
void __fastcall TFormListenPort::IdTCPServer1Execute(TIdContext *AContext){ //------------- тестовая запись в файл -------------- TStringList * ListWatch = new TStringList(); AnsiString S = AContext->Connection->IOHandler->ReadLn(); TStringList * ListWatchCatch = new TStringList(); //точка останова AnsiString Log = ExtractFilePath(Application->ExeName); Log = Log + "logport.txt"; pms->Seek(0, soFromEnd); ListWatchCatch->Add(S); ListWatchCatch->SaveToStream(pms); pms->SaveToFile(Log); delete ListWatchCatch; delete ListWatch; //------------ конец тестовой записи -----------------}
void __fastcall TFormListenPort::IdTCPServer1Connect(TIdContext *AContext){ TByteDynArray IDunit; try { AContext->Connection->IOHandler->ReadBytes(IDunit, sizeof(SyncStruct), false); if ((IDunit[0] != 0xFA) || (IDunit[1] != 0xF8)) { return; } AContext->Connection->IOHandler->Write(IDunit); } catch(...) { AnsiString Log = ExtractFilePath(Application->ExeName); Log = Log + "uniterror.txt"; if(FileExists(Log)) { SyncStruct ss; unsigned int i; unsigned short sh[2]; unsigned char ch[4]; for(int j = 0; j<4; j++) ch[j] = IDunit[j]; memcpy(sh,ch,4); for(int j = 0; j<4; j++) ch[j] = IDunit[j+4]; memcpy(&i,ch,4); ss.SyncHeader = sh[0]; ss.SyncID = sh[1]; ss.UnitID = i; er->LoadFromFile(Log); er->Seek(0, soFromEnd); TDateTime T(Now()); TStringList * ListWatchCatch = new TStringList(); ListWatchCatch->Add("Ошибка отправки/приема эхо: " + AnsiString(ss.SyncHeader) + " " + AnsiString(ss.SyncID) + " " + AnsiString(ss.UnitID) + " " + T.DateTimeString()); ListWatchCatch->SaveToStream(er); er->SaveToFile(Log); delete ListWatchCatch; } } }
void __fastcall TFormListenPort::IdTCPServer1Execute(TIdContext *AContext){ AnsiString S = AContext->Connection->IOHandler->ReadLn(); AnsiString Log = ExtractFilePath(Application->ExeName); Log = Log + "logport.txt"; pms->LoadFromFile(Log); pms->Seek(0, soFromEnd); TStringList * ListWatchCatch = new TStringList(); ListWatchCatch->Add(S); ListWatchCatch->SaveToStream(pms); pms->SaveToFile(Log); delete ListWatchCatch;}
TFileStream *fsLogFile;if (!FileExists(LogFile)) fsLogFile = new TFileStream(LogFile, fmCreate);else fsLogFile = new TFileStream(LogFile, fmOpenWrite);fsLogFile->Seek(0, soFromEnd);AnsiString LogStr = /*Лог-строка*/ + "\r\n";fsLogFile->Write(LogStr.c_str(), LogStr.Length());delete fsLogFile;