Hvordan få tilgang Serial & Parallel porter ved hjelp av Visual Basic 6

Hvis du utvikler et program ved hjelp av Visual Basic 6, og du vil være i stand til å få tilgang til de serielle og parallelle porter, må du sette inn konkrete Visual Basic programmering koder inn i programmet er ".VB" fil. Du kan redigere denne filen direkte i Visual Basic programmeringsmiljø, men koden for å legge tilgang til serie- og parallellporter er veldig lang og konkret.

Bruksanvisning

1 Dobbeltklikk på "Microsoft Visual Studio .NET" program ikonet for å starte programmet. Klikk på "File" -menyen, beveger musepekeren over "Ny" og velg "Project" alternativet.

2 Klikk på "Visual Basic Projects" alternativet under "prosjekttyper" overskriften. Klikk på "Console Application" alternativet under "Maler" overskriften.

3 Skriv inn et navn for programmet i feltet og klikk på "OK" -knappen for å opprette prosjektet. Den "Module1.vb" filen åpnes automatisk.

4 Lim inn følgende kode i prosjektets "Module1.vb" fil før linjen med kode som leser "Module module1":

Option Strict On

'Definer en CommException klasse som arver fra ApplicationException klassen,
'Og deretter kaste et objekt av typen CommException når du får en feilmelding.
klasse CommException
arver ApplicationException
Sub New (ByVal Reason As String)

MyBase.New(Reason)

End Sub
End Class

5 Lim inn følgende kode i prosjektets "Module1.vb" fil etter linje med kode som leser "Module module1":

«Erklær strukturer.
Offentlig Struktur DCB
Offentlig DCBlength Som Int32
Offentlig baudrate Som Int32
Offentlige fBitFields Som Int32 'Se kommentarer i Win32API.Txt
Offentlig wReserved Som int16
Offentlig XonLim Som int16
Offentlig XoffLim Som int16
Offentlig ByteSize Som Byte
Offentlig Paritet Som Byte
Offentlige Stopbits As Byte
Offentlig XonChar Som Byte
Offentlig XoffChar Som Byte
Offentlig ErrorChar Som Byte
Offentlig EofChar Som Byte
Offentlig EvtChar Som Byte
Offentlig wReserved1 Som int16 'Reserved; Ikke bruk
End Structure

Offentlig Struktur COMMTIMEOUTS
Offentlig ReadIntervalTimeout Som Int32
Offentlig ReadTotalTimeoutMultiplier Som Int32
Offentlig ReadTotalTimeoutConstant Som Int32
Offentlig WriteTotalTimeoutMultiplier Som Int32
Offentlig WriteTotalTimeoutConstant Som Int32
End Structure

"Erklærer konstanter.
Offentlig Const GENERIC_READ Som Int32 = & H80000000
Offentlig Const GENERIC_WRITE Som Int32 = & H40000000
Offentlig Const OPEN_EXISTING Som Int32 = 3
Offentlig Const FILE_ATTRIBUTE_NORMAL Som Int32 = & H80
Offentlig Const NOPARITY Som Int32 = 0
Offentlig Const ONESTOPBIT Som Int32 = 0

"Erklærer referanser til eksterne funksjoner.
Offentlig Erklærer Auto Function Create Lib "kernel32.dll"


(ByVal lpFileName As String, ByVal dwDesiredAccess Som Int32,

ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr) As IntPtr

Offentlig Erklærer Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid Som IntPtr, _
ByRef lpDCB Som DCB) som boolsk

Offentlig Erklærer Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid Som IntPtr, _
ByRef lpDCB Som DCB) som boolsk

Offentlig Erklærer Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile Som IntPtr, _
ByRef lpCommTimeouts Som COMMTIMEOUTS) Som boolsk

Offentlig Erklærer Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile Som IntPtr, _
ByRef lpCommTimeouts Som COMMTIMEOUTS) Som boolsk

