0
Tải bản đầy đủ (.pdf) (194 trang)

Chọn chƣơng trình viết giao diện của máy đo áp suất thủy động

Một phần của tài liệu LUẬN VĂN: NGHIÊN CỨU THIẾT KẾ VÀ CHẾ TẠO MÁY ĐO ÁP SUẤT CỦA Ổ ĐỠ THUỶ ĐỘNG CÓ KẾT NỐI MÁY TÍNH. PPTX (Trang 162 -178 )

2. Tính toán và thiết kế các chi tiết bộ phạn của máy BKM-10

4.2. Chọn chƣơng trình viết giao diện của máy đo áp suất thủy động

Một số lý do để chọn Visual Basic làm ngôn ngữ thảo chƣơng trình: - Dễ học

- Thíc hợp cho MS WINDOWS

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 162

a. Hộp công cụ

Hộp công cụ là bảng chứa các điều khiển và ta thiết kế giao diện ngƣời sử dụng bằng cách chọn các điều khiển từ hộp công cụ và đƣa chúng vào các biểu mẫu.

Một số điều khiển có sẵn trong Visual basic và không thể gỡ bỏ khỏi hộp công cụ. Một số khác nằm bên ngoài Visual basicvà chứa trong các tập tin mà có phần mở rộng là. ocx. Các điều khiển này có thể đƣợc thêm vào hoặc gỡ bỏ khỏi thanh công cụ.

Trong chƣơng trình của đề tài cần tập tin mở rộng là NTgraph.ocx điều khiển này cho phép tạo một đồ thị trên form chính. Ngoài ra cần thêm vào thanh công cụ phần Mscomm1. Để sử dụng ta tìm file Ntgraph.ocx trên trang chủ sau đó paste và system32 trên giao diện của VB chọn menu project -> components cửa sổ components hiện ra ta sẽ tìm và chọn các công cụ trên.

Bộ môn: Máy và ma sát học Đồ án tốt nghiệp

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 163

b. Vẽ đồ thị với NTgraph.

Đây là một điều khiển OCX tạo ra một đồ thị 2D với tọa độ X,Y đƣợc tính toán, điều chỉnh nhập vào từ bên ngoài có thể lấy từ tín hiệu qua cổng COM, LPT, Card giao tiếp PC.

Hình 4.4 NTgraph active

Để vẽ đồ thị ta cũng có thể dùng Teechart nhƣng phức tạp hơn mà phải trả phí. Với NTgraph thì giao thức đơn giản và phù hợp với tín hiệu 2 trục mà ta cần trong đề tài này.

Trong NTgraph ta có thể đặt các thuộc tính màu sắc đƣờng nét, trục tọa độ, tạo thêm nhiều đƣờng hiển thị khác…

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 164

Hình 4.5 Bảng lựa chọn chế độ vẽ đồ thị

Ta có thể sử dụng code để áp đặt thuộc tính cho đồ thị: Graph.Caption = “ ” - tiêu đề của đồ thị.

.Xlabel = “ ” – tiêu đề trục X. .Ylabel = “ ” – tiêu đề trục Y.

.PlotAreaColor = (red, green, black, white) - màu nền đồ thị. .XgridNumber - vạch chia trên trục X.

.YgridNumber - vạch chia trên trục Y.

.SetRange(xmin, xmax, ymin, ymax) – đặt dải tọa độ trên các trục.

Bộ môn: Máy và ma sát học Đồ án tốt nghiệp

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 165

.ClearGraph – xóa đồ thị.

Để nhận và vẽ đồ thị từ tín hiệu cảm biến chuột máy tính ta cần khai báo các hàm, lệnh liên kết với windows.

c. Code chƣơng trình giao diện máy đo áp suất

Dim MotorDir As Boolean Dim AD As FIFO

Dim PD As FIFO

Dim InputData As BYTEFIFO Dim NumberStepValue As String Dim Vel As String

Private Sub ExcelSave_Click() 'oXL.oWB.ORng

Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As Excel.Range Vel = "0"

' Start Excel and get Application object.

Set oXL = CreateObject("Excel.Application") oXL.Visible = True

' Get a new workbook.

Set oWB = oXL.Workbooks.Add Set oSheet = oWB.ActiveSheet

' Add table headers going cell by cell. oSheet.Cells(1, 1).Value = "Lan Do" oSheet.Cells(1, 2).Value = "Goc" oSheet.Cells(1, 3).Value = "Ap Suat"

