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

📄 mainform.cpp

📁 串口程序
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include <vcl\IniFiles.hpp>
#include "mainform.h"
//---------------------------------------------------------------------------
#pragma link "Grids"
#pragma link "HW_32"
#pragma resource "*.dfm"

#define MaxPorts  8

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

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;
}
//---------------------------------------------------------------------------
void ShowButtons()
{
   Form1->B_SetMemory->Enabled  =  Form1->HwCtrl->ActiveHW;
   if (!Form1->HwCtrl->ActiveHW) Form1->B_Mask->Checked  =  FALSE;
   Form1->SpinLPT->Enabled = Form1->HwCtrl->ActiveHW;
   Form1->SpinLPT->Max = Form1->HwCtrl->LPTNumPorts;
   Form1->L_LPTNumber->Caption = IntToStr(Form1->SpinLPT->Position);
   Form1->L_LPTs->Caption = IntToStr(Form1->HwCtrl->LPTNumPorts);
   Form1->L_Base->Caption = IntToHex(Form1->HwCtrl->LPTBasePort,3)+'h';
   Form1->C_Hard->Checked  =  Form1->HwCtrl->HardAccess;
   Form1->C_Hard->Enabled  =  Form1->HwCtrl->ActiveHW;
   Form1->SpinIRQ->Enabled  =  Form1->HwCtrl->ActiveHW && (!Form1->B_Mask->Checked);
   Form1->B_Open->Enabled = !Form1->HwCtrl->ActiveHW;
   Form1->B_CloseDriver->Enabled  =  Form1->HwCtrl->ActiveHW;
   Form1->B_Write->Enabled = Form1->HwCtrl->ActiveHW;
   Form1->B_Read->Enabled = Form1->HwCtrl->ActiveHW;
   Form1->B_WriteAll->Enabled = Form1->HwCtrl->ActiveHW;
   Form1->B_ReadAll->Enabled = Form1->HwCtrl->ActiveHW;
   Form1->C_Init->Enabled = Form1->HwCtrl->ActiveHW;
   Form1->B_Print->Enabled = Form1->HwCtrl->ActiveHW;
   Form1->B_Stop->Enabled = Form1->HwCtrl->ActiveHW;
   Form1->B_ReadMemory->Enabled = Form1->HwCtrl->ActiveHW && (PointPhys!=NULL);
   Form1->B_FillMemory->Enabled = Form1->HwCtrl->ActiveHW && (PointPhys!=NULL);
   Form1->B_Mask->Enabled = Form1->HwCtrl->ActiveHW &&
                           (Form1->HwCtrl->IRQNumber>0) &&
                           (Form1->HwCtrl->IRQNumber<16);
   Form1->C_LPT_IRQ->Enabled = Form1->HwCtrl->ActiveHW &&
       ((Form1->HwCtrl->IRQNumber==7 && Form1->HwCtrl->LPTNumber == 1) ||
       (Form1->HwCtrl->IRQNumber==5  && Form1->HwCtrl->LPTNumber == 2));
   for (int nPin = 1;  nPin<18; nPin++) {
     if (!Form1->HwCtrl->ActiveHW) CPinRead[nPin]->Checked = FALSE;
     if (!Form1->HwCtrl->ActiveHW) CPinWrite[nPin]->Checked = FALSE;
     CPinWrite[nPin]->Enabled = Form1->HwCtrl->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((__int64)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;}
   HwCtrl->Port[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);
      HwCtrl->Port[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)HwCtrl->Port[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)HwCtrl->Port[ports[i]],2);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::C_HardClick(TObject *Sender)
{
  HwCtrl->HardAccess = 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((__int64)PhysAddr,8);
  PointPhys = (Byte*)HwCtrl->MapPhysToLinear(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((__int64)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 (!B_Mask->Checked) {
      Sum_Ticks = Flag_Tim;
      HwCtrl->IRQMasked  =  TRUE;
      HwCtrl->Port[Word(HwCtrl->LPTBasePort+2)] = (Byte)0x00;
      C_LPT_IRQ->Checked = FALSE;
  }
  else {
      IRQ  =  SpinIRQ->Position; 
      HwCtrl->IRQNumber   =  (Byte)IRQ;
      Flag_Intr  =  0;
      Sum_Ticks  =  0;
      Flag_Tim   =  0;
      Scan_Code  =  0;
      OldTicker = GetTickCount();
      CurrTicker = GetTickCount();
      HwCtrl->IRQMasked  =  FALSE;
   };
   ShowButtons();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::C_LPT_IRQClick(TObject *Sender)
{
  Word n = Word(HwCtrl->LPTBasePort+2);
  if (C_LPT_IRQ->Checked)
    HwCtrl->Port[n] = (Byte)0x10;
  else
    HwCtrl->Port[n] = (Byte)0x00;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WPin1Click(TObject *Sender)
{
  for (Byte nPin = 1; nPin<=17; nPin++)
     HwCtrl->Pin[nPin] = (boolean)CPinWrite[nPin]->Checked;
	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::C_InitClick(TObject *Sender)
{
  HwCtrl->LPTInit();	
}
//---------------------------------------------------------------------------
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->OpenDriver();
//  L_Debug.caption:='DebugCode='+IntToStr(HwCtrl.DebugCode);
  if (!HwCtrl->ActiveHW) {
    MessageBeep(0);
    Application->MessageBox("Driver  VICHWxx  not found",
                      " Warning! ",IDOK);
  }
  else {
         IRQ = SpinIRQ->Position;
         HwCtrl->IRQNumber = (Byte)IRQ;
         Timer1->Enabled = TRUE;
       };
  B_SetMemory->Enabled = TRUE;
  ShowButtons();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_CloseDriverClick(TObject *Sender)
{
  Timer1->Enabled = FALSE;
  HwCtrl->Port[Word(HwCtrl->LPTBasePort+2)] = 0x00;
  C_LPT_IRQ->Checked = FALSE;
  HwCtrl->CloseDriver();
  B_Mask->Checked = FALSE;
  PointPhys = NULL;
  B_SetMemory->Enabled = FALSE;
  Flag_Intr = 0;
  ShowButtons();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
//    ShortString s;
    AnsiString s;
    if (HwCtrl->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 (HwCtrl->ActiveHW && (!HwCtrl->IRQMasked)){
      CurrTicker = GetTickCount();
      Flag_Tim = Sum_Ticks+CurrTicker-OldTicker;
    }
    else  OldTicker = GetTickCount();
    for ( Byte nPin = 1; nPin<=17;  nPin++)
      CPinRead[nPin]->Checked = HwCtrl->Pin[nPin];

    C_ACKWL->Checked  =  HwCtrl->LPTAckwl;
    C_BUSY->Checked   =  HwCtrl->LPTBusy;
    C_ERROR->Checked  =  HwCtrl->LPTError;
    C_PE->Checked     =  HwCtrl->LPTPaperEnd;
    C_SLCT->Checked   =  HwCtrl->LPTSlct;

    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 (HwCtrl->LPTPrintChar(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);
  HwCtrl->IRQNumber = IRQ;
  ShowButtons();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpinLPTClick(TObject *Sender, TUDBtnType Button)
{
  HwCtrl->LPTNumber = 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,
      BYTE LPT_DataReg, BYTE LPT_StatusReg, BYTE Keyb_ScanCode)
{
  Data_Reg   = LPT_DataReg;
  Status_Reg = LPT_StatusReg;
  Scan_Code  = Keyb_ScanCode;
  IRQCounter = HwCounter;
  Flag_Intr++;
    
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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