BÀI 1:
Lập trình trên VB giao tiếp với cổng COM sử dụng MSComm6.0
Truyền – Nhận.
1. Giao diện điều khiển trên VB.
2. Code lệnh.
Private Sub Cmd_truyen_Click()
MSComm1.Output = txttruyen.Text
End Sub
Private Sub Cmd_nhan_Click()
txtnhan = MSComm1.Input
End Sub
Private Sub Form_Load()
With MSComm1
• Comport = 1
• Input Mode = ComInputMode text
• Settings = “9600,n,8,1”
• Rthreshold = 1
• Portopen = true
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Private Sub MSComm1_Oncomm()
‘txtnhan.text = Asc(MSComm1.Input)’
End Sud
Private Sub txttruyen_Change()
End Sub
Private Sub txtnhan_Change()
End Sub
3. Giao diện khi chạy chương trình.
BÀI 2:
Lập trình trên VB giao tiếp với cổng LPTCOM sử dụng thư viện liên kết
động DLL( io.dll hoặc Port.dll).
1. Giao diện điều khiển trên VB.
2. Code lệnh.
Private Declare Function OPENCOM Lib "Port" (ByVal A$) As Integer
Private Declare Sub CLOSECOM Lib "Port" ()
Private Declare Sub SENDBYTE Lib "Port" (ByVal b%)
Private Declare Function READBYTE Lib "Port" () As Integer
Private Declare Sub DTR Lib "Port" (ByVal b%)
Private Declare Sub RTS Lib "Port" (ByVal b%)
Private Declare Sub TXD Lib "Port" (ByVal b%)
Private Declare Function CTS Lib "Port" () As Integer
Private Declare Function DSR Lib "Port" () As Integer
Private Declare Function RI Lib "Port" () As Integer
Private Declare Function DCD Lib "Port" () As Integer
Private Declare Sub DELAY Lib "Port" (ByVal b%)
Private Declare Sub TIMEINIT Lib "Port" ()
Private Declare Sub TIMEINITUS Lib "Port" ()
Private Declare Function TIMEREAD Lib "Port" () As Long
Private Declare Function TIMEREADUS Lib "Port" () As Long
Private Declare Sub DELAYUS Lib "Port" (ByVal l As Long)
Private Declare Sub REALTIME Lib "Port" (ByVal i As Boolean)
Private Declare Sub OUTPORT Lib "Port" (ByVal A%, ByVal b%)
Private Declare Function INPORT Lib "Port" (ByVal p%) As Integer
Private Declare Function JOYX Lib "Port" () As Long
Private Declare Function JOYY Lib "Port" () As Long
Private Declare Function JOYZ Lib "Port" () As Long
Private Declare Function JOYW Lib "Port" () As Long
Private Declare Function JOYBUTTON Lib "Port" () As Integer
Private Declare Function SOUNDSETRATE Lib "Port" (ByVal Rate%) As
Integer
Private Declare Function SOUNDGETRATE Lib "Port" () As Integer
Private Declare Function SOUNDBUSY Lib "Port" () As Boolean
Private Declare Function SOUNDIS Lib "Port" () As Boolean
Private Declare Sub SOUNDIN Lib "Port" (ByVal Buffer$, ByVal Size%)
Private Declare Sub SOUNDOUT Lib "Port" (ByVal Buffer$, ByVal Size%)
Private Declare Function SOUNDGETBYTES Lib "Port" () As Integer
Private Declare Function SOUNDSETBYTES Lib "Port" (ByVal b%) As
Integer
Private Declare Sub SOUNDCAPIN Lib "Port" ()
Private Declare Sub SOUNDCAPOUT Lib "Port" ()
Option Explicit
'TGDK BIT 7 6 5 4 3 2 1 0
' -G SEL ALE2 ALE1
'BIT2=0=SEL : DOC 4 BIT THAP CUA 1 BYTE
'BIT2=1=SEL : DOC 4 BIT CAO CUA 1 BYTE
'BIT3=0 :CHO PHEP IC74245 SO 1
'BIT3=1 :CHO PHEP IC74245 SO 2
'***********************************
Private Sub cmddoc1_Click()
Dim xl As Byte, xh As Byte, x1 As Byte
OUTPORT &H37A, &H0 'chon byte thap,chon sel=0,doc 4 bit thap
xl = INPORT(&H379)
xl = xl \ 16
xl = xl And &HF
OUTPORT &H37A, &H4 'chon byte thap,chon sel=1,doc 4 bit cao
xh = INPORT(&H379)
xh = xh And &HF0
x1 = xl Or xh
txtdoc1 = xl & " " & xh & " " & " " & x1
End Sub
'***********************************
Private Sub cmddoc2_Click()
Dim xl As Byte, xh As Byte, x2 As Byte
OUTPORT &H37A, &H8 'chon byte cao,chon sel=0,doc 4 bit cao
xl = INPORT(&H379)
xl = xl \ 16
xl = xl And &HF
OUTPORT &H37A, &HC 'chon byte cao,chon sel=1,doc 4 bit cao
xh = INPORT(&H379)
xh = xh And &HF0
x2 = xl Or xh
txtdoc2 = xl & " " & xh & " " & " " & x2
End Sub
'***********************************
Private Sub cmdtat1_Click()
'xuat 8 bit thap bang 0: Tat 8 led
OUTPORT &H378, 0 'XUAT SO 00H RA THANH GHI DU LIEU
'CHOT DU LIEU:BIT 0(TGDK) DKHIEN CHAN CHOT CUA IC74373 SO
1
OUTPORT &H37A, 1 'SET BIT 0 CUA THANH GHI DIEU KHIEN
OUTPORT &H37A, 0 'CLR BIT 0 CUA THANH GHI DIEU KHIEN
End Sub
'***********************************
Private Sub cmdtat2_Click()
'xuat 8 bit cao bang 0: Tat 8 led
OUTPORT &H378, 0 'XUAT SO 00H RA THANH GHI DU LIEU
'CHOT DU LIEU:BIT 1(TGDK) DKHIEN CHAN CHOT CUA IC74373 SO
2
OUTPORT &H37A, 2 'SET BIT 1 CUA THANH GHI DIEU KHIEN
OUTPORT &H37A, 0 'CLR BIT 1 CUA THANH GHI DIEU KHIEN
End Sub
'***********************************
Private Sub cmdxuat1_Click()
Dim x As Byte
x = Val(txtxuat1)
'xuat 8 bit thap bang 1: bat 8 led
OUTPORT &H378, x 'XUAT SO FFH RA THANH GHI DU LIEU
'CHOT DU LIEU:BIT 0 DKHIEN CHAN CHOT CUA IC74373 SO 1
OUTPORT &H37A, 1 'SET BIT 0 CUA THANH GHI DIEU KHIEN
OUTPORT &H37A, 0 'CLR BIT 0 CUA THANH GHI DIEU KHIEN
End Sub
'***********************************
Private Sub cmdxuat2_Click()
Dim x As Byte
x = Val(txtxuat2)
'xuat 8 bit cao bang 1: bat 8 led
OUTPORT &H378, x 'XUAT SO FFH RA THANH GHI DU LIEU
'CHOT DU LIEU: BIT 1(TGDK) DKHIEN CHAN CHOT CUA IC74373
SO 2
OUTPORT &H37A, 2 'SET BIT 1 CUA THANH GHI DIEU KHIEN
OUTPORT &H37A, 0 'CLR BIT 1 CUA THANH GHI DIEU KHIEN
End Sub
Private Sub Form_Load()
End Sub
Bài 3 :
Ðiều khiển led 7 vạch chạy từ 0 ÷ 9
Trước tiên ta cần lập bảng trạng thái để xuất dữ liệu ra mà led 7 vạch có thể
hiện từ số 0 đến số 9.
Số Giá trị xuất ra
0 63
1 6
2 91
3 79
4 102
5 109
6 125
7 7
8 127
9 111
Giao diện lập trình:
Chương trình điều khiển :
Dim a(0 To 10) As Integer
Dim i As Integer
Dim r As Boolean
Private Sub Command1_Click()
r = True
End Sub
Private Sub Command2_Click()
r = False
End Sub
Private Sub Command3_Click()
i = 0
r = True
End Sub
Private Sub Form_Load()
a(2) = 91
a(1) = 6
a(3) = 79
a(4) = 102
a(5) = 109
a(6) = 125
a(7) = 7
a(8) = 127
a(9) = 111
a(0) = 63
i = 0
End Sub