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

📄 vb对低层硬件访问控制.htm

📁 VB对低层硬件访问控制
💻 HTM
字号:
<!--STATUS OK--><form action=http://www.baidu.com/baidu><table bgcolor=ffffff border=0 width=100% cellpadding=0><tr><td width=254><a href=http://www.baidu.com><img src=http://img.baidu.com/images/logo_kz.gif border=0></a><td><font size=-1 color=black><input type=hidden name=tn value=baidu><input name=word size=30 style="font-size:16px;font-family:Verdana,Arial,Helvetica,sans-serif;"> <input type=submit value=百度搜索></font></td></tr><tr><td colspan=2><font size=-1 color=black>您的查询字词都已标明如下:<B style='color:black;background-color:#A0FFFF'><a href=#1>vb</a>&nbsp;</B><B style='color:black;background-color:#99ff99'><a href=#2>编程</a>&nbsp;</B> (点击查询词,可以跳到它在文中首次出现的位置)<br><font color=gray>(百度和网页<a href="http://vbeden.xg88.com/vbtech/other/page_1/file16.htm"><font color=blue>http://vbeden.xg88.com/vbtech/other/page_1/file16.htm</font></a>的作者无关,不对其内容负责。)</font></font></td></tr></table></form><hr><base href="http://vbeden.xg88.com/vbtech/other/page_1/file16.htm"><meta http-equiv="Content-Type" Content="text/html; charset=gb2312"><html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>VB对低层硬件访问控制</title>
<style type="text/css">font         { font-size: 9pt; line-height: 13pt }
p            { font-size: 9pt; line-height: 13pt }
td           { font-size: 9pt; line-height: 13pt }
select       { font-family: 宋体; font-size: 9pt }
input        { font-family: 宋体; font-size: 9pt }
textarea     { font-family: 宋体; font-size: 9pt }
a            { color: blue; font-size: 9pt; text-decoration: none }
a:hover      { color: red; text-decoration: underline }
a:active     { color: red; text-decoration: underline }
</style>
</head>

<body bgcolor="#FEEAA5">

<div align="center">
<center>
<table border="0" width="100%">
<tr>
<td width="100%" align="center"><font color="#FF0000"><a name=1></a><B style='color:black;background-color:#A0FFFF'>VB</B>对低层硬件访问控制</font></td>
</tr>
<tr>
<td><B style='color:black;background-color:#A0FFFF'>VB</B> 没 有 提 供 直 接 访 问 低 层 硬 件 的 控 件 和 方 法, 一 度 给 对 访 问 硬 件 感 兴 趣 的 编 程 者 带 来 不 便。 目 前 我 们 可 从 网 上 搜 索 到 支 持 低 层 硬 件 访 问 的DLL 和ActiveX 控 件, 通 过 它 们 可 读 写 存 储 器 单 元、 端 口, 甚 至 控 制 硬 件 中 断。 下 面 通 过 两 个 利 用DLL 和ActiveX 控 件 示 例 介 绍<B style='color:black;background-color:#A0FFFF'>VB</B> 对 低 层 硬 件 的 访 问 控 制。 
<p>一、 利 用DLL 读 写 端 口</p> 
<p>----若 在 应 用 程 序 中 只 是 简 单 地 读 写 端 口, 利 用DLL 编 程 实 现 较 为 简 便。 从http://personal.vsnl.com/sr 网 站 可 下 载 一 个 免 费 的32 位<B style='color:black;background-color:#A0FFFF'>VB</B>IO.DLL, 该 连 接 库 允 许 在<B style='color:black;background-color:#A0FFFF'>VB</B>4、5 或6 中 使 用, 共 有 七 个 函 数 和 过 程, 分 别 为:</p> 
<p>Anjan DLL 的 解 锁 过 程<br> 
Inp 端 口 读 字 节 函 数<br> 
Inpw 端 口 读 字 函 数<br> 
Out 端 口 写 字 节 过 程<br> 
Outw 端 口 写 字 过 程<br> 
GetLptBaseAddr 获 取 并 口 基 地 址 的 函 数<br> 
GetComBaseAddr 获 取 串 口 基 地 址 的 函 数<br> 
</p>
<p>----图1 是 一 个 发 声 示 例 程 序 的 窗 体, 在 输 入 框 中 键 入 一 频 率 值 并 按SoundOn 钮, 则 在PC 机 的 扬 声 器 中 发 出 指 定 频 率 音 调, 程 序 中 对 音 调 的 变 化、 声 音 的 开 关 是 用<B style='color:black;background-color:#A0FFFF'>VB</B>IO.DLL 的 过 程 和 函 数 访 问 发 声 系 统 的 定 时 器/ 计 数 器 和 控 制 端 口 实 现 的。 编 程 要 点:1. 应 在Form _Load 中 加 入Anjan 解 锁 过 程。2. 若 在 模 块 中 声 明 函 数 和 过 程, 应 去 掉private 或 用Public 替 代。3.<B style='color:black;background-color:#A0FFFF'>VB</B>IO.DLL 应 拷 贝 到 \windows\system  
子 目 录 下。 
<p>----<b>程 序 清 单:</b> 
<p>Option Explicit<br> 
Private Declare Sub Anjan Lib<br> 
“<B style='color:black;background-color:#A0FFFF'>vb</B>io.dll" ()<br> 
Private Declare Function Inp Lib<br> 
“<B style='color:black;background-color:#A0FFFF'>vb</B>io.dll" (ByVal port &) As Integer<br> 
Private Declare Function Inpw Lib<br> 
“<B style='color:black;background-color:#A0FFFF'>vb</B>io.dll" (ByVal port &) As Long<br> 
Private Declare Sub Out Lib<br> 
“<B style='color:black;background-color:#A0FFFF'>vb</B>io.dll" (ByVal port &, ByVal byt %)<br> 
Private Declare Sub Outw Lib<br> 
“<B style='color:black;background-color:#A0FFFF'>vb</B>io.dll" (ByVal port &, ByVal wrd &)<br> 
Private Declare Function GetLptBaseAddr Lib<br> 
“<B style='color:black;background-color:#A0FFFF'>vb</B>io.dll" (ByVal lpt &) As Integer<br> 
Private Declare Function GetComBaseAddr Lib<br> 
“<B style='color:black;background-color:#A0FFFF'>vb</B>io.dll" (ByVal com &) As Integer<br> 
<br>
Public Sub SetFreq(soundHz As Integer) ' 设 置 频 率<br> 
 If soundHz Then<br> 
 Dim divisor As Long<br> 
 divisor = 1193180 / soundHz ' 计 算 时 间 常 数<br> 
 Out &H42, &HB6<br> 
 '8253 -5 通 道2 设 置 为 方 式3<br> 
 Out &H42, divisor Mod 256 ' 送 时 间 常 数<br> 
 Out &H42, divisor \ 256 '<br> 
 Speaker True<br> 
 Else<br> 
 Speaker False<br> 
 End If<br> 
