📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CPort"
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE hThread;
BOOL bRunThread = TRUE;
char fwbuf[262144];
char eebuf[65536];
unsigned short fwsize;
unsigned short eesize;
unsigned char ServerState=0;
struct {
unsigned char DeviceID;
unsigned char FlashSize;
unsigned char BootSize;
unsigned short PageSize;
} bootcfg;
#define STATE_ZERO 0
#define STATE_FLASHINIT 1
#define STATE_YQ 77
#define STATE_FLASHSEND 2
#define STATE_FLASHOVER 3
#define STATE_EEPSEND 11
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//ComPort1->BaudRate;
//ComPort1->Port;
ComPort1->Connected=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ComPort1->Connected=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
AnsiString Phrase;
Phrase = Edit1->Text;
ComPort1->WriteStr(Phrase); //蒫rit toute la cha頽e "Phrase" sur le port s閞ie
}
int gethexaword(unsigned short* retour,char* point){
unsigned short val=0;
int i;
for(i=0;i<4;i++){
val=val<<4;
if ((*point>='0') && (*point<='9')){
val+=*point-'0';
}
else if ((*point>='A') && (*point<='F')){
val+=*point-'A';
val+=10;
}
else if ((*point>='a') && (*point<='f')){
val+=*point-'a';
val+=10;
}
else
return 1;
point++;
}
retour[0]=val;
return 0;
}
int gethexabyte(unsigned char* retour,char* point){
unsigned char val=0;
int i;
for(i=0;i<2;i++){
val=val<<4;
if ((*point>='0') && (*point<='9')){
val+=*point-'0';
}
else if ((*point>='A') && (*point<='F')){
val+=*point-'A';
val+=10;
}
else if ((*point>='a') && (*point<='f')){
val+=*point-'a';
val+=10;
}
else
return 1;
point++;
}
retour[0]=val;
return 0;
}
void Perror(AnsiString S){
Form1->Memo1->Lines->Add(S);
}
void Debug(AnsiString S){
// Form1->Memo1->Lines->Add(S);
}
int loadintelhex(char* filename,char* destbuffer,unsigned short* count){
char buf[256];
AnsiString S;
unsigned int rc,mempointer;
unsigned char line,i,recbyte,reclength,rectype;
char crc;
unsigned short recaddress;
FILE* fd;
*count=0;
Form1->Memo1->Lines->Add("Reading hex file...");
fd=fopen(filename,"r");
if (!fd){
Perror("lecture .hex impossible");
return 1;
}
while(NULL!=fgets(buf,sizeof(buf),fd)){
S.sprintf("%s",buf);
Debug(S);
//interprete
if (buf[0]!=':'){
Perror("invalid .hex file");
return 1;
}
if (gethexabyte(&reclength,&buf[1])!=0){
Perror("invalid reclength line n");
fclose(fd);
return 1;
}
S.sprintf("reclength=%d",reclength);
Debug(S);
if (gethexaword(&recaddress,&buf[3])!=0){
fclose(fd);
return 1;
}
S.sprintf("recaddress=%d",recaddress);
Debug(S);
if (gethexabyte(&rectype,&buf[7])!=0){
fclose(fd);
return 1;
}
switch(rectype){
case 0 :
S.sprintf("rectype=data");
break;
case 1 :
S.sprintf("rectype=EOF");
*count=mempointer;
break;
case 2 :
case 4 :
S.sprintf("rectype=unsuported yet");
break;
default :
S.sprintf("rectype=unknown");
}
Debug(S);
//data
mempointer=recaddress;
for(i=0;i<reclength;i++){
if (gethexabyte(&recbyte,&buf[9+i*2])!=0){
Perror("invalid recdata line n");
fclose(fd);
return 1;
}
destbuffer[mempointer]=recbyte;
S.sprintf("recdata=%d",recbyte);
Debug(S);
mempointer++;
}
crc=0;
//1+2+1+N
for(i=0;i<5+reclength;i++){
if (gethexabyte(&recbyte,&buf[1+2*i])!=0){
Perror("error while computing crc");
fclose(fd);
return 1;
}
crc+=recbyte;
}
if (crc!=0){
Perror("one crc line failed");
fclose(fd);
return 1;
}
}
Form1->Memo1->Lines->Add("...OK");
S.sprintf("%d bytes read",*count);
Form1->Memo1->Lines->Add(S);
fclose(fd);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
AnsiString FileName;
OpenDialog1->Filter="hex file|*.hex";
if (OpenDialog1->Execute()){
FileName= OpenDialog1->FileName;
// charge le .hex
loadintelhex(FileName.c_str(),fwbuf,&fwsize);
Edit2->Text=FileName;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
ComPort1->ShowSetupDialog ();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
ComPort1->WriteStr("#PON:YBA1*");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
ComPort1->WriteStr("#POF:YBA1*");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
unsigned char Buf;
unsigned char tableau[1] = {'<'};
return;
ComPort1->Events = TComEvents(); // Effacer tous les 関enements
ComPort1->Connected = true;
TComEvents Ev;
Ev << evRxChar;
ComPort1->WaitForEvent (Ev, 0, 30000);
// Attend un 関閚ement OnRxChar ou 30 secondes avant de continuer
if (Ev.Contains(evRxChar))
{
// Executer une action, une donn閑 est arriv閑
//on verrifie que le caractere est le bon
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -