Skip to content

Commit

Permalink
add LPT class
Browse files Browse the repository at this point in the history
  • Loading branch information
何秀奇 committed Jul 3, 2024
1 parent 9c0147c commit 2f175ad
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CommunTools/CommunTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<None Include="UseLib\InpOut\INPOUT32.DPR" />
<None Include="UseLib\InpOut\INPOUT32.FRM" />
<None Include="UseLib\InpOut\INPOUT32.VBP" />
<None Include="UseLib\InpOut\INPOUT32.vbw" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Commun.NetWork\Commun.NetWork.csproj">
Expand All @@ -203,6 +207,9 @@
</ItemGroup>
<ItemGroup>
<Content Include="login.ico" />
<Content Include="UseLib\InpOut\INPOUT32.BAS" />
<Content Include="UseLib\InpOut\INPOUT32.DLL" />
<Content Include="UseLib\InpOut\INPOUT32.TXT" />
<None Include="Resources\shichang.png" />
<None Include="Resources\close.png" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions CommunTools/FrmBaseCom/Frm_ComLPT.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 41 additions & 7 deletions CommunTools/FrmBaseCom/Frm_ComLPT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,65 @@ public Frm_ComLPT()
InitializeComponent();
}

static int portAddress = 0x378; // Typically, 0x378 is the address for LPT1
static int portDecData = 0xFF;

public class PortControl // Import dll to project
{
[DllImport("inpout32.dll", EntryPoint = "Out32")]
public static extern void Output(int portAddress, int value); // decimal
public static extern void Output(int portAddress, int data); // decimal

[DllImport("inpout32.dll", EntryPoint = "Inp32")]
public static extern int Input(int portAddress);
}

static int portAddress = 0x378; // Typically, 0x378 is the address for LPT1
static int portDecData = 0xFF;
public static void WriteDataPort(int data)
{
Output(portAddress, data);
}

public static void WriteControlPort(int data)
{
data = data ^ 0xB;
data = data & 0xF;
Output(0x037A, data);
}

public static int ReadControlPort()
{
int data = Input(0x037A);
data = data ^ 0xB;
data = data & 0xF;
return data;
}

public static int ReadStatusPort()
{
int ValueGet = Input(0x0379);

ValueGet = ValueGet ^ 0x80;
ValueGet = ValueGet & 0xF0;
ValueGet = ValueGet >> 4;

return ValueGet;
}
}

private void btnInp_BtnClick(object sender, EventArgs e)
{
// Read a value from the parallel port
Console.WriteLine("Reading value from the parallel port...");
int value = PortControl.Input(portAddress);
Console.WriteLine($"Value read from port: {value:X}");
int value = PortControl.ReadControlPort();
Console.WriteLine($"Value read from port: {value:X}");
richTextBox_Send.AppendText("读取数据 " + value + "\n");
}

private void btnOut_BtnClick(object sender, EventArgs e)
{
// Write a value to the parallel port
Console.WriteLine("Writing value to the parallel port...");
PortControl.Output(portAddress, portDecData); // Sending 0xFF (all bits high)
PortControl.WriteDataPort(portDecData); // Sending 0xFF (all bits high)
richTextBox_Send.AppendText ("写入数据 " + PortControl.ReadStatusPort()+"\n");

}
}
}
9 changes: 9 additions & 0 deletions CommunTools/UseLib/InpOut/INPOUT32.BAS
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Attribute VB_Name = "inpout"