' Format A1:D1 as bold, vertical alignment = center. With oSheet.Range("A1", "C1")

.Font.Bold = True

.VerticalAlignment = xlVAlignCenter End With

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 166

' Create an array to set multiple values at once. Dim size As Integer

Dim i As Integer AD.ResetReadIndex PD.ResetReadIndex i = 2

Do While Not AD.IsEmpty oSheet.Cells(i, 1).Value = i - 1 oSheet.Cells(i, 2).Value = AD.Pop oSheet.Cells(i, 3).Value = PD.Pop i = i + 1

Loop

' Fill A2:B6 with an array of values (First and Last Names). 'oSheet.Range("A2", "C6").Value = Result

Set oRng = oSheet.Range("A1", "C1") oRng.EntireColumn.AutoFit

' Make sure you release object references. Set oRng = Nothing

Set oSheet = Nothing Set oWB = Nothing Set oXL = Nothing

Exit Sub Err_Handler:

MsgBox Err.Description, vbCritical, "Error: " & Err.Number End Sub

Private Sub CandyButton1_Click() Unload Me

End End Sub

Private Sub ClearCmd_Click() CmdText.Text = ""

Bộ môn: Máy và ma sát học Đồ án tốt nghiệp

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 167

Private Sub Excel_Click() 'oXL.oWB.ORng

Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As Excel.Range

' Start Excel and get Application object.

Set oXL = CreateObject("Excel.Application") oXL.Visible = True

' Get a new workbook.

Set oWB = oXL.Workbooks.Add Set oSheet = oWB.ActiveSheet

' Add table headers going cell by cell. oSheet.Cells(1, 1).Value = "Lan Do" oSheet.Cells(1, 2).Value = "Goc" oSheet.Cells(1, 3).Value = "Ap Suat"

' Format A1:D1 as bold, vertical alignment = center. With oSheet.Range("A1", "C1") .Font.Bold = True .VerticalAlignment = xlVAlignCenter End With Dim i As Integer AD.ResetReadIndex PD.ResetReadIndex i = 2

Do While Not AD.IsEmpty oSheet.Cells(i, 1).Value = i - 1

oSheet.Cells(i, 2).Value = AD.Pop * 0.9 oSheet.Cells(i, 3).Value = PD.Pop * 25 / 1023 i = i + 1

Loop

' Fill A2:B6 with an array of values (First and Last Names). 'oSheet.Range("A2", "C6").Value = Result

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 168

Set oRng = oSheet.Range("A1", "C1") oRng.EntireColumn.AutoFit

' Make sure you release object references. Set oRng = Nothing

Set oSheet = Nothing Set oWB = Nothing Set oXL = Nothing Exit Sub

Err_Handler:

MsgBox Err.Description, vbCritical, "Error: " & Err.Number End Sub

Private Sub Form_Load() Set PD = New FIFO Set AD = New FIFO

Set InputData = New BYTEFIFO Dim cnttemp As Integer

PD.Reinitialize AD.Reinitialize InputData.Reinitialize InputData.SetSpecialData ("z") NumberStepValue = "1" PWMScroll.Enabled = True PWMScroll.Min = 0 PWMScroll.Max = 255 PWMScroll.Value = 0 ComList.AddItem "COM1" ComList.AddItem "COM2" ComList.AddItem "COM3" ComList.AddItem "COM4" ComList.AddItem "COM5" ComList.ListIndex = 0 For cnttemp = 0 To 255 PWMValue.AddItem Str(cnttemp) Next cnttemp PWMValue.ListIndex = 0 For cnttemp = 1 To 400

Bộ môn: Máy và ma sát học Đồ án tốt nghiệp

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 169

StepNumber.AddItem Str(cnttemp * 0.9) Next cnttemp

StepNumber.ListIndex = 0

MotorDir = True

'ConnectStt.Caption = "Chua Ket Noi"

With Graph

.PlotAreaColor = vbBlack .FrameStyle = BITMAP

.Caption = " Do Thi Goc-Apsuat" .XLabel = "Goc (Do)"

.YLabel = "Ap Suat (mBar)" .ClearGraph .ElementLineColor = RGB(255, 255, 0) .XGridNumber = 19 .YGridNumber = 15 .GridColor = RGB(100, 100, 100) .ShowGrid = True .ElementWidth = 1 .ElementLinetype = Solid .SetRange 0, 380, 0, 30 End With Timer1.Interval = 50

