|
Create Form
We are going to design with using VB Control which name is MSComm
You should be defind in Custom Control which go to menu Project--->Companents
After that select item name is MSComm that will be show a yellow telephone icon. Drag the icon from tool dialog and drop it to form of your project.
After that drag and drop Frame control for Communication and Control I/O Microcontroller part
Regrading arrange all control , I had have put explanation with in picture
Picture 1 : Form designed
Function programming of this program
First listing is command button for Setting Communication PC Serial Port
Private Sub Command8_Click()
On Error GoTo Errlabel
' Defind value Setting is ="Baud Rate,Parity(if not using put "N"),Data bit,Stop bit"
Ex. 9600,N,8,1
MSComm1.Settings = Text1.Text '
As we are going to put value Setting in Textbox
MSComm1.CommPort = Combo1.ListIndex + 1
' To defind Com port
MSComm1.RThreshold = 1 'Setted value to use Event-driven when has data coming
MSComm1.PortOpen = True ' Open Com port for availability
MSComm1.InputLen = 0
' Defind to read all data in Buffer of MSComm
Exit Sub
' For error checking of program
Errlabel:
If Err.Number = 8002 Then MsgBox "Select com Port", vbInformation, "8051 Control I/O"
End Sub
Control button set for send value out to 8051 microcontroller
' Command in sending code to microcontroller for control each LED
Private Sub Command1_Click()
MSComm1.Output = "1" ' Send out Number 1 is Ascii =31(Hex) to on LED1
valLED1 = Not valLED1
' Defind value for check pressing button LED1
End Sub
Private Sub Command2_Click()
MSComm1.Output = "2" ' Send out Number 1 is Ascii =32(Hex) to on LED2
valLED2 = Not valLED2 ' Defind value for check pressing button LED2
End Sub
Private Sub Command3_Click()
MSComm1.Output = "3" ' Send out Number 1 is Ascii =33(Hex) to on LED3
valLED3 = Not valLED3
' Defind value for check pressing button LED3
End Sub
Private Sub Command4_Click()
MSComm1.Output = "4" ' Send out Number 1 is Ascii =34(Hex) to on LED4
valLED4 = Not valLED4 ' Defind value for check pressing button LED4
End Sub
Private Sub Command5_Click()
MSComm1.Output = "5"
' Send out Number 1 out is Ascii =35(Hex) to on LED5
valLED5 = Not valLED5
' Defind value for check pressing button LED5
End Sub
Private Sub Command6_Click()
MSComm1.Output = "6" ' Send out Number 1 is Ascii =36(Hex) to on LED6
valLED6 = Not valLED6 ' Defind value for check pressing button LED6
End Sub
Private Sub Command7_Click()
MSComm1.Output = "7"
' Send out Number 1 out is Ascii =37(Hex) to on LED7
valLED7 = Not valLED7
' กำหนดค่าเพื่อตรวจสอบการกดปุ่ม LED7
End Sub
Private Sub Command9_Click()
MSComm1.Output = "0" ' Send out Number 0 is Ascii =30(Hex) to off all LED
valLED = Not valLED ' Defind value for check pressing button ED0
End Sub
Receive value from microcontroller function with in MSComm1_OnComm()
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent ' Occur event Comm-Event
Case comEvReceive ' When it has receive from Com Port
Dim Buffer As Variant ' Variable for save data when receive occurrence
Buffer = MSComm1.Input ' Save data in variable
Label2.Caption = "Return = " & Buffer
' In case when microcontroller send data coming to confirm perform function finished .
' check value that receive ,Is it "C" ? and valLED1-7 of each button
' For change color of circle to red(meaning is ON LED)and If it is white(meaning is off LED)
On Error GoTo Errlabel
If Buffer = "C" Then
If valLED = True Then
For x = 0 To 7 Step 1
Shape1(x).FillColor = &HFFFFFF
Next
Shape1(0).FillColor = &HFF&
valLED1 = False
valLED2 = False
valLED3 = False
valLED4 = False
valLED5 = False
valLED6 = False
valLED7 = False
Command9.Caption = "ON ALL"
' If button remain "ON ALL" ,It can not control up to press button to "OFF ALL"
Exit Sub
Else
Shape1(0).FillColor = &HFFFFFF
Command9.Caption = "OFF ALL"
End If
If valLED1 = True Then
Shape1(1).FillColor = &HFF&
Else
Shape1(1).FillColor = &HFFFFFF
End If
If valLED2 = True Then
Shape1(2).FillColor = &HFF&
Else
Shape1(2).FillColor = &HFFFFFF
End If
If valLED3 = True Then
Shape1(3).FillColor = &HFF&
Else
Shape1(3).FillColor = &HFFFFFF
End If
If valLED4 = True Then
Shape1(4).FillColor = &HFF&
Else
Shape1(4).FillColor = &HFFFFFF
End If
If valLED5 = True Then
Shape1(5).FillColor = &HFF&
Else
Shape1(5).FillColor = &HFFFFFF
End If
If valLED6 = True Then
Shape1(6).FillColor = &HFF&
Else
Shape1(6).FillColor = &HFFFFFF
End If
If valLED7 = True Then
Shape1(7).FillColor = &HFF&
Else
Shape1(7).FillColor = &HFFFFFF
End If
End If
End Select
Errlabel:
Exit Sub
End Sub
|