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

📄 mainform.cpp

📁 《Delphi5企业级解决方案及应用剖析》参考程序 DELPHI 资料集
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include <vcl\IniFiles.hpp>
#include "mainform.h"
//---------------------------------------------------------------------------
#pragma link "Grids"
#pragma resource "*.dfm"

#define MaxPorts  8

#include "tvichw32.h"

Word      PortWSel,PortRSel,NumLine,NumSymbol;
Byte      ValWSel,NomWSel,NomRSel,Scan_Code,Data_Reg,Status_Reg;
DWORD     TestVar,CurrAddr,PhysAddr,IRQCounter,Flag_Intr,Flag_Tim,
          Sum_Ticks,CurrTicker,OldTicker;
char      TestString[256];
TCheckBox *CPinRead[26],*CPinWrite[26];
Byte      *TPointPhys[17][17];
boolean   FlagPrint;
BOOL      ActiveHW = FALSE;
Byte      *PointPhys;
short	  IRQ;

PVicHW32Descriptor HwCtrl;

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
ULONG CharToNumber(char ch)
{
  int n,i;
  char hexh[17] = "0123456789ABCDEF";
  n = 0;
  for (i=0;i<=15;i++){ if (UpperCase(ch)==hexh[i]) n=i; }
  return n;
}
//---------------------------------------------------------------------------
int HexToInt(AnsiString s)
{

  int i,r,k;
  char    ch;

  k=1; r=0;
  for (i=s.Length();i>0; i--){
    ch=s[i];
    r=r+CharToNumber(ch)*k; if (i>1) k*=16;
  }
  return r;
}
// ---------------------------------------------------------
// Hardware Interrupt Handler
// ---------------------------------------------------------
void _stdcall
HWCtrlHwInterrupt(long  HWCounter,
				  short LPT_DataReg,
				  short LPT_StatusReg,
				  short Keyb_ScanCode)
{
	Flag_Intr ++;
	IRQCounter = HWCounter;
	Scan_Code = Keyb_ScanCode;
    Data_Reg = LPT_DataReg;
	Status_Reg = LPT_StatusReg;

}

