Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Dim pulse As Integer    'Varible for desired_pulse
  3. Dim str As Variant      'Count pulse received from microcontroller
  4. Dim speed As Double     'Real speed
  5. Dim desired_speed As Double
  6. Dim Error_speed As Double
  7. Dim objExcel As Excel.Application   ' Setup for OLE graph
  8. Dim wExcel As Excel.Workbook
  9. Dim xlchart As Excel.Chart
  10. Dim j As Integer
  11.  
  12. Private Sub cmdExit_Click()
  13.     objExcel.Visible = True
  14.     End
  15.     'Exit the form and show the graph in Microsoft Excel
  16. End Sub
  17.  
  18. Private Sub cmdNew_Click()
  19.     'Clear the graph
  20.     Workbooks("Speed2").Sheets("Sheet1").Range("A1:E1000") = ""
  21.     j =2
  22.     OLE_Speed.Update
  23.     Counter1.Value = 0
  24.     Counter2.Value = 0
  25.     Counter3.Value = 0
  26. End Sub
  27.  
  28. Private Sub cmdRun_Stop_Click()
  29.     If cmdRun_Stop.Caption = "&Run" Then
  30.         desired_speed = Val(cboSpeed.Text) ' Get desired speed from user
  31.         pulse = Round(desired_speed / 60 * 4 * 0.39321) 'Convert the desired speed into desired pulse
  32.     If Val(cboSpeed.Text) > 0 Then
  33.         ' If receive desired speed from user
  34.         MSComm1.Output = Chr$(pulse) ' Send desired count pulse to microcontroller
  35.         lblPulse.Caption = pulse
  36.         Counter1.TimerEnabled = True ' Start the counter as timer
  37.         cboSpeed.Locked = True
  38.         ' Lock the desired speed selection
  39.         cmdRun_Stop.Caption = "&Stop"
  40.         cmdExit.Enabled = False
  41.         ' Disable the command of Exit and New Graph
  42.         cmdNew.Enabled = False
  43.     Else
  44.         MsgBox "You must select a desired speed first!", vbOKOnly, "Invalid Data"
  45.         ' If no desired speed receive from user
  46.         MSComm1.Output = Chr$(0)
  47.         cmdExit.Enabled = True
  48.         cmdNew.Enabled = True
  49.     End If
  50.     ElseIf cmdRun_Stop.Caption = "&Stop" Then
  51.         Counter1.TimerEnabled = False
  52.         ' Stop the timer
  53.         cmdRun_Stop.Caption = "&Run"
  54.         pulse = 0
  55.         MSComm1.Output = Chr$(0)
  56.         ' Send speed = 0 rpm to stop the motor
  57.         cboSpeed.Locked = False
  58.         cmdExit.Enabled = True
  59.         cmdNew.Enabled = True
  60.     End If
  61. End Sub
  62.  
  63. Private Sub Form_Load()
  64.     'MSComm setup
  65.     With MSComm1
  66.         .CommPort = 2
  67.         ' Use CommPort2 to communicate with microcontroller
  68.         .Settings = "9600,N,8,1"
  69.         ' Baud rate 9600, none parity, 8 data bits, 1 stop bit
  70.         .InBufferSize = 1024
  71.         ' Receiver buffer = 1024 bytes
  72.         .OutBufferSize = 1024
  73.         ' Transmitter buffer = 1024 bytes
  74.         .DTREnable = True
  75.         ' Enable the Data Terminal Ready signal
  76.         .EOFEnable = False
  77.         ' Disable the End of File type
  78.         .Handshaking = comNone ' Disable all network handshaking
  79.         .InputLen = 1
  80.         ' Read all the charater in buffer
  81.         .InputMode = comInputModeText ' Set the incoming messages to ve ASCII text characters
  82.         .NullDiscard = False
  83.         ' Discard bytes that are all zero's
  84.         .RThreshold = 1
  85.         ' Set the Receive Oncomm event to occur after 1 byte of data have been received
  86.         .RTSEnable = True
  87.         ' Enable the request to send data
  88.         .SThreshold = 1
  89.         ' Set the Transmitter Oncomm event to occur after 1 byte of data have been received
  90.         .PortOpen = True
  91.         ' Open CommPort
  92.     End With
  93.    
  94.     'Setup for excel file
  95.     Set objExcel = GetObject("", "Excel.Application")
  96.     Set wExcel = objExcel.Workbooks.Open("C:\Speed2.xls") ' File Speed2 as storage
  97.     objExcel.Visible = False
  98.     j = 2
  99.     With Workbooks("Speed2").Sheets("Sheet1")
  100.         .Cells(1, 1) = 0
  101.         .Cells(2, 1) = 0
  102.     End With
  103.    
  104.     OLE_Speed.Update
  105. End Sub
  106.  
  107. Private Sub MSComm1_OnComm()
  108.     If MSComm1.CommEvent = comEvReceive Then
  109.         ' This is used when data is received
  110.         str = Asc(MSComm1.Input)
  111.         'Get the counter pulse from microcontroller
  112.         lblCount_pulse.Caption = str
  113.         speed = Round(str / 4 / 0.39321 * 60, 2) ' Convert counter pulse into speed(rpm)
  114.         Error_speed = Round((speed - desired_speed), 2) ' Calculate the error between detected speed with the desired speed
  115.         lblSpeed.Caption = speed
  116.         lblError.Caption = Error_speed
  117.         MSComm1.Output = Chr$(pulse) 'Always send desired pulse to microcontroller
  118.     End If
  119.    
  120.     If Counter1.Value > 0 Then
  121.         If (Counter3.Value * 60 * 60) + (Counter2.Value * 60) + Counter1.Value > Workbooks("Speed2").Sheets("Sheet1").Cells(j - 1, 1).Value Then
  122.             'Plotting graph
  123.             With Workbooks("Speed2").Sheets("Sheet1")
  124.                 .Cells(j, 1) = (Counter3.Value * 60 * 60) + (Counter2.Value * 60) + Counter1.Value
  125.                 .Cells(j, 2) = Format(speed, "##.##")
  126.                 .Cells(j, 3) = desired_speed
  127.             End With
  128.             j = j + 1
  129.             OLE_Speed.Update ' Update the graph
  130.         End If
  131.     End If
  132.     'Timer : Counter1 as second, Counter2 as minute, and Counter3 as hour
  133.     If Counter1.Value = 60 Then
  134.         Counter2.Value = Counter2.Value + 1
  135.         Counter1.Value = 0
  136.         Counter1.TimerEnabled = True
  137.         If Counter2.Value = 60 Then
  138.             Counter3.Value = Counter3.Value + 1
  139.             Counter2.Value = 0
  140.         End If
  141. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement