⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 echoramapp.cpp

📁 Module usb ram - bardzo uzyteczny do komunikacji z innymi urzadzeniami
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "EchoRAMApp.h"

#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <sstream> 

using namespace std;

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
   StatusBar1->Panels->Items[0]->Text = "Brak polaczenia";

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Memo2->Lines->Add("-------------------------------------------------- " );
    Memo2->Lines->Add("Otwieram polaczenie... " );
    ftStatus = FT_Open(DevNum,&ftHandle);
    if (ftStatus != FT_OK){
      Memo2->Lines->Add("    - Blad!. Nie moge usyskac polaczenia." );
    }
    else{
      Memo2->Lines->Add("    - Polaczenie otwarte" );
    }
   SendCounter = 0;
   ReadCounter = 0;
   StatusBar1->Panels->Items[0]->Text = "Polaczenie otwarte";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    FT_Close(ftHandle);
    Memo2->Lines->Add("Polaczenie zakonczone." );
    Memo2->Lines->Add("-------------------------------------------------- " );
    StatusBar1->Panels->Items[0]->Text = "Brak polaczenia";
    SendCounter = 0;
    ReadCounter = 0;
   Label5->Caption = IntToStr(SendCounter);
   Label7->Caption = IntToStr(SendCounter);



}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    FT_Close(ftHandle);
    Memo2->Lines->Add("Polaczenie zakonczone." );
    Memo2->Lines->Add("-------------------------------------------------- " );

   Memo1->Lines->Clear();
   Memo2->Lines->Clear();
   Memo3->Lines->Clear();

    Memo2->Lines->Add("-------------------------------------------------- " );
    Memo2->Lines->Add("Otwieram polaczenie... " );
    ftStatus = FT_Open(DevNum,&ftHandle);
    if (ftStatus != FT_OK){
      Memo2->Lines->Add("    - Blad!. Nie moge usyskac polaczenia." );
    }
    else{
      Memo2->Lines->Add("    - Polaczenie otwarte" );
    }
   SendCounter = 0;
   ReadCounter = 0;
   StatusBar1->Panels->Items[0]->Text = "Polaczenie otwarte";

   SendCounter = 0;
   ReadCounter = 0;

   Label5->Caption = IntToStr(SendCounter);
   Label7->Caption = IntToStr(SendCounter);



}

//---------------------------------------------------------------------------