//---------------------------------------------------------------------------
void ShowButtons()
{
   Form1->B_SetMemory->Enabled  =  ActiveHW;
   if (!ActiveHW) Form1->B_Mask->Checked  =  FALSE;
   Form1->SpinLPT->Enabled = ActiveHW;
   if (ActiveHW) {
      Form1->SpinLPT->Max = GetLPTNumPorts(HwCtrl);
      Form1->L_LPTs->Caption = IntToStr(GetLPTNumPorts(HwCtrl));
      Form1->L_Base->Caption = IntToHex(GetLPTBasePort(HwCtrl),3)+'h';
      Form1->C_Hard->Checked  =  GetHardAccess(HwCtrl);
   }
   Form1->L_LPTNumber->Caption = IntToStr(Form1->SpinLPT->Position);
   Form1->C_Hard->Enabled  =  ActiveHW;
   Form1->SpinIRQ->Enabled  =  ActiveHW && (!Form1->B_Mask->Checked);
   Form1->B_Open->Enabled = !ActiveHW;
   Form1->B_CloseDriver->Enabled  =  ActiveHW;
   Form1->B_Write->Enabled = ActiveHW;
   Form1->B_Read->Enabled = ActiveHW;
   Form1->B_WriteAll->Enabled = ActiveHW;
   Form1->B_ReadAll->Enabled = ActiveHW;
   Form1->C_Init->Enabled = ActiveHW;
   Form1->B_Print->Enabled = ActiveHW;
   Form1->B_Stop->Enabled = ActiveHW;
   Form1->B_ReadMemory->Enabled = ActiveHW && (PointPhys!=NULL);
   Form1->B_FillMemory->Enabled = ActiveHW && (PointPhys!=NULL);
   Form1->B_Mask->Enabled = ActiveHW && (IRQ>0) && (IRQ<16);
   Form1->C_LPT_IRQ->Enabled = ActiveHW &&
       ((IRQ==7 && GetLPTNumber(HwCtrl) == 1) ||
       (IRQ==5  && GetLPTNumber(HwCtrl) == 2));
   for (int nPin = 1;  nPin<18; nPin++) {
     if (!ActiveHW) CPinRead[nPin]->Checked = FALSE;
     if (!ActiveHW) CPinWrite[nPin]->Checked = FALSE;
     CPinWrite[nPin]->Enabled = ActiveHW;
   };
   CPinWrite[10]->Enabled = FALSE;
   CPinWrite[11]->Enabled = FALSE;
   CPinWrite[12]->Enabled = FALSE;
   CPinWrite[13]->Enabled = FALSE;
   CPinWrite[15]->Enabled = FALSE;
}
void __fastcall TForm1::FormActivate(TObject *Sender)
{
 int i;

 TIniFile* MyIniFile = new TIniFile("HW_test");
 TabbedNotebook1->PageIndex = 0;
 CPinRead[ 1] = RPin1;    CPinWrite[ 1] = WPin1;
 CPinRead[ 2] = RPin2;    CPinWrite[ 2] = WPin2;
 CPinRead[ 3] = RPin3;    CPinWrite[ 3] = WPin3;
 CPinRead[ 4] = RPin4;    CPinWrite[ 4] = WPin4;
 CPinRead[ 5] = RPin5;    CPinWrite[ 5] = WPin5;
 CPinRead[ 6] = RPin6;    CPinWrite[ 6] = WPin6;
 CPinRead[ 7] = RPin7;    CPinWrite[ 7] = WPin7;
 CPinRead[ 8] = RPin8;    CPinWrite[ 8] = WPin8;
 CPinRead[ 9] = RPin9;    CPinWrite[ 9] = WPin9;
 CPinRead[10] = RPin10;   CPinWrite[10] = WPin10; WPin10->Enabled = FALSE;
 CPinRead[11] = RPin11;   CPinWrite[11] = WPin11; WPin11->Enabled = FALSE;
 CPinRead[12] = RPin12;   CPinWrite[12] = WPin12; WPin12->Enabled = FALSE;
 CPinRead[13] = RPin13;   CPinWrite[13] = WPin13; WPin13->Enabled = FALSE;
 CPinRead[14] = RPin14;   CPinWrite[14] = WPin14;
 CPinRead[15] = RPin15;   CPinWrite[15] = WPin15; WPin15->Enabled = FALSE;
 CPinRead[16] = RPin16;   CPinWrite[16] = WPin16;
 CPinRead[17] = RPin17;   CPinWrite[17] = WPin17;


  if ((GetVersion() & 0x80000000) != 0)
     Label3->Caption = "Windows 95/98";
  else
     Label3->Caption = "Windows NT";
  PhysAddr = MyIniFile->ReadInteger("misc","ADDR",0xC0044);
  IRQ = (short)MyIniFile->ReadInteger("misc","IRQ",10);
  SpinIRQ->Position = IRQ; L_IRQ->Caption = IntToStr(IRQ);
  E_Addr->Text = IntToHex(PhysAddr,8);
  for (i=1; i<=MaxPorts; i++) {
      GWrite->Cells[0][i] = IntToStr(i);
      GWrite->Cells[1][0] = "PORT";
      GWrite->Cells[2][0] = "VAL";
      GWrite->Cells[1][i] = MyIniFile->ReadString("PortW","Port"+IntToStr(i),"");
      GWrite->Cells[2][i] = MyIniFile->ReadString("Values","Val"+IntToStr(i),"");
      GRead->Cells[0][i] = IntToStr(i);
      GRead->Cells[1][0] = "PORT";
      GRead->Cells[2][0] = "VAL";
      GRead->Cells[1][i] = MyIniFile->ReadString("PortR","Port"+IntToStr(i),"");
  };
 MyIniFile->Free();
 MemoHex->Cells[0][0] = "  ADDR";
 MemoHex->Cells[1][0] = "             HEX";
 MemoHex->Cells[2][0] = "     ASCII";
 ShowButtons();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_WriteClick(TObject *Sender)
{
   PortWSel=(Word)HexToInt(GWrite->Cells[1][GWrite->Row]);
   GWrite->Cells[1][GWrite->Row]=IntToHex(PortWSel,4);
   ValWSel=(UCHAR)HexToInt(GWrite->Cells[2][GWrite->Row]);
   GWrite->Cells[2][GWrite->Row]=IntToHex(ValWSel,2);
   NomWSel=(Word)GWrite->Row;
   if (PortWSel==0) {MessageBeep(0); return;}
   SetPortByte(HwCtrl,PortWSel,ValWSel);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_WriteAllClick(TObject *Sender)
{
  Word i,P,V;
  for (i=1; i<=MaxPorts; i++) {
    P=(Word)HexToInt(GWrite->Cells[1][i]); GWrite->Cells[1][i]=IntToHex(P,4);
    if (P) {
      V=(Word)HexToInt(GWrite->Cells[2][i]); GWrite->Cells[2][i]=IntToHex(V,2);
      SetPortByte(HwCtrl,P,V);
    }
   }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_ReadClick(TObject *Sender)
{
   PortRSel=(Word)HexToInt(GRead->Cells[1][GRead->Row]);
   GRead->Cells[1][GRead->Row]=IntToHex(PortRSel,4);
   NomRSel=(Word)GRead->Row;
   if (PortRSel==0) { MessageBeep(0); return; }
   GRead->Cells[2][GRead->Row]=IntToHex((int)GetPortByte(HwCtrl,PortRSel),2);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_ReadAllClick(TObject *Sender)
{
 UCHAR cl[17];
 Word  ports[17],P,N,i;

 N=0;
 for (i=1; i<=MaxPorts; i++) {
   P=(Word)HexToInt(GRead->Cells[1][i]);
   GRead->Cells[1][i]=IntToHex(P,4);
   if(P) {ports[++N]=P; cl[N]=(UCHAR) i;}
 }
 for (i=1; i<=N; i++)
   GRead->Cells[2][cl[i]]=IntToHex((int)GetPortByte(HwCtrl,ports[i]),2);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::C_HardClick(TObject *Sender)
{
  SetHardAccess(HwCtrl,C_Hard->Checked);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_SetMemoryClick(TObject *Sender)
{
  ULONG i,n;
  PhysAddr=0; n=0x10000000L;
  for (i=1; i<=8; i++){
    PhysAddr=PhysAddr+CharToNumber(E_Addr->Text[i])*n; n=n>>4;
  };
  E_Addr->Text = IntToHex(PhysAddr,8);
  PointPhys = (Byte*)MapPhysToLinear(HwCtrl,PhysAddr,256);
  B_SetMemory->Enabled = false;
  ShowButtons();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_ReadMemoryClick(TObject *Sender)
{
int i,j;
AnsiString s,s1;
char b;
Byte *LocP;

  if (PointPhys) {
    CurrAddr = PhysAddr;
    LocP = PointPhys;
    for (i=1; i<=16; i++) {
      MemoHex->Cells[0][i]=IntToHex(CurrAddr,8);
      s  = "";
      s1 = "";
      for (j=1; j<=16; j++) {
        s = s+IntToHex(*LocP,2);
        b=*LocP++;
        if (b<0x20) b='.';
        s1 = s1 + b;
      }
      MemoHex->Cells[1][i]=s;
      MemoHex->Cells[2][i]=s1;
      CurrAddr+=16;
    }

  }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::E_AddrChange(TObject *Sender)
{
B_SetMemory->Enabled = true;	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_MaskClick(TObject *Sender)
{
  if (!ActiveHW) return;
  if (!B_Mask->Checked) {
      Sum_Ticks = Flag_Tim;
      MaskIRQ(HwCtrl);
      SetPortByte(HwCtrl,(Word)GetLPTBasePort(HwCtrl)+2,(Byte)0x00);
      C_LPT_IRQ->Checked = FALSE;
  }
  else {
      IRQ  =  SpinIRQ->Position;
      SetIRQ(HwCtrl,(BYTE)IRQ, HWCtrlHwInterrupt);
      Flag_Intr  =  0;
      Sum_Ticks  =  0;
      Flag_Tim   =  0;
      Scan_Code  =  0;
      UnmaskIRQ(HwCtrl);
   };
   ShowButtons();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::C_LPT_IRQClick(TObject *Sender)
{
  Word n = GetLPTBasePort(HwCtrl)+2;
  if (C_LPT_IRQ->Checked)
    SetPortByte(HwCtrl,n,0x10);
  else
    SetPortByte(HwCtrl,n,0x00);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WPin1Click(TObject *Sender)
{
  for (Byte nPin = 1; nPin<=17; nPin++)
     SetPin(HwCtrl,nPin,(BOOL)CPinWrite[nPin]->Checked);
	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::C_InitClick(TObject *Sender)
{
  LPTInit(HwCtrl);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_ClearClick(TObject *Sender)
{
  TextMemo->Clear();	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_PrintClick(TObject *Sender)
{
    if (FlagPrint) return;
    NumLine = 0;
    NumSymbol = 1;
    FlagPrint = TRUE;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_StopClick(TObject *Sender)
{
  FlagPrint = FALSE;	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_OpenClick(TObject *Sender)
{
  HwCtrl = NULL;
  HwCtrl = OpenTVicHW32(HwCtrl);
  ActiveHW = GetActiveHW(HwCtrl);
//  L_Debug.caption:='DebugCode='+IntToStr(HwCtrl.DebugCode);
  if (!ActiveHW) {
    MessageBeep(0);
    Application->MessageBox("Driver  VICHWxx  not found",
                      " Warning! ",IDOK);
  }
  else {
         IRQ = SpinIRQ->Position;
//         IRQNumber = (Byte)IRQ;
         Timer1->Enabled = TRUE;
       };
  B_SetMemory->Enabled = TRUE;
  ShowButtons();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_CloseDriverClick(TObject *Sender)
{
  Timer1->Enabled = FALSE;
  ActiveHW = FALSE;
  SetPortByte(HwCtrl,GetLPTBasePort(HwCtrl)+2,0x00);
  C_LPT_IRQ->Checked = FALSE;
  HwCtrl = CloseTVicHW32(HwCtrl);
  PointPhys = NULL;
  B_SetMemory->Enabled = FALSE;
  Flag_Intr = 0;
  ShowButtons();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
//    ShortString s;
    AnsiString s;
    if (ActiveHW) L_Gen->Caption = IntToStr(IRQCounter);
//    L_Debug->caption = "DebugCode="+IntToStr(DebugCode);
    L_ScanCode->Caption = ": "+IntToHex(Scan_Code,2)+"h";
    L_LPT_Data->Caption = ": "+IntToHex(Data_Reg,2)+"h";
    L_LPT_STATUS->Caption = ": "+IntToHex(Status_Reg,2)+"h";
    L_Timers->Caption = IntToStr(Flag_Tim / 1000);
    L_Flag->Caption = IntToStr(Flag_Intr);
    if (ActiveHW && (!IsIRQMasked(HwCtrl))){
      CurrTicker = GetTickCount();
      Flag_Tim = Sum_Ticks+CurrTicker-OldTicker;
    }
    else  OldTicker = GetTickCount();
    for ( Byte nPin = 1; nPin<=17;  nPin++)
      CPinRead[nPin]->Checked = GetPin(HwCtrl,nPin);
    C_ACKWL->Checked  =  GetLPTAckwl(HwCtrl);
    C_BUSY->Checked   =  GetLPTBusy(HwCtrl);
    C_ERROR->Checked  =  GetLPTError(HwCtrl);
    C_PE->Checked     =  GetLPTPaperEnd(HwCtrl);
    C_SLCT->Checked   =  GetLPTSlct(HwCtrl);

    if (FlagPrint) {
      Timer1->Enabled = FALSE;
      for ( Byte i = 1; i<100; i++) {
        Application->ProcessMessages();
        s = TextMemo->Lines->Strings[NumLine];
        s = s+(char)0x0D+(char)0x0A;
        if (NumSymbol>(Word)strlen(s.c_str())) {
          if (NumLine == TextMemo->Lines->Count) {
             Timer1->Enabled = TRUE;
             FlagPrint = FALSE; return;
          };
          NumLine++;
          s = TextMemo->Lines->Strings[NumLine]+(char)0x0D+(char)0x0A;
          NumSymbol = 1;
        };
        if (LPTPrintChar(HwCtrl,s[NumSymbol])) NumSymbol++;
      };
      Timer1->Enabled = TRUE;
    };

    B_Stop->Enabled = FlagPrint;
    B_Print->Enabled = ! FlagPrint;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpinIRQClick(TObject *Sender, TUDBtnType Button)
{
  IRQ = SpinIRQ->Position;
  L_IRQ->Caption = IntToStr(IRQ);
  ShowButtons();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpinLPTClick(TObject *Sender, TUDBtnType Button)
{
  SetLPTNumber(HwCtrl,SpinLPT->Position);
  ShowButtons();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
 int i;

 TIniFile* MyIniFile = new TIniFile("HW_test");
 MyIniFile->WriteInteger("misc","ADDR",PhysAddr);
 MyIniFile->WriteInteger("misc","IRQ",IRQ);
 for (i = 1; i<=MaxPorts; i++) {
      MyIniFile->WriteString("PortW","Port"+IntToStr(i),GWrite->Cells[1][i]);
      MyIniFile->WriteString("Values","Val"+IntToStr(i),GWrite->Cells[2][i]);
      MyIniFile->WriteString("PortR","Port"+IntToStr(i),GRead->Cells[1][i]);
 };
 MyIniFile->Free();
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HwCtrlHwInterrupt(TObject *Sender, int HwCounter,
	short LPT_DataReg, short LPT_StatusReg, short ScanCode)
{
  Data_Reg   = LPT_DataReg;
  Status_Reg = LPT_StatusReg;
  Scan_Code  = ScanCode;
  IRQCounter = HwCounter;
  Flag_Intr++;

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

⌨️ 快捷键说明

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