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/

解决方案 »

  1.   

    ublic Declare Function DlPortReadPortUchar Lib "dlportio.dll" (ByVal Port As Long) As Byte
    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
      

  2.   

    Part 1First off.  This will not work in Windows NT/2000/XP without some extra work.  This is because these operating systems have security issues with programs directly accessing ports.  I found this link: http://www.zeecube.com/IOAccess/index.htm that has some info on how to get around this but you may need to do some more searching.  That said, since Visual Basic doesn’t provide direct access to the parallel port we will have to use an external control or DLL.  I am using the INPOUT32.DLL for this.  I chose this since a post on PSC yesterday had some info on programming in the parallel port but no code and they said to use this one.  So that’s why I’m using it. To get the full effect of this it is helpful to have a small amount of electronics knowledge.  You could do this simply by sticking the positive side of a LED into pins 2-9 on your parallel port and the negative side of the LED into pins 18-25 if you don’t have much electronics knowledge.  However, below is the schematic for a simple array of LED’s that I made up last night.  You could build this very easily if you know how to solder or you could even go to the local Radio Shack and buy a breadboard, some wire, and 8 LED’s. With this circuit you would simply run a wire from the positive end of all of the LED’s to one of the pins on the parallel port, pin 2-9, and then the other end of the LED to pin 18-25 of the parallel port.  All of the ground wires can be put into 1 of the parallel port’s ground pins, not all 8 individual wires but 1 wire that they all hook up to. The diagram below shows the parallel port and what all of the pins do.  For our current purposes, we only need pins 2-9 and pins 18-25. 
    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
      

  3.   

    去问专家!!他会解答你的  http://www.vbio.com.tw/