Programming The Parallel Port In Visual Basic
http://www.aaroncake.net/electronics/vblpt.htmParallel port monitors
http://neil.fraser.name/software/lpt/get Inpout32.dll from
http://www.logix4u.cjb.net/
http://www.aaroncake.net/electronics/vblpt.htmParallel port monitors
http://neil.fraser.name/software/lpt/get Inpout32.dll from
http://www.logix4u.cjb.net/
解决方案 »
- vb怎么读取txt文件
- 求教VSFlexGrid中的FormatString属性的具体用法.
- 新手遇到 MSHFlexGrid1 两个简单问题
- 为何我用GetCursorPos得到的鼠标位置和计算的鼠标位置有这么大的差距!!!
- 怎样编程动态生成一个命令按钮?不想在设计窗体时就设计好它。
- 那位老大给我说一说Microsoft DAO 3。51 object library与 Microsoft DAO 6。0 object library 之间有什么不同
- 那里能找到sql2000个人版(无内容)?
- 来者有分,小弟的诗歌,呵呵
- [求助]怎么实现Office XP的菜单风格?
- vb串行通信
- 如何用VB编写一个在win2000下隐身的程序?
- 一个控件顺序的问题,大家可能都碰到过!
Public Declare Function DlPortReadPortUshort Lib "dlportio.dll" (ByVal Port As Long) As Integer
Public Declare Function DlPortReadPortUlong Lib "dlportio.dll" (ByVal Port As Long) As LongPublic Declare Sub DlPortReadPortBufferUchar Lib "dlportio.dll" (ByVal Port As Long, Buffer As Any, ByVal Count As Long)
Public Declare Sub DlPortReadPortBufferUshort Lib "dlportio.dll" (ByVal Port As Long, Buffer As Any, ByVal Count As Long)
Public Declare Sub DlPortReadPortBufferUlong Lib "dlportio.dll" (ByVal Port As Long, Buffer As Any, ByVal Count As Long)Public Declare Sub DlPortWritePortUchar Lib "dlportio.dll" (ByVal Port As Long, ByVal Value As Byte)
Public Declare Sub DlPortWritePortUshort Lib "dlportio.dll" (ByVal Port As Long, ByVal Value As Integer)
Public Declare Sub DlPortWritePortUlong Lib "dlportio.dll" (ByVal Port As Long, ByVal Value As Long)Public Declare Sub DlPortWritePortBufferUchar Lib "dlportio.dll" (ByVal Port As Long, Buffer As Any, ByVal Count As Long)
Public Declare Sub DlPortWritePortBufferUshort Lib "dlportio.dll" (ByVal Port As Long, Buffer As Any, ByVal Count As Long)
Public Declare Sub DlPortWritePortBufferUlong Lib "dlportio.dll" (ByVal Port As Long, Buffer As Any, ByVal Count As Long)Function BinToDec%(BinNumber$)
Dim Weight%
Dim Dec%
Dim i%
Weight% = 1
Dec% = 0 'Reset decimal number If BinNumber$ <> "00000000" Then
For i% = Len(BinNumber$) To 1 Step -1
If Mid$(BinNumber$, i%, 1) = "1" Then
Dec% = Dec% + Weight% 'If bit=1 then add weigth factor
End If
Weight% = Weight% * 2 'Multiply weight factor by 2
Next
BinToDec% = Dec% 'Store result
Else
BinToDec% = 0
End If
End FunctionFunction DecToBin$(Decnumber%)
'Conversion of decimal number (0...255) to 8 bit binary string.
'--------------------------------------------------------------
Dim Bin$
Dim Faktor%, i% Bin$ = ""
Faktor% = 128 If Decnumber% <> 0 Then
For i% = 1 To 8
If Faktor% > Decnumber% Then
Bin$ = Bin$ + "0"
Else
Bin$ = Bin$ + "1"
Decnumber% = Decnumber% - Faktor%
End If
Faktor% = Faktor% \ 2
Next
DecToBin$ = Bin$
Else
DecToBin$ = "00000000"
End If
End Function
Private Sub Command1_Click()
Value% = DlPortReadPortUchar(&H379)
Text2.Text = Value%
Text3.Text = "&H" + Hex(Value%)
Text4.Text = DecToBin$(Value%)
End SubPrivate Sub Command2_Click()
DlPortWritePortUchar &H378, Val(Text5.Text)
End SubPrivate Sub Text1_Change()
Text5.Text = BinToDec(Val(Text1.Text))
End Sub
Next we will discuss programming the parallel port. Attached is a sample program that allows you to turn on one of the data pins at a time. This would be used to light up a LED that was attached to that pin. However, I will walk you through the process of making that program right now. Ok, as I stated above, Visual Basic doesn’t provide any direct access to the parallel port. So, we will have to declare the functions that we will need to use from the INPOUT32.dll. Here are the declarations that I placed into the Declarations section of Form1:Private Declare Function Inp Lib "inpout32.dll" _
Alias "Inp32" (ByVal PortAddress As Integer) As IntegerPrivate Declare Sub Out Lib "inpout32.dll" _
Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)These declarations allow us to read data from the parallel port and also to write data to the parallel port. We would use Inp to read and Out to write. For anyone familiar with QBASIC, you will recognize these as the commands used to read and write to the parallel port. In these declarations, PortAddress is the hexidecimal address for the parallel port and Value is the value that you want to write to the parallel port. Next, I declared a variable to hold the value of the port address so that we don’t have to put it in every time: Dim PortAddress As StringNext, we place code into the Form_Load Sub: PortAddress = &H378This is the default HEX address to the parallel port. This might be different on your computer so you would go to the device manager and click on the Resources tab under your parallel port.Next, I added 9 command buttons to my form and labeled the first 8 of them Pin 1, Pin 2, etc. The last one is labeled Off and is used to turn off all of the lights. Here is the code for the command buttons:Private Sub Command1_Click()
Out PortAddress, 1
End SubPrivate Sub Command2_Click()
Out PortAddress, 2
End SubPrivate Sub Command3_Click()
Out PortAddress, 4
End SubPrivate Sub Command4_Click()
Out PortAddress, 8
End SubPrivate Sub Command5_Click()
Out PortAddress, 16
End SubPrivate Sub Command6_Click()
Out PortAddress, 32
End SubPrivate Sub Command7_Click()
Out PortAddress, 64
End SubPrivate Sub Command8_Click()
Out PortAddress, 128
End SubPrivate Sub Command9_Click()
Out PortAddress, 0
End SubAs you can see, the code for command buttons 1 through 8 is almost identical except for the numbers. Those numbers simply state which of the data ports to send the data to. You might think that you would put in 3 to output to data port 3 and 6 to output to data port 6. However, if you were to put in 3 it would output to data ports 2 and 3, and if you were to put in 6 it would output to data ports 3 and 4. This is because the data ports on the parallel port go by powers of 2. The first port is 1, the second is 2, the third is 4 and so on. All of the numbers in between are used to light up different combinations of the pins. All numbers between say 8 and 16 will light up all of the lights on the first data port through the sixth data port. I will leave you to figure out those combinations though. I will, however, tell you that the values for the data ports go from 1 to 255. If you put in a number above 255 you will get an overflow error. The code for command9 sends to port 0. This will turn off all of the data ports and will send the data through pin 1 on the parallel port.As you can see, this is only a very basic tutorial on using the parallel port. Using this code you would be able to turn off any device that will run off of +5 volts DC. So, you would be able to run motors or basically any other device that uses 5 volts DC. If you know enough about electronics you could even make a device to turn on and off lights or other electrical appliances in your home. I will be writing more advanced tutorials on the parallel port at a later date but I think that this should get you started with programming the parallel port in Visual Bas