![]() |
|
โครงงานการเขียนโปรแกรมติดต่อกับ Driver Stepper Moter |
สามารถดาว์ลโหลด Project Source Code ได้ที่ส่วนของ Download ซึ่งมี source code อื่นๆ อีกมากมาย
รูปโปรแกรมและการจัดวาง control ต่างๆ

รูปที่ 1 การจัดว่างตำแหน่งคอนโทรลต่างๆในโปรแกรม

รูปที่ 2 รายละเอียดการสร้างเมนูบาร์
รูปที่ 3 การตั้งค่าพารามิเตอร์เพื่อติดต่อกับ Comm Port ซึ่งเราอาจใส่ค่าที่นี้เลยก็ได้

รูปที่ 4 การตั้งค่า Interval สำหรับ Timer ใช้โพลลิ่งกับ Comm Port
| Programming Code พร้อมคำอธิบายแต่และเหตุการณ์(Event) |
| Private Sub Command1_Click() ' ส่งค่า Character "+" และค่าตัวเลขเพื่อกำหนดจำนวนสแต็ปของมอเตอร์ MSComm1.Output = "+" & Text8.Text End Sub |
| Private Sub Command2_Click() ' ส่งค่า Character "-" และค่าตัวเลขเพื่อกำหนดจำนวนสแต็ปของมอเตอร์ MSComm1.Output = "-" & Text8.Text End Sub |
| Private Sub Command3_Click() ' ส่งค่า Character "M" และค่าตัวเลขเพื่อกำหนดจำนวนสแต็ปของมอเตอร์ที่จะ Move MSComm1.Output = "M" & Text1.Text End Sub |
| Private Sub Command4_Click() ' ส่งค่า Character "F" และค่าตัวเลขเพื่อกำหนดจำนวนสแต็ปของมอเตอร์ในการหาโฮม MSComm1.Output = "F" & Text2.Text End Sub |
| Private Sub Command5_Click() Call SettingValue ' เรียกซับรูทีนตั้งค่าดีฟอร์ท Form1.Height = 3030 Frame1.Visible = False Frame2.Visible = False End Sub |
| Private Sub Command6_Click() ' ให้ค่า Default กับ Text และCombo ต่างๆ เพื่อการติดต่อ Comm port และ Driver Text3.Text = "10" Text4.Text = "10" Text5.Text = "20" Text7.Text = "20" Combo1.Text = "0" Combo2.Text = "Com1" Text6(0).Text = "9600" Text6(1).Text = "N" Text6(2).Text = "8" Text6(3).Text = "1" End Sub |
| Private Sub
Form_Activate() ' ป้ายเตือนให้ตั้งค่าต่างๆก่อนใช้โปรแกรม MsgBox "คุณต้องตั้งค่า ของ Comm Port และ ค่า Defaulte Driver IM483IE เสียก่อนครับ", vbInformation, "คำเตือนก่อนใช้โปรแกรม" End Sub |
| Private Sub Form_Load() Form1.Height = 7650 End Sub |
| Private Sub
mnuCom_Click() Frame1.Visible = True Frame2.Visible = True Form1.Height = 7650 End Sub |
| Private Sub
mnuDrive_Click() Frame1.Visible = True Frame2.Visible = False Form1.Height = 5790 End Sub |
| Private Sub
mnuExit_Click() Unload Me End Sub |
| Private Sub Timer1_Timer() MSComm1.Output = "z 0" ' ส่งค่า Charecter ไปถาม Driver IM483IE แบบโพลลิ่ง End Sub |
| Private Function GetInputData(ByVal SndData As String)
' ในการที่เราค่ามาจาก Driver IM483IE นั้นก็คือรับค่าที่ส่งมาจากไมโครคอนโทรลเลอร์นั้นเอง ดังนั้นบ้างครั้งค่าที่มาจะมีลักษณะเป็นตัวอักขระขยะ จึงต้องมีวิธีที่จะสกัดเอาแต่ค่าที่เราต้องการจริงๆเท่านั้น Dim VbspLen As Integer, VbspInd As Integer |
| Public Sub
SettingValue() ' ตั้งค่าสำหรับ Comm Port ติดต่อกับ Driver IM483IE MSComm1.RThreshold = 1 MSComm1.Settings = Text6(0).Text & "," & Text6(1).Text & _ "," & Text6(2).Text & Text6(3).Text MSComm1.CommPort = Right(Combo2.Text, 1) MSComm1.PortOpen = True MSComm1.PortOpen = True MSComm1.Output = "K" & Text3.Text & Space(1) & Text4.Text & Space(1) & _ "I" & Text5.Text & Space(1) & "V" & Text7.Text & Space(1) & _ "D" & Combo1.Text End Sub |
รูปที่ 5 เมื่อรันโปรแกรมขึ้นมาจะแสดงคำเตือนให้ตั้งค่าการ Interface Hardware ก่อน

รูปที่ 6 หน้าตาโปรแกรมเพื่อที่จะกำหนดใส่ค่า Interface Hardware
รูปที่ 7 เมื่อกดปุ่ม Done แล้วจะมีหน้าต่างดังรูปแต่ถ้าต้องการตั้งค่าใหม่ให้คลิกเมนูบาร์ด้านบนแล้วเลือก Setting Defaulte
| Copyright (c)2000-2002 ThaiIO.com All rights reserved. |