void __fastcall TForm1::Button4Click(TObject *Sender)
{
  using namespace std;

   SendCounter = 0;
   ReadCounter = 0;
   StatusBar1->Panels->Items[0]->Text = "Polaczenie otwarte";

   SendCounter = 0;
   ReadCounter = 0;

   Label5->Caption = IntToStr(SendCounter);
   Label7->Caption = IntToStr(SendCounter);
   Memo2->Lines->Add("    - Generuje tablice 1K");

   int i, j, k;
   int size = StrToInt(Edit1->Text);
   int ileBlokow = StrToInt(Edit2->Text);
   int ileRazy = StrToInt(Edit3->Text);

   char* buforA = new char[ileBlokow*size];
   char* buforB = new char[ileBlokow*size];

   char num;
   char sig = 0;
   for(i = 0; i < size; i++){
      num = rand() % 128;
      sig ^= num;
      buforA[i] = (char) num;
//      buforA[i] = (char)random(64);
      buforB[i] = 0;
      for(j = 1; j < ileBlokow; j++){
         buforA[size * j + i] = buforA[i] ;
         buforB[size * j + i] = 0;
      }
   }
   Memo2->Lines->Add("    - Tablica wygtenerowana");
   string line;
   int noOfFullLines = size / 16;
   int noOfRemainingCharacters = size % 16;
   for(i = 0; i < noOfFullLines; i++){
      line = "";
      for(j = 0; j < 16; j++){
         line = line + (IntToHex(buforA[i+j],2)).c_str();
         line = line + " ";
      }
      Memo1->Lines->Add("[" + IntToStr(i) + "] => \t" + line.c_str());

   }
   if(noOfRemainingCharacters){
      line = "";
      for(j = 0; j < noOfRemainingCharacters; j++){
         line = line + (IntToHex(buforA[noOfFullLines+j],2)).c_str();
         line = line + " ";
      }
      Memo1->Lines->Add("[" + IntToStr(i) + "] => " + line.c_str());
   }

   Memo1->Lines->Add("-----------\t-----------\t-----------\t-----------\t-----------");
   Memo1->Lines->Add("[syngnatura ] => " + IntToStr(sig)+ "\t " +IntToHex(sig,2));

//   Memo1->Lines->Add("    -   [" + IntToStr(i) + "] => " + IntToStr(buforA[i])+ "\t " +IntToHex(buforA[i],2));
//   Memo1->Lines->Add("    - Values :" + IntToStr(i+1));
//   for(i = 0; i < size; i++){
//      Memo1->Lines->Add("    -   [" + IntToStr(i) + "] => " + IntToStr(buforA[i]));
//   }

   char *lpBuffer;
   DWORD dwBytesToWrite = ileBlokow*size;
   DWORD dwBytesToRead = ileBlokow*size;
   DWORD lpdwBytesWritten;
   DWORD lpdwBytesReturned;

   time_t start, stop;

   for(k = 0; k < ileRazy; k++){
      start=clock();
      Memo2->Lines->Add(" - Wysylam " + IntToStr(k+1));
      Memo2->Lines->Add("        - Wysylam " + IntToStr(ileBlokow) + " x " + IntToStr(size) + "bajtow");
      lpBuffer = buforA;
      ftStatus = FT_Write(ftHandle,lpBuffer,dwBytesToWrite,&lpdwBytesWritten);
      if(ftStatus != FT_OK){
         Memo2->Lines->Add("        - Nie mozna wyslac danych!" );
      }
      else{
         SendCounter+=lpdwBytesWritten;
         Label5->Caption = IntToStr(SendCounter);
         Memo1->Lines->Add("    - Wyslano" + IntToStr(k+1));
      }


      Memo2->Lines->Add(" - Odbieram " + IntToStr(k+1));

      lpBuffer = buforB;
      ftStatus = FT_Read(ftHandle,lpBuffer,dwBytesToRead,&lpdwBytesReturned);

      if(ftStatus != FT_OK){
         Memo2->Lines->Add("        - Nie mozna czytac danych!" );
      }
      else{
         ReadCounter+=lpdwBytesReturned;
         Label7->Caption = IntToStr(ReadCounter);
         Memo2->Lines->Add("        - Odczytalem" + IntToStr(k+1));
      }
      //Oblicz przepustowosc
      stop=clock();
      float czas = (stop - start)/CLK_TCK;
      float kbs;
      if(czas != 0)
         kbs = (ileBlokow*size/czas)/1000;
      else
         kbs = -1;

      Memo2->Lines->Add(" - Zrobione w " + FloatToStrF(czas, ffFixed, 7, 3	) + " s");
      Memo2->Lines->Add(" - Przepustowosc = " + FloatToStrF(kbs, ffFixed, 7, 3	) + " kBajtow/s");
      Memo2->Lines->Add(" - Przepustowosc = " + FloatToStrF(kbs*8, ffFixed, 7, 3	) + " kBitow/s");
      Memo2->Lines->Add(" ---------------------------------------------------");

      // Wypisz odebrane dane
      for(i = 0; i < noOfFullLines; i++){
         line = "";
         for(j = 0; j < 16; j++){
            line = line + (IntToHex(buforB[i+j],2)).c_str();
            line = line + " ";
         }
         Memo3->Lines->Add("[" + IntToStr(i) + "] => \t" + line.c_str());

      }
      if(noOfRemainingCharacters){
         line = "";
         for(j = 0; j < noOfRemainingCharacters; j++){
            line = line + (IntToHex(buforB[noOfFullLines+j],2)).c_str();
            line = line + " ";
         }
         Memo3->Lines->Add("[" + IntToStr(i) + "] => " + line.c_str());
      }

      // Policz sygnature dla odebranych danych
      sig = 0;
      for(i = 0; i < size; i++){
         num = (char)buforB[i];
         sig ^= num;
      }
      // Wypisz sygnature
      Memo3->Lines->Add("-----------\t-----------\t-----------\t-----------\t-----------");
      Memo3->Lines->Add("[syngnatura ] => " + IntToStr(sig)+ "\t " +IntToHex(sig,2));

/*
      for(j = 0; j < ileBlokow*size; j++)
         if(buforA[j] != buforB[j]){
            Memo3->Lines->Add("    - ERROR :");
            Memo3->Lines->Add("       Value at position: "+ IntToStr(j));
            Memo3->Lines->Add("       Value sent       : "+ IntToStr(buforA[j]));
            Memo3->Lines->Add("       Value read       : "+ IntToStr(buforB[j]));
//            break;
         }
//      if(j != size)break;
*/
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
   String fileName, encryptedFileName;
   string line1;
   string line2;

   FT_Close(ftHandle);
   Memo2->Lines->Add("Polaczenie zakonczone." );
   Memo2->Lines->Add("-------------------------------------------------- " );

   Memo1->Lines->Clear();
   Memo2->Lines->Clear();
   Memo3->Lines->Clear();

   if(OpenDialog1->Execute()){
      fileName = OpenDialog1->FileName;
   }
   else return;

   Memo2->Lines->Add("-------------------------------------------------- " );
   Memo2->Lines->Add("Otwieram polaczenie... " );
   ftStatus = FT_Open(DevNum,&ftHandle);
   if (ftStatus != FT_OK){
      Memo2->Lines->Add("    - Blad!. Nie moge usyskac polaczenia." );
   }
   else{
      Memo2->Lines->Add("    - Polaczenie otwarte" );
   }
   SendCounter = 0;
   ReadCounter = 0;
   StatusBar1->Panels->Items[0]->Text = "Polaczenie otwarte";

   SendCounter = 0;
   ReadCounter = 0;

   int i, j, k;
   int size = StrToInt(Edit1->Text);
   int ileBlokow = StrToInt(Edit2->Text);

   char* buforA = new char[ileBlokow*size];
   char* buforB = new char[ileBlokow*size];

   Label5->Caption = IntToStr(SendCounter);
   Label7->Caption = IntToStr(SendCounter);
   Memo2->Lines->Add("    - Wysylam plik " + fileName);

   fstream file(fileName.c_str(),ios_base::in | ios_base::binary);
   encryptedFileName =  fileName+".enc";
   fstream encryptedFile(encryptedFileName.c_str(),ios_base::out | ios_base::binary);

   char bufor[1024];

   char *lpBuffer;
   DWORD dwBytesToWrite = 1;
   DWORD dwBytesToRead = 1;
   DWORD lpdwBytesWritten;
   DWORD lpdwBytesReturned;

   // Wyzeruj sygnature
   unsigned char sig = 0;

   int fileSize, noOfFullBlocks, sizeOfLastBlock;
   // Przejdz na koniec pliku
   file.seekg(0,ios_base::end);
   // Odczytaj dlugosc pliku
   fileSize = file.tellg();
   //Ile pelnych blokow o dlugosci bufora  'ileBlokow*size'
   noOfFullBlocks = fileSize / (ileBlokow*size);
   // Jaka dlug jest reszta
   sizeOfLastBlock =  fileSize % (ileBlokow*size);

   // Przejdz na poczatek pliku i zacznij odczyt
   file.seekg(0,ios_base::beg);

   line1 = "";
   line2 = "";
   // Obsluz pelne bloki danych
   for(k = 0; k < noOfFullBlocks; k++){
      // Odczytaj blok
      file.read(buforA, ileBlokow*size);
      // Uaktualnij sygnature
      for(i = 0; i < ileBlokow*size; i++){
         sig ^= (unsigned char)buforA[i];
      }
      Label18 -> Caption = IntToStr(sig)+ "\t " +IntToHex(sig,2);
      Label18 -> Refresh();


      // Wypisz do okna zawartosc bloku
      j = 0;
      for(i = 0; i < ileBlokow*size; i++, j++){
         if(buforA[i] == '\r');
         else if(buforA[i] == '\n' || j == 120){
            Memo1->Lines ->Add (line1.c_str());
            line1 = "";
            j = 0;
         }
         else{
            line1 = line1 + (char) buforA[i];
         }
      }

      // Wyslij blok
      lpBuffer = buforA;
      dwBytesToWrite = ileBlokow*size;
      ftStatus = FT_Write(ftHandle,lpBuffer,dwBytesToWrite,&lpdwBytesWritten);
      if(ftStatus != FT_OK){
         Memo2->Lines->Add("    - Nie mozna wyslac danych!" );
         return;
      }
      else{
         SendCounter+=lpdwBytesWritten;
         Label5->Caption = IntToStr(SendCounter);
      }

      // Odbierz dane
      lpBuffer = buforB;
      dwBytesToRead = ileBlokow*size;
      ftStatus = FT_Read(ftHandle,lpBuffer,dwBytesToRead,&lpdwBytesReturned);
      if(ftStatus != FT_OK){
         Memo2->Lines->Add("    - Nie mozna czytac danych!" );
         return;
      }
      else{
         ReadCounter+=lpdwBytesReturned;
         Label7->Caption = IntToStr(ReadCounter);
      }
      // Wypisz do okna zawartosc bloku
      j = 0;
      for(i = 0; i < ileBlokow*size; i++, j++){
         if(buforB[i] == '\r');
         else if(buforB[i] == '\n' || j == 120){
            Memo3->Lines ->Add (line2.c_str());
            line2 = "";
            j = 0;
         }
         else{
            line2 = line2 + (char) buforB[i];
         }
      }

      //Zapisz odebrane dane do pliku
      encryptedFile.write(buforB, ileBlokow*size);
   }
   encryptedFile.close();
   file.close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -