经过近一周的努力,终于写好了EEPROM自动读写的Tool //--------------------------------------------------------------------------- #include <vcl.h> #include <stdio.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "MSCommLib_OCX" #pragma resource "*.dfm" #define CharMode 0 #define StringGrid_ColCount 17 #define StringGrid_RowCount 514 #define WriteToEEPromAddressRange 2048 AnsiString receive, strGetString; OleVariant oleString1; TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { if(MSComm1->PortOpen == true) MSComm1->PortOpen = false; int ColCount, RowCount, TempRowAdd = 0; for(ColCount = 1;ColCount < StringGrid_ColCount;ColCount++) StringGrid1->Cells[ColCount][0] = IntToHex(ColCount-1,2); StringGrid1->Cells[0][0] = "DATA "; for(RowCount = 1;RowCount<=StringGrid_RowCount;RowCount++) { if(TempRowAdd>=0&&TempRowAdd<10) StringGrid1->Cells[0][RowCount] = AnsiString("L") +"000" +AnsiString(TempRowAdd) + ":"; else if(TempRowAdd>=10&&TempRowAdd<100) StringGrid1->Cells[0][RowCount] = AnsiString("L") +"00" +AnsiString(TempRowAdd) + ":"; else if(TempRowAdd>=100&&TempRowAdd<1000) StringGrid1->Cells[0][RowCount] = AnsiString("L") +"0" +AnsiString(TempRowAdd) + ":"; else StringGrid1->Cells[0][RowCount] = AnsiString("L") + AnsiString(TempRowAdd) + ":"; TempRowAdd = TempRowAdd + 4; } } //--------------------------------------------------------------------------- void __fastcall TForm1::OpenPortClick(TObject *Sender) { if(MSComm1->PortOpen==false) { AnsiString aniSent; //choose the port MSComm1->CommPort = StrToInt(ComboBox1->Text.SubString(4,1)); //choose the Speed MSComm1->Settings = ComboBox2->Text + ",n" + ",8" + ",1"; MSComm1->InputMode = CharMode; MSComm1->PortOpen = true; aniSent = "TPVDumpEEPRom 0 2047/r/n"; MSComm1->Output = StringToOleStr(aniSent); OpenPort->Enabled = false; ClosePort->Enabled = true; } } //--------------------------------------------------------------------------- void __fastcall TForm1::MSComm1Comm(TObject *Sender) { AnsiString Temp; //OleVariant oleString1; oleString1 = MSComm1->Input; strGetString = oleString1.AsType(varString); receive += strGetString; Temp = receive; if(receive.Length()>31260) { ReadFromEEProm->Enabled=true; } } //--------------------------------------------------------------------------- void __fastcall TForm1::WriteToBinFileClick(TObject *Sender) { BYTE ucpBuffer[2048*4]={0}; FILE *fpFile; int RowCount,ColCount,Temp = 0; AnsiString Grid_String,Str_To_Int; if(FileExists("Zoran_EEPROM.bin")) { system("del Zoran_EEPROM.bin"); } if ((fpFile = fopen("Zoran_EEPROM.bin","ab+"))==NULL) for( ColCount = 1;ColCount<513;ColCount++) { for(RowCount=1;RowCount<17;RowCount++) { //Grid_String += StrToInt("$"+StringGrid1->Cells[RowCount][ColCount]); ucpBuffer[Temp] = StrToInt("0x"+StringGrid1->Cells[RowCount][ColCount]); Temp++; } } fwrite (ucpBuffer, 2048*4, 1, fpFile); // List->Add(Grid_String); fclose(fpFile); //List->SaveToFile("Zoran_EEPROM_Data.bin"); //delete List; } //--------------------------------------------------------------------------- void __fastcall TForm1::Write_Data_To_StringGrid() { int DataLeft[17], DataRight[17],DataCount[17]; if(receive.Pos("TPVDumpEEPRom")!=0) { receive = receive.SubString(receive.Pos("TPVDumpEEPRom"),31300); if(receive.Length()>31260) { //Memo1->Text=receive; DataLeft[1] = 31, DataRight[1] = 32; //Write Col 1 for(DataCount[1] = 1;DataCount[1]<513;DataCount[1]++) { StringGrid1->Cells[1][DataCount[1]]=(AnsiString)receive[DataLeft[1]]+(AnsiString)receive[DataRight[1]]; DataLeft[1]= DataLeft[1] + 61; DataRight[1] = DataRight[1] + 61; } //Write Col 2 if(DataCount[1]==513) { DataLeft[2] = 34, DataRight[2] = 35; for(DataCount[2] = 1;DataCount[2]<513;DataCount[2]++) { StringGrid1->Cells[2][DataCount[2]]=(AnsiString)receive[DataLeft[2]]+(AnsiString)receive[DataRight[2]]; DataLeft[2] = DataLeft[2] + 61; DataRight[2] = DataRight[2] + 61; } } } //Write Col 3 if(DataCount[2]==513) { DataLeft[3] = 37, DataRight[3] = 38; for(DataCount[3] = 1;DataCount[3]<513;DataCount[3]++) { StringGrid1->Cells[3][DataCount[3]]=(AnsiString)receive[DataLeft[3]]+(AnsiString)receive[DataRight[3]]; DataLeft[3] = DataLeft[3] + 61; DataRight[3] = DataRight[3] + 61; } } //Write Col 4 if(DataCount[3]==513) { DataLeft[4] = 40, DataRight[4] = 41; for(DataCount[4] = 1;DataCount[4]<513;DataCount[4]++) { StringGrid1->Cells[4][DataCount[4]]=(AnsiString)receive[DataLeft[4]]+(AnsiString)receive[DataRight[4]]; DataLeft[4] = DataLeft[4] + 61; DataRight[4] = DataRight[4] + 61; } } // write Col 5 if(DataCount[4]==513) { DataLeft[5] = 44, DataRight[5] = 45; for(DataCount[5] = 1;DataCount[5]<513;DataCount[5]++) { StringGrid1->Cells[5][DataCount[5]]=(AnsiString)receive[DataLeft[5]]+(AnsiString)receive[DataRight[5]]; DataLeft[5] = DataLeft[5] + 61; DataRight[5] = DataRight[5] + 61; } } //Write Col 6 if(DataCount[5]==513) { DataLeft[6] = 47, DataRight[6] = 48; for(DataCount[6] = 1;DataCount[6]<513;DataCount[6]++) { StringGrid1->Cells[6][DataCount[6]]=(AnsiString)receive[DataLeft[6]]+(AnsiString)receive[DataRight[6]]; DataLeft[6] = DataLeft[6] + 61; DataRight[6] = DataRight[6] + 61; } } //Write Col 7 if(DataCount[6]==513) { DataLeft[7] = 50, DataRight[7] = 51; for(DataCount[7] = 1;DataCount[7]<513;DataCount[7]++) { StringGrid1->Cells[7][DataCount[7]]=(AnsiString)receive[DataLeft[7]]+(AnsiString)receive[DataRight[7]]; DataLeft[7] = DataLeft[7] + 61; DataRight[7] = DataRight[7] + 61; } } //Write Col 8 if(DataCount[7]==513) { DataLeft[8] = 53, DataRight[8] = 54; for(DataCount[8] = 1;DataCount[8]<513;DataCount[8]++) { StringGrid1->Cells[8][DataCount[8]]=(AnsiString)receive[DataLeft[8]]+(AnsiString)receive[DataRight[8]]; DataLeft[8] = DataLeft[8] + 61; DataRight[8] = DataRight[8] + 61; } } //Write Col 9 if(DataCount[8]==513) { DataLeft[9] = 57, DataRight[9] = 58; for(DataCount[9] = 1;DataCount[9]<513;DataCount[9]++) { StringGrid1->Cells[9][DataCount[9]]=(AnsiString)receive[DataLeft[9]]+(AnsiString)receive[DataRight[9]]; DataLeft[9] = DataLeft[9] + 61; DataRight[9] = DataRight[9] + 61; } } //Write Col 10 if(DataCount[9]==513) { DataLeft[10] = 60, DataRight[10] = 61; for(DataCount[10] = 1;DataCount[10]<513;DataCount[10]++) { StringGrid1->Cells[10][DataCount[10]]=(AnsiString)receive[DataLeft[10]]+(AnsiString)receive[DataRight[10]]; DataLeft[10] = DataLeft[10] + 61; DataRight[10] = DataRight[10] + 61; } } //Write Col 11 if(DataCount[10]==513) { DataLeft[11] = 63, DataRight[11] = 64; for(DataCount[11] = 1;DataCount[11]<513;DataCount[11]++) { StringGrid1->Cells[11][DataCount[11]]=(AnsiString)receive[DataLeft[11]]+(AnsiString)receive[DataRight[11]]; DataLeft[11] = DataLeft[11] + 61; DataRight[11] = DataRight[11] + 61; } } //Write Col 12 if(DataCount[11]==513) { DataLeft[12] = 66, DataRight[12] = 67; for(DataCount[12] = 1;DataCount[12]<513;DataCount[12]++) { StringGrid1->Cells[12][DataCount[12]]=(AnsiString)receive[DataLeft[12]]+(AnsiString)receive[DataRight[12]]; DataLeft[12] = DataLeft[12] + 61; DataRight[12] = DataRight[12] + 61; } } //Write Col 13 if(DataCount[12]==513) { DataLeft[13] = 70, DataRight[13] = 71; for(DataCount[13] = 1;DataCount[13]<513;DataCount[13]++) { StringGrid1->Cells[13][DataCount[13]]=(AnsiString)receive[DataLeft[13]]+(AnsiString)receive[DataRight[13]]; DataLeft[13] = DataLeft[13] + 61; DataRight[13] = DataRight[13] + 61; } } //Write Col 14 if(DataCount[13]==513) { DataLeft[14] = 73, DataRight[14] = 74; for(DataCount[14] = 1;DataCount[14]<513;DataCount[14]++) { StringGrid1->Cells[14][DataCount[14]]=(AnsiString)receive[DataLeft[14]]+(AnsiString)receive[DataRight[14]]; DataLeft[14] = DataLeft[14] + 61; DataRight[14] = DataRight[14] + 61; } } //Write Col 15 if(DataCount[14]==513) { DataLeft[15] = 76, DataRight[15] = 77; for(DataCount[15] = 1;DataCount[15]<513;DataCount[15]++) { StringGrid1->Cells[15][DataCount[15]]=(AnsiString)receive[DataLeft[15]]+(AnsiString)receive[DataRight[15]]; DataLeft[15] = DataLeft[15] + 61; DataRight[15] = DataRight[15] + 61; } } //Write Col 16 if(DataCount[14]==513) { DataLeft[16] = 79, DataRight[16] = 80; for(DataCount[16] = 1;DataCount[16]<513;DataCount[16]++) { StringGrid1->Cells[16][DataCount[16]]=(AnsiString)receive[DataLeft[16]]+(AnsiString)receive[DataRight[16]]; DataLeft[16] = DataLeft[16] + 61; DataRight[16] = DataRight[16] + 61; } } } } void __fastcall TForm1::ReadFromEEPromClick(TObject *Sender) { //Memo1->Text=receive.Length(); Write_Data_To_StringGrid(); WriteToBinFile->Enabled = true; WriteToEEProm->Enabled = true; WriteToTextFile->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ClosePortClick(TObject *Sender) { int RowCount,ColCount; if(MSComm1->PortOpen == true) { MSComm1->PortOpen = false; OpenPort->Enabled = true; } receive = ""; ReadFromEEProm->Enabled = false; WriteToEEProm->Enabled = false; WriteToBinFile->Enabled = false; WriteToTextFile->Enabled = false; for( ColCount = 1;ColCount<513;ColCount++) { for(RowCount=1;RowCount<17;RowCount++) { StringGrid1->Cells[RowCount][ColCount] = ""; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::WriteToEEPromClick(TObject *Sender) { Write_To_EEProm(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Write_To_EEProm() { int Address,Address_Col[4],Address_Row; AnsiString EEPromVal1,EEPromVal2,EEPromVal3,EEPromVal4; AnsiString aniSent; for(Address = 0;Address < WriteToEEPromAddressRange;Address++) { Address_Row = Address/4 + 1; if(Address%4 == 0) { Address_Col[0] = 1; Address_Col[1] = 2; Address_Col[2] = 3; Address_Col[3] = 4; } else if(Address%4 == 1) { Address_Col[0] = 5; Address_Col[1] = 6; Address_Col[2] = 7; Address_Col[3] = 8; } else if(Address%4 == 2) { Address_Col[0] = 9; Address_Col[1] = 10; Address_Col[2] = 11; Address_Col[3] = 12; } else if(Address%4 == 3) { Address_Col[0] = 13; Address_Col[1] = 14; Address_Col[2] = 15; Address_Col[3] = 16; } EEPromVal1=StringGrid1->Cells[Address_Col[0]][Address_Row]; EEPromVal2=StringGrid1->Cells[Address_Col[1]][Address_Row]; EEPromVal3=StringGrid1->Cells[Address_Col[2]][Address_Row]; EEPromVal4=StringGrid1->Cells[Address_Col[3]][Address_Row]; aniSent ="/r/n"+(AnsiString)"tpvset_Address"+" "+(AnsiString)Address+" "+"0x" +EEPromVal1+EEPromVal2+EEPromVal3+EEPromVal4+"/r/n"; MSComm1->Output = StringToOleStr(aniSent); } OpenPort->Enabled = false; ClosePort->Enabled = true; ShowMessage("The EEPROM Data Already Writen"); } void __fastcall TForm1::ReadFormTextFileClick(TObject *Sender) { /* file file name grid stringGrid1 row 指定行开始存放 col 指定列开始存放 index 指定行开始读 */ int index = 1,col = 0,row = 1; if(!FileExists("Zoran_EEPROM.txt")) { ShowMessage("Zoran_EEPROM.Text file not exist"); return; } for(int c=1;c<StringGrid1->ColCount;c++) for(int r=1;r<StringGrid1->RowCount;r++) StringGrid1->Cells[c][r] = ""; TStringList *list = new TStringList(); TStringList *sub = new TStringList(); list->LoadFromFile("Zoran_EEPROM.txt"); StringGrid1->ColCount = 1; StringGrid1->RowCount = list->Count; for(int r=index;r<list->Count;r++) { sub->Delimiter = ' '; sub->DelimitedText = list->Strings[r]; if( StringGrid1->ColCount - 1 < sub->Count ) StringGrid1->ColCount = sub->Count; for(int c=0;c<sub->Count;c++) { StringGrid1->Cells[col+c][row+r-index] = sub->Strings[c]; } } ShowMessage("Data has read to StringGrid"); delete list; } //--------------------------------------------------------------------------- void __fastcall TForm1::WriteToTextFileClick(TObject *Sender) { int RowCount,ColCount; AnsiString Grid_String; TStringList *List=new TStringList(); for( ColCount = 0;ColCount<513;ColCount++) { Grid_String = ""; for(RowCount=0;RowCount<17;RowCount++) { Grid_String += StringGrid1->Cells[RowCount][ColCount] +" "; } List->Add(Grid_String); List->SaveToFile("Zoran_EEPROM_Data.txt"); } delete List; } //---------------------------------------------------------------------------