Offentlig Erklærer Auto Function Writefile Lib "kernel32.dll" (ByVal hFile Som IntPtr,
ByVal lpBuffer As Byte (), ByVal nNumberOfBytesToWrite Som Int32,

ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean

Offentlig Erklærer Auto Function Readfile Lib "kernel32.dll" (ByVal hFile Som IntPtr,
ByVal lpBuffer As Byte (), ByVal nNumberOfBytesToRead Som Int32,

ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean

Offentlig Erklærer Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean

6 Lim inn følgende kode i prosjektets "Module1.vb" fil etter linje med kode som leser "Sub Main":

"Erklærer de lokale variabler som du vil bruke i koden.
Dim hSerialPort, hParallelPort Som IntPtr
Dim suksess Som boolsk
Dim MyDCB Som DCB
Dim MyCommTimeouts Som COMMTIMEOUTS
Dim BytesWritten, BytesRead Som Int32
Dim Buffer () As Byte

"Erklærer variablene som skal brukes for koding.
Dim oEncoder som ny System.Text.ASCIIEncoding
Dim oEnc Som System.Text.Encoding = oEncoder.GetEncoding (1252)

"Konverter String til byte ().
Buffer = oEnc.GetBytes ( "Test")

Try
' Access the serial port.
Console.WriteLine("Accessing the COM1 serial port")
' Obtain a handle to the COM1 serial port.
hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
' Verify that the obtained handle is valid.
If hSerialPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the COM1 port")
End If
' Retrieve the current control settings.
Success = GetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to retrieve the current control settings")
End If
' Modify the properties of the retrieved DCB structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyDCB.BaudRate = 9600
MyDCB.ByteSize = 8
MyDCB.Parity = NOPARITY
MyDCB.StopBits = ONESTOPBIT
' Reconfigure COM1 based on the properties of the modified DCB structure.
Success = SetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure COM1")
End If
' Retrieve the current time-out settings.
Success = GetCommTimeouts(hSerialPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to retrieve current time-out settings")
End If
' Modify the properties of the retrieved COMMTIMEOUTS structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyCommTimeouts.ReadIntervalTimeout = 0
MyCommTimeouts.ReadTotalTimeoutConstant = 0
MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
MyCommTimeouts.WriteTotalTimeoutConstant = 0
MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
' Reconfigure the time-out settings, based on the properties of the modified COMMTIMEOUTS structure.
Success = SetCommTimeouts(hSerialPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to reconfigure the time-out settings")
End If
' Write data to COM1.
Console.WriteLine("Writing the following data to COM1: Test")
Success = WriteFile(hSerialPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to write to COM1")
End If
' Read data from COM1.
Success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to read from COM1")
End If
Catch ex As Exception
Console.WriteLine(Ex.Message)
Finally
' Release the handle to COM1.
Success = CloseHandle(hSerialPort)
If Success = False Then
Console.WriteLine("Unable to release handle to COM1")
End If
End Try

7 Lim inn følgende kode rett etter koden du satt inn i "Module1.vb" -filen i Trinn 6:

Prøve

' Parallel port.
Console.WriteLine("Accessing the LPT1 parallel port")
' Obtain a handle to the LPT1 parallel port.
hParallelPort = CreateFile("LPT1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
' Verify that the obtained handle is valid.
If hParallelPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the LPT1 port")
End If
' Retrieve the current control settings.
Success = GetCommState(hParallelPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to retrieve the current control settings")
End If
' Modify the properties of the retrieved DCB structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyDCB.BaudRate = 9600
MyDCB.ByteSize = 8
MyDCB.Parity = NOPARITY
MyDCB.StopBits = ONESTOPBIT
' Reconfigure LPT1 based on the properties of the modified DCB structure.
Success = SetCommState(hParallelPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure LPT1")
End If
' Retrieve the current time-out settings.
Success = GetCommTimeouts(hParallelPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to retrieve current time-out settings")
End If
' Modify the properties of the retrieved COMMTIMEOUTS structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyCommTimeouts.ReadIntervalTimeout = 0
MyCommTimeouts.ReadTotalTimeoutConstant = 0
MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
MyCommTimeouts.WriteTotalTimeoutConstant = 0
MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
' Reconfigure the time-out settings, based on the properties of the modified COMMTIMEOUTS structure.
Success = SetCommTimeouts(hParallelPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to reconfigure the time-out settings")
End If
' Write data to LPT1.
' Note: You cannot read data from a parallel port by calling the ReadFile function.
Console.WriteLine("Writing the following data to LPT1: Test")
Success = WriteFile(hParallelPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to write to LPT1")
End If
Catch ex As Exception
Console.WriteLine(Ex.Message)
Finally
' Release the handle to LPT1.
Success = CloseHandle(hParallelPort)
If Success = False Then
Console.WriteLine("Unable to release handle to LPT1")
End If
End Try

Console.WriteLine (& quot; Press ENTER for å avslutte & quot;)
Console.ReadLine ()

8 Klikk på "Build" -menyen og velg "Build Solution" alternativet. Klikk på "Debug" -menyen og velg "Start" alternativet. Søknaden har nå tilgang til serielle og parallelle porter.