' disable all button when the comport is not already connected Timer1.Enabled = False PWM.Enabled = False Direction.Enabled = False Dir.Enabled = False PWMValue.Enabled = False Command.Enabled = False SaveGraph.Enabled = False ClearGraph.Enabled = False PWMScroll.Enabled = False StartProcess.Enabled = False StepButton.Enabled = False StopMove.Enabled = False

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 170 Excel.Enabled = False CmdText.Enabled = False PWMValue.Enabled = False Label2.Enabled = False SendStt.Enabled = False StepNumber.Enabled = False Connect.ColorButtonUp = &HFF& RefreshGraph.Enabled = False ClearCmd.Enabled = False End Sub

Private Sub PWM_Click()

Vel = Trim$(PWMValue.Text) PWMScroll.Value = Val(Vel) End Sub

Private Sub CmdText_Click() On Error GoTo ErrHandle SendStt = "Wait"

Exit Sub ErrHandle:

If Err.Number = 8018 Then MsgBox "Loi: Chua chon cong COM!" End Sub

Private Sub ClearGraph_Click() With Graph

.ClearGraph End With

End Sub

Private Sub Command_Click() On Error GoTo ErrHandle If CmdText.Text = "" Then SendStt = "Error"

MsgBox ("Error:Khong Co du lieu") Exit Sub Else Me.RS232Com.Output = CmdText.Text SendStt = "Done" End If Exit Sub

Bộ môn: Máy và ma sát học Đồ án tốt nghiệp

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 171

ErrHandle:

If Err.Number = 8018 Then MsgBox "Loi: Chua chon cong COM!" End Sub

Private Sub Connect_Click() Dim Rs232ComId As Byte Rs232ComId = 0

If ComList.Text = "COM1" Then Rs232ComId = 1

ElseIf ComList.Text = "COM2" Then Rs232ComId = 2

ElseIf ComList.Text = "COM3" Then Rs232ComId = 3

ElseIf ComList.Text = "COM4" Then Rs232ComId = 4

ElseIf ComList.Text = "COM5" Then Rs232ComId = 5

End If

If RS232Com.PortOpen Then RS232Com.PortOpen = False On Error GoTo ErrHandle

If Not Rs232ComId = 0 Then

RS232Com.CommPort = Rs232ComId RS232Com.Settings = "9600,N,8,1" Me.RS232Com.RThreshold = 1 RS232Com.InputLen = 1 RS232Com.PortOpen = True Me.RS232Com.Output = "a" Timer1.Enabled = True PWM.Enabled = True Direction.Enabled = True Dir.Enabled = True PWMValue.Enabled = True Command.Enabled = True SaveGraph.Enabled = True ClearGraph.Enabled = True PWMScroll.Enabled = True StartProcess.Enabled = True StepButton.Enabled = True StopMove.Enabled = True Excel.Enabled = True

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 172 CmdText.Enabled = True PWMValue.Enabled = True Label2.Enabled = True SendStt.Enabled = True StepNumber.Enabled = True Connect.ColorButtonUp = &H404000 RefreshGraph.Enabled = True ClearCmd.Enabled = True Else

MsgBox ("Chua Chon Cong Com") 'ConnectStt.Caption = "Chua Ket Noi" Timer1.Enabled = False PWM.Enabled = False Direction.Enabled = False Dir.Enabled = False PWMValue.Enabled = False Command.Enabled = False SaveGraph.Enabled = False ClearGraph.Enabled = False PWMScroll.Enabled = False StartProcess.Enabled = False StepButton.Enabled = False StopMove.Enabled = False Excel.Enabled = False CmdText.Enabled = False PWMValue.Enabled = False Label2.Enabled = False SendStt.Enabled = False StepNumber.Enabled = False Connect.ColorButtonUp = &HFF& ClearCmd.Enabled = False End If Exit Sub ErrHandle:

'ConnectStt.Caption = "Chua Ket Noi"

If Err.Number = 8002 Then MsgBox "Error: Thiet bi chua ket noi hoac chua bat nguon !"

If Err.Number = 8018 Then MsgBox "Error: Chi gui duoc khi thiet di da san sang!"

Bộ môn: Máy và ma sát học Đồ án tốt nghiệp

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 173