'Inp and Out declarations for direct port I/O
'in 32-bit Visual Basic 4 programs.

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)
Binary file added CommunTools/UseLib/InpOut/INPOUT32.DLL
Binary file not shown.
38 changes: 38 additions & 0 deletions CommunTools/UseLib/InpOut/INPOUT32.DPR
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{Source code for inpout32.dll.
Enables 32-bit Visual Basic programs to do direct port I/O
(Inp and Out) under Windows 95.
To be compiled with Borland's Delphi 2.0.}
library inpout32;
uses SysUtils;
procedure Out32(PortAddress:smallint;Value:smallint);stdcall;export;
var
ByteValue:Byte;
begin
ByteValue:=Byte(Value);
asm
push dx
mov dx,PortAddress
mov al, ByteValue
out dx,al
pop dx
end;
end;

function Inp32(PortAddress:smallint):smallint;stdcall;export;
var
ByteValue:byte;
begin
asm
push dx
mov dx, PortAddress
in al,dx
mov ByteValue,al
pop dx
end;
Inp32:=smallint(ByteValue) and $00FF;
end;
Exports
Inp32,
Out32;
begin
end.
52 changes: 52 additions & 0 deletions CommunTools/UseLib/InpOut/INPOUT32.FRM
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
VERSION 4.00
Begin VB.Form inpout32
Caption = "Form1"
ClientHeight = 1692
ClientLeft = 912
ClientTop = 1404
ClientWidth = 2400
Height = 2076
Left = 864
LinkTopic = "Form1"
ScaleHeight = 1692
ScaleWidth = 2400
Top = 1068
Width = 2496
Begin VB.TextBox Text1
Height = 372
Left = 120
TabIndex = 1
Text = "Text1"
Top = 1080
Width = 2052
End
Begin VB.CommandButton cmdWriteToPort
Caption = "Write to Port"
Height = 732
Left = 240
TabIndex = 0
Top = 120
Width = 1932
End
End
Attribute VB_Name = "inpout32"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
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
72 changes: 72 additions & 0 deletions CommunTools/UseLib/InpOut/INPOUT32.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
Documentation for inpout32.zip

Inpout32.zip contains a DLL that enables direct reading and writing to I/O ports in 32-bit Visual-Basic programs running under Windows 95.

by Jan Axelson
Lakeview Research
Email: [email protected]
WWW: http://www.lvr.com

Important information and cautions:

1. Use this DLL at your own risk. Writing directly to hardware ports can result in system crashes, loss of data, and even permanent damage. Inpout32 was developed to allow access to parallel ports and other ports on custom hardware, but you can use it to attempt to access any hardware that is mapped as an I/O port. You've been warned!

2. Use this DLL only with 32-bit programs. 16-bit programs require a 16-bit DLL (inpout16.dll).

3. Windows 95 allows direct port reads and writes unless a VxD has control of the port and blocks access. Under Windows NT, direct port access is not allowed, and you must use a kernel-mode device driver.

4. For the latest parallel-port programming and interfacing information and tools, visit Parallel Port Central at:

http://www.lvr.com

***

Inpout32.zip contains the following files:

inpout32.txt
This file

inpout32.dll
A DLL that enables the use of Inp and Out routines in 32-bit Visual Basic 4 and Visual Basic 5 programs.

inpout32.bas
Visual-Basic declarations for Inp and Out

inpout32.vbp
Visual Basic 4 test project for inpout32. The project will also load into and run under Visual Basic 5.

inpout32.frm
Startup form for the test project

inpout32.dpr
Source code for inpout32.dll. The DLL was compiled with Borland's Delphi 2.0 Object Pascal compiler.

***

How to run the test program (inpout32.vbp):
1. Copy inpout32.dll to one of these locations: your default Windows directory (usually \Windows), your Windows system directory (usually \Windows\system), or your application's working directory. In the VB programming environment, the working directory is the default VB directory.
2. Open the project inpout32.vbp.
3. In the Form_Load subroutine, set PortAddress equal to the port address you want to test.
3. Clicking the command button causes the program to do the following: write a value to the port, read the port, and display the result. The value increments with each click, resetting to 0 at 255.

***

How to use inpout32 in your programs:

1. Copy inpout32.dll to your default Windows directory (or other directory as described above).

2. Add inpout32.bas to your Visual-Basic project (File menu, Add File).

3. Use this syntax to write to a port:
Out PortAddress, ValueToWrite

Example:
Out &h378, &h55

Use this syntax to read a port:
ValueRead = Inp(PortAddress)

Example:
ValueRead = Inp(&h378)

(The syntax is identical to QuickBasic's Inp and Out).
14 changes: 14 additions & 0 deletions CommunTools/UseLib/InpOut/INPOUT32.VBP
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Form=inpout32.frm
Module=inpout; Inpout32.bas
ProjWinSize=82,446,194,128
ProjWinShow=2
IconForm="inpout32"
Name="Project1"
HelpContextID="0"
StartMode=0
VersionCompatible32="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0

0 comments on commit 2f175ad

Please sign in to comment.