| Although VB6 is no longer used, these examples are included because it is simple to learn. In particular, we provide detailed guides on how to use and program hardware components such as parallel ports (LPT) and serial ports (COM).Below you can find a simple Visual Basic code example that checks the availability of serial and parallel ports and reports the number of available ports. Functions to find out whether parallel and serial ports exist. If the return values are True, the port is available. |
Function to Find Serial Port
Function IsComPortAvailable(ByVal portNum As Integer) As Boolean
Dim fnum As Integer
On Error GoTo ErrorHandler
fnum = FreeFile
Open "COM" & CStr(portNum) For Binary Shared As #fnum
Close #fnum
IsComPortAvailable = True
Exit Function
ErrorHandler:
If Err.Number <> 0 Then
IsComPortAvailable = False
Err.Clear
End If
End Function
Function to find parallel port
Function IsLptPortAvailable(ByVal portNum As Integer) As Boolean
' Dosya numarasını tutacak bir değişken tanımla
Dim fnum As Integer
' Çalışma zamanı hatalarını bastır
On Error Resume Next
' Boş bir dosya numarası al
fnum = FreeFile
' Belirtilen LPT portunu ikili paylaşımlı erişim için açmayı dene
Open "LPT" & CStr(portNum) For Binary Shared As #fnum
' Portu açarken bir hata olup olmadığını kontrol et
If Err = 0 Then
' Port başarıyla açıldıysa, portu kapat
Close #fnum
' Portun kullanılabilir olduğunu belirten dönüş değerini True yap
IsLptPortAvailable = True
End If
' Hata varsa, fonksiyon varsayılan olarak False dönecek
End Function
We must call the above functions to find the number of ports.
It is not mandatory to use these functions.
For Serial Ports
Dim count As Integer, i As Integer
' 1'den 16'ya kadar olan port numaraları için döngü başlat
For i = 1 To 16
' Eğer mevcut bir COM portu varsa, sayacı artır
If IsComPortAvailable(i) Then count = count + 1
Next
' Bulunan COM port sayısını mesaj kutusunda göster
MsgBox count & " adet seri port bulundu"
For Parallel Ports
Dim count As Integer, i As Integer
' 1'den 16'ya kadar olan port numaraları için döngü başlat
For i = 1 To 16
' Eğer mevcut bir LPT portu varsa, sayacı artır
If IsLptPortAvailable(i) Then count = count + 1
Next
' Bulunan LPT port sayısını mesaj kutusunda göster
MsgBox count & " adet paralel port bulundu"
For operation on parallel or serial ports.
‘Once the current Port number is learned, it is opened using this function.
The fnum variable is the port number known to be open.Here, only the necessary part should be changed for LPT.
For COM, only the necessary part should be changed.
Public Sub OpenPort(ByVal PortNum As Integer)
' Dosya numarasını tutmak için bir değişken tanımla
Dim fnum As Integer
' Boş bir dosya numarası al
fnum = FreeFile
' Belirtilen LPT bağlantı noktasını çıktı modu ile aç
Open "LPT" & CStr(PortNum) For Output As #fnum
' Dosya numarasını global veya modül seviyesindeki bir değişkene ata
FileNumber = fnum
End Sub
‘The string variable is sent to the opened port with this function.
Public Sub SendToPort(ByVal PortNum As Integer, ByVal Data As Byte)
' Belirtilen dosya numarasına (LPT portu) veriyi yaz
Write #CStr(FileNumber), Data
End Sub
Public Sub ClosePort(ByVal PortNum As Integer)
Close #CStr(PortNum)'İşimiz bittiğinde portun kapatılması gereklidir.
End Sub