Timer1.Enabled = False PWM.Enabled = False Direction.Enabled = False Dir.Enabled = False PWMValue.Enabled = False Command.Enabled = False SaveGraph.Enabled = False ClearGraph.Enabled = False PWMScroll.Enabled = False StartProcess.Enabled = False StepButton.Enabled = False StopMove.Enabled = False Excel.Enabled = False CmdText.Enabled = False PWMValue.Enabled = False Label2.Enabled = False SendStt.Enabled = False Connect.ColorButtonUp = &HFF& RefreshGraph.Enabled = False ClearCmd.Enabled = False End Sub

Private Sub Direction_Click() On Error GoTo ErrHandle MotorDir = Not MotorDir If Not MotorDir Then Dir.Text = "Trai" Me.RS232Com.Output = "aAzaRz" Else Dir.Text = "Phai" Me.RS232Com.Output = "aBzaRz" End If Exit Sub ErrHandle:

If Err.Number = 8018 Then MsgBox "Error: Chua chon cong COM!" End Sub

Private Sub PWMScroll_Change()

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 174

End Sub

Private Sub PWMValue_Change()

PWMScroll.Value = Val(PWMValue.Text) End Sub

Private Sub RefreshGraph_Click() Graph.SetRange 0, 380, 0, 30 End Sub

Private Sub RS232Com_OnComm() Dim tempstr As String

If Me.RS232Com.CommEvent = comEvReceive Then tempstr = RS232Com.Input

InputData.Push (tempstr) End If

End Sub

Private Sub SaveGraph_Click() Graph.PrintGraph

End Sub

Private Sub StartProcess_Click() On Error GoTo ErrHandle If Dir.Text = "Trai" Then

Me.RS232Com.Output = "aAz" Else

Me.RS232Com.Output = "aBz" End If

Me.RS232Com.Output = "aD" + Vel + "zaT" + NumberStepValue + "zaEzaRz"

Exit Sub ErrHandle:

If Err.Number = 8018 Then MsgBox "Error: Chua Ket Noi Thiet Bi!" End Sub

Private Sub StepButton_Click()

NumberStepValue = Str(StepNumber.ListIndex + 1) End Sub

Bộ môn: Máy và ma sát học Đồ án tốt nghiệp

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 175

Private Sub StopMove_Click() On Error GoTo ErrHandle

Me.RS232Com.Output = "aD0zaRz" PWMValue.Text = "0"

Exit Sub ErrHandle:

If Err.Number = 8018 Then MsgBox "Error: Chua Ket Noi Thiet Bi!" End Sub

Private Sub ComInputDecoder() Dim Temp As String

Dim Value As Long

'Ma du lieu gui ve: aX[data]Y[data]z '#define _REPORT_AD 'X' // '#define _REPORT_PD 'Y' // If InputData.CheckData Then ' Go to the Starting code Temp = InputData.Pop Do While Not Temp = "a" Temp = InputData.Pop Loop

' Get the next cmd code Temp = InputData.Pop Value = 0

If Temp = "X" Then Temp = InputData.Pop Do While Not Temp = "Y"

Value = Value * 10 + Val(Temp) Temp = InputData.Pop

Loop

AD.Push (Value) Value = 0

Temp = InputData.Pop Do While Not Temp = "z"

Value = Value * 10 + Val(Temp) Temp = InputData.Pop

Loop

PD.Push (Value) Else

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 176

End If End If End Sub

Private Sub Timer1_Timer() ComInputDecoder

If Not AD.IsEmpty And Not PD.IsEmpty Then

Graph.PlotXY AD.Pop * 0.9, PD.Pop * 25 / 1023, 0 End If

Bộ môn: Máy và ma sát học Đồ án tốt nghiệp

Thực hiện:Nguyễn Tiến Long_Vũ Hoàng Thanh_Nguyễn Tiến Thành 177

CHƢƠNG V: NGHIÊN CỨU THỰC NGHIỆM MÁY ĐO ÁP

SUẤT Ổ ĐỠ THỦY ĐỘNG BKM-10

5.1. Sơ đồ độngvà nguyên lý hoạt động của máy BKM-10 5.1.1. Sơ đồ động của máy

Một phần của tài liệu LUẬN VĂN: NGHIÊN CỨU THIẾT KẾ VÀ CHẾ TẠO MÁY ĐO ÁP SUẤT CỦA Ổ ĐỠ THUỶ ĐỘNG CÓ KẾT NỐI MÁY TÍNH. PPTX (Trang 162 -178 )

×