【发布时间】:2011-11-23 10:20:39
【问题描述】:
我得到了这个简单的 VB 应用程序和库,我被告知可以打开连接到 0x378 基地址的打印机端口的门/turnstyle。
'Inp and Out declarations for port I/O using inpout32.dll.
Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" _
(ByVal PortAddress As Integer) _
As Integer
Public Declare Sub Out Lib "inpout32.dll" Alias "Out32" _
(ByVal PortAddress As Integer, _
ByVal Value As Integer)
------------------------------------------------------------------------------------
Option Explicit
Dim Value As Integer
Dim PortAddress As Integer
Private Sub cmdWriteToPort_Click()
'Write to a port.
Out PortAddress, Value
'Read back and display the result.
Text1.Text = Inp(PortAddress)
Value = Value + 1
If Value = 255 Then Value = 0
End Sub
Private Sub Form_Load()
'Test program for inpout32.dll
Value = 0
'Change PortAddress to match the port address to write to:
'(Usual parallel-port addresses are &h378, &h278, &h3BC)
PortAddress = &H378
End Sub
但是我需要在 Delphi 5 中重新编写它以集成到我的应用程序中。
- 是否可以通过 D5 访问同一个库?
- 使用以下代码,我的方向是否正确?
//使用库的端口 I/O 的 Inp 和 Out 声明
function Inp(PortAddress:String); external 'inpout32.dll.dll'
begin
return ??
end;
procedure Output(PortAddress:String;Value:Integer); external 'inpout32.dll.dll'
procedure TForm1.FormActivate(Sender: TObject);
begin
//Test program for inpout32.dll
Value := 0;
//Change PortAddress to match the port address to write to:
//(Usual parallel-port addresses are &h378, &h278, &h3BC)
PortAddress := '&H378';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//Write to a port.
Output(PortAddress, Value);
//Read back and display the result.
Edit1.Text := Inp(PortAddress);
Value := Value + 1;
if Value = 255 then
Value := 0;
end;
我不确定如何声明库函数以及将变量声明为什么(&H378 显然不是整数)
谢谢
【问题讨论】:
-
&H378 是一个十六进制值:十进制的 888。不知道这是否适合 Delphi 的整数类型。
-
@Xopmeister 谢谢。我猜库函数在这种情况下只接受十六进制,但不确定。无论如何,删除 H 似乎让它的值设置为 378 美元......
-
您使用的是串行端口还是并行端口?我问的原因是其中一个 cmets 似乎在讨论并行端口。
-
你说得对,代码实际上是在尝试与并行端口而不是串行端口通信。这就是为什么我在不同的 PC 上得到不同结果的原因。我什至不知道并行端口是打印机端口(哎呀)
标签: vb.net delphi parallel-port