unit1.cpp

来自「I/O端口访问,能在Win98下读写任意端口内容」· C++ 代码 · 共 82 行

CPP
82
字号
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TDLPortIO"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
   // Default port
    PortEdit->Text="0x378"; // LPT1:
    DataEdit->Text="0x00";

    // Driver is in the same directory as the demo.exe file!
    DLPortIO1->DriverPath=ExtractFileDir(ParamStr(0));
    // Open the DriverLINX driver
    DLPortIO1->OpenDriver();
   if (!DLPortIO1->ActiveHW)
      MessageDlg("Could not open the DriverLINX driver.",
                 mtError, TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ExitClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReadButtonClick(TObject *Sender)
{
WORD DataPort;  // Port to read data from
DWORD DataRead; // Data read from port

   // Get the data port address
   try {
      DataPort=(WORD)StrToInt(PortEdit->Text);
   } catch (...) {
      MessageDlg("You have specified an invalid port.\nNo action performed.",
                 mtError, TMsgDlgButtons() << mbOK, 0);

      return;
   }
    // Read the data
    DataRead=DLPortIO1->Port[DataPort];
    DataEdit->Text="0x"+IntToHex(DataRead, 0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::WriteButtonClick(TObject *Sender)
{
WORD DataPort;   // Port to read data from
DWORD DataWrite; // Data to write to port

   // Get the data port address
   try {
      DataPort=(WORD)StrToInt(PortEdit->Text);
   } catch (...) {
      MessageDlg("You have specified an invalid port.\nNo action performed.",
                 mtError, TMsgDlgButtons() << mbOK, 0);
      return;
   }

   // Get the data to write
   try {
      DataWrite=(DWORD)StrToInt(DataEdit->Text);
   } catch (...) {
      MessageDlg("You have specified an invalid port.\nNo action performed.",
                 mtError, TMsgDlgButtons() << mbOK, 0);
      return;
   }

   // Write the data
    DLPortIO1->Port[DataPort]=(BYTE)(DataWrite&0xFF);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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