End Sub<br> 
<br>
Public Sub Speaker(sOn As Boolean) ' 开 关 声 音<br> 
 Dim portVal As Integer<br> 
 portVal = Inp( &H61)<br> 
 If sOn Then<br> 
 portVal = portVal Or 3<br> 
 ' 低 位 为 通 道2 的 门 控 信 号<br> 
 Else ' 次 低 位 为 整 形 与 门 控 制 信 号<br> 
 portVal = portVal And (Not 3)<br> 
 End If<br> 
 Out &H61, portVal<br> 
<br>
End Sub<br> 
<br>
Private Sub Form_Load()<br> 
Anjan ' 软 件 解 锁<br> 
End Sub<br> 
<br>
Private Sub SoundOff_Click()<br> 
Speaker False<br> 
End Sub<br> 
<br>
Private Sub SoundOn_Click()<br> 
SetFreq Val(TextHz)<br> 
End Sub<br> 
</p>
<p>二、 利 用ActiveX 处 理 硬 件 中 断</p> 
<br>
----在 应 用 程 序 中 如 果 需 要 访 问 存 储 单 元、 端 口 以 及 处 理 硬 件 中 断, 使 用TVicHW32 ActiveX 控 件 是 一 很 好 的 选 择, 该 控 件 是 一 个 共 享 软 件, 支 持Windows 95/98/NT, 可 从 http://www.entechtaiwan.com/tools.htm 处 下 载。 该 控 件 除 具 备 直 接 访 问 存 储 单 元 和 端 口 的 功 能 外, 还 提 供 了 丰 富 的 处 理 并 口 的 属 性 和 方 法, 以 及 处 理 硬 件 中 断 的 属 性、 方 法 和 事 件, 极 大 地 拓 展 了<B style='color:black;background-color:#A0FFFF'>VB</B> 对 低 层 硬 件 的 访 问 控  
制。 下 面 通 过 一 个 显 示 键 盘 中 断 次 数 和 按 键 扫 描 码 的 示 例 介 绍 控 件 的 使 用 过 程。 
<ol>
<li>下 载TVicHW32 压 缩 软 件 包 并 解 压 到 一 个 目 录 中, 如\HW。 把 driver 子 目 录 下 的vichw00.vxd 文 件 拷 贝 到\windows\system 子 目 录 下, 该 文 件 是 控 件 访 问 硬 件 的 驱 动 程 序, 使 用 控 件 前 先 用OpenDriver 打 开, 最 后 用Close_Driver 方 法 关 闭。 
<li>把ocx 子 目 录 下 的tvichw32.ocx 拷 贝 到\windows\system 子 目 录 下, 并 在 DOS 命 令 行 状 态 下 键 入 以 下 命 令 进 行 注 册:<br> 
----regsvr32 tvichw32.ocx 
<li>在<B style='color:black;background-color:#A0FFFF'>VB</B> 环 境 下 通 过 菜 单 工 程 - - 部 件 - - 控 件 并 选 择TVicHW32 ActiveX Control Module 将 控 件 添 加 到 工 具 箱 中。 
<li>相 关 的 属 性、 方 法 及 事 件</li>
</ol>
<p> 方 法 OpenDriver 打 开 支 持 访 问 硬 件<br> 
 的 驱 动 程 序vichw.vxd(windows95 下)<br> 
 方 法 CloseDriver 关 闭 驱 动 程 序<br> 
 属 性 ActiveHW As Bool 驱 动 程 序 打 开<br> 
 则 为True; 关 闭 为False<br> 
 中 断 事 件 OnHwInterrupt(ByVal HwCounter As Long,<br> 
 ByVal LPT_DataReg As Integer,<br> 
    ByVal LPT_StatusReg As Integer,<br> 
    ByVal ScanCode As Integer<br> 
   )<br>
    参 数<br> 
  HwCounter : 中 断 次 数<br> 
 LPT_DataReg : 如 果 使 用IRQ7,<br> 
 则 为 打 印 并 口 的 数 据<br> 
 LPT_StatusReg : 如 果 使 用IRQ7,<br> 
 则 为 打 印 并 口 的 数 据<br> 
 ScanKode : 如 果 使 用IRQ1,<br> 
 则 为 按 键 的 扫 描 码<br> 
 属 性 IRQNumber 指 定 中 断 号,<br> 
 范 围IRQ1 - -15<br> 
 属 性 IRQMasked 中 断 非 屏 蔽 则 为True;<br> 
 屏 蔽 为False。<br> 
</p>
<p>----图2 是 示 例 的 窗 体, 程 序 运 行 后 首 先 按 Open_Driver 钮 打 开 驱 动 程 序, 然 后 选 择Unmarsk 复 选 框 开 放 中 断, 此 时 每 按 一 次 键 框 中 分 别 显 示 该 键 的 扫 描 码 和 中 断 次 数。 处 理 其 他 中 断 只 需 更 改 中 断 号 即 可( 中 断 号1 —15)。 
<p>----程 序 清 单: 
<p>Public Sub ShowButtons()<br> 
 Open_Driver.Enabled = Not HwCtrl.ActiveHW<br> 
 Close_Driver.Enabled = HwCtrl.ActiveHW<br> 
 B_Unmask.Enabled = HwCtrl.ActiveHW<br> 
End Sub<br> 
<br>
Private Sub Form_Load()<br> 
ShowButtons<br>
End Sub<br> 
<br>
Private Sub Open_Driver_Click()<br> 
 HwCtrl.OpenDriver ' 打 开 驱 动 程 序<br> 
 If Not HwCtrl.ActiveHW Then<br> 
 MsgBox (“The driver VICHWxx not found")<br> 
 Else:<br> 
 HwCtrl.IRQNumber = 1 ' 中 断 号 为1, 键 盘 中 断<br> 
 End If<br> 
 ShowButtons<br> 
End Sub<br> 
<br>
Private Sub Close_Driver_Click()<br> 
HwCtrl.CloseDriver ' 关 闭 驱 动 程 序<br> 
B_Unmask.Value = 0<br> 
ShowButtons<br>
End Sub<br> 
<br>
Private Sub B_Unmask_Click()<br> 
If B_Unmask.Value = 0 Then<br> 
 HwCtrl.IRQMasked = True<br> 
 Else<br> 
 HwCtrl.IRQNumber = 1<br> 
 Scan_Code = 0<br> 
 HwCtrl.IRQMasked = False ' 开 放 中 断<br> 
 End If<br> 
End Sub<br> 
<br>
Private Sub HwCtrl_OnHwInterrupt<br> 
(ByVal HwCounter As Long, ByVal LPT_DataReg As Integer,<br> 
ByVal LPT_StatusReg As Integer, ByVal ScanCode As Integer)<br> 
Scan_Code.Caption = ScanCode<br> 
IRQC.Caption = HwCounter<br> 
End Sub</td> 
</tr>
</table>
</center>
</div>
<div align="center">
<center>
<table border="0" width="100%" cellspacing="1">
<tr>
<td width="100%">
<p align="center"><a name=2></a><a href="http://www.vbeden.com" target="_blank"><B style='color:black;background-color:#A0FFFF'>VB</B><B style='color:black;background-color:#99ff99'>编程</B>乐园</a>版权所有<font color="#000080">(c),1999-2000</font><script>var tc_user="vbboshi";var tc_class="2";</script><script src="http://stat.t2t2.com/stat.js"></script><font color="#000080"><br>
</font>网页设计制作:<font color="#000080"><a href="mailto:MyWebVB@21cn.com">LiZhiDong</a></font> </td> 
</tr>
</table>
</center>
</div>

</body>

</html>

⌨️ 快捷键说明

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