Kali ini saya akan menjelaskan tentang bagaimana cara membuat aplikasi dari visual basic 6 yang berfungsi untuk menampilkan data dari input atau serial, data yang bisa bersumber dari sensor kemudian dikirim secara serial melalui microcontroller, atau juga bisa yang lainnya, pada aplikasi ini akan menampilkan data sebanyak 3 buah dengan 2 angka dibelakang koma, sebenarnya bisa dirubah sesuai keinginan, misal jika ingin menampilkan 8 buah data atau 5 buah data juga bisa, hanya perlu merubah nilai Mscomm.RThreshold dan Mscomm.inputlen. berikut adalah penjelasan programnya, untuk memiliki aplikasi ini dapat didownload di link berikut.
a. Tampilan Program
b. Program VB 6
Option Explicit
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 10 '//untuk data panjang, isikan 100
Private Sub berhenti_Click()
start.Enabled = True
berhenti.Enabled = False
TimerBaca.Enabled = False
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
ComboCOM.AddItem (i)
Next i
End Sub
Private Sub MSComm2_OnComm()
Dim buffer As String
Dim temp As String
Dim pisah() As String
Dim i As Integer
If MSComm2.CommEvent = comEvReceive Then
buffer = MSComm2.Input
pisah = Split(buffer, Chr$(13))
On Error Resume Next
If buffer <> " " Then
With Text1
.SelStart = Len(.Text)
.SelText = buffer
End With
For i = 0 To 3 '//untuk 8 tampilan, isikan 0 to 7
If 0 < 4 Then '//untuk 8 tampilan isikan 0<8
Text2(i).Text = CStr(pisah(i))
End If
Next i
Else
buffer = " "
End If
End If
End Sub
Private Sub start_Click()
Dim u As Integer
On Error GoTo ada_eror
MSComm2.CommPort = ComboCOM.Text
MSComm2.Settings = "9600,N,8,1"
MSComm2.RThreshold = 15 '//untuk 8 tampilan, isikan 47
MSComm2.InputLen = 15 '//untuk 8 tampilan, isikan 47
MSComm2.InputMode = comInputModeText
MSComm2.PortOpen = True ' buka port
ReDim arrdata(1 To MAKSBACA + 1)
For u = 1 To MAKSBACA
arrdata(u) = 0
Next
TotalBaca = 0
start.Enabled = False
berhenti.Enabled = True
TimerBaca.Enabled = True
ada_eror:
If ComboCOM.ListIndex < 1 Then
MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
start.Enabled = True
berhenti.Enabled = True
ComboCOM.Text = "Pilih COM"
End If
End Sub
Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
strInput = MSComm2.Input
strPotong = strInput
'Text1.Text = Text1.Text + strPotong + vbCrLf
'singleInput = Val(strPotong)
'Label1.Caption = singleInput
If TotalBaca >= MAKSBACA Then
TimerBaca.Enabled = False
If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
Call berhenti_Click
End If
End Sub
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 10 '//untuk data panjang, isikan 100
Private Sub berhenti_Click()
start.Enabled = True
berhenti.Enabled = False
TimerBaca.Enabled = False
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
ComboCOM.AddItem (i)
Next i
End Sub
Private Sub MSComm2_OnComm()
Dim buffer As String
Dim temp As String
Dim pisah() As String
Dim i As Integer
If MSComm2.CommEvent = comEvReceive Then
buffer = MSComm2.Input
pisah = Split(buffer, Chr$(13))
On Error Resume Next
If buffer <> " " Then
With Text1
.SelStart = Len(.Text)
.SelText = buffer
End With
For i = 0 To 3 '//untuk 8 tampilan, isikan 0 to 7
If 0 < 4 Then '//untuk 8 tampilan isikan 0<8
Text2(i).Text = CStr(pisah(i))
End If
Next i
Else
buffer = " "
End If
End If
End Sub
Private Sub start_Click()
Dim u As Integer
On Error GoTo ada_eror
MSComm2.CommPort = ComboCOM.Text
MSComm2.Settings = "9600,N,8,1"
MSComm2.RThreshold = 15 '//untuk 8 tampilan, isikan 47
MSComm2.InputLen = 15 '//untuk 8 tampilan, isikan 47
MSComm2.InputMode = comInputModeText
MSComm2.PortOpen = True ' buka port
ReDim arrdata(1 To MAKSBACA + 1)
For u = 1 To MAKSBACA
arrdata(u) = 0
Next
TotalBaca = 0
start.Enabled = False
berhenti.Enabled = True
TimerBaca.Enabled = True
ada_eror:
If ComboCOM.ListIndex < 1 Then
MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
start.Enabled = True
berhenti.Enabled = True
ComboCOM.Text = "Pilih COM"
End If
End Sub
Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
strInput = MSComm2.Input
strPotong = strInput
'Text1.Text = Text1.Text + strPotong + vbCrLf
'singleInput = Val(strPotong)
'Label1.Caption = singleInput
If TotalBaca >= MAKSBACA Then
TimerBaca.Enabled = False
If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
Call berhenti_Click
End If
End Sub
untuk program di mikronya gimana mas?
ReplyDeleteKasih penjelasan tiap sub programnya gan
ReplyDelete