v12345vtm

vbdotnet

Feb 19th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. 'Simple example of receiving serial data
  2. 'written in Visual Basic 2013
  3. 'http://www.martyncurrey.com/arduino-and-visual-basic-part-1-receiving-data-from-the-arduino/
  4.  
  5. Imports System
  6. Imports System.IO.Ports
  7.  
  8. Public Class Form1
  9. Dim comPORT As String
  10. Dim receivedData As String = ""
  11.  
  12. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  13. Timer1.Enabled = False
  14. comPORT = ""
  15. For Each sp As String In My.Computer.Ports.SerialPortNames
  16. comPort_ComboBox.Items.Add(sp)
  17. Next
  18. End Sub
  19.  
  20.  
  21. Private Sub comPort_ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
  22. If (comPort_ComboBox.SelectedItem <> "") Then
  23. comPORT = comPort_ComboBox.SelectedItem
  24. End If
  25. End Sub
  26.  
  27.  
  28. Private Sub connect_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles connect_BTN.Click
  29. If (connect_BTN.Text = "Connect") Then
  30. If (comPORT <> "") Then
  31. SerialPort1.Close()
  32. SerialPort1.PortName = comPORT
  33. SerialPort1.BaudRate = 115200
  34. SerialPort1.DataBits = 8
  35. SerialPort1.Parity = Parity.None
  36. SerialPort1.StopBits = StopBits.One
  37. SerialPort1.Handshake = Handshake.None
  38. SerialPort1.Encoding = System.Text.Encoding.Default
  39. SerialPort1.ReadTimeout = 1000 ' 10000
  40.  
  41. SerialPort1.Open()
  42. connect_BTN.Text = "Dis-connect"
  43. Timer1.Enabled = True
  44. Timer_LBL.Text = "Timer: ON"
  45. Else
  46. MsgBox("Select a COM port first")
  47. End If
  48. Else
  49. SerialPort1.Close()
  50. connect_BTN.Text = "Connect"
  51. Timer1.Enabled = False
  52. Timer_LBL.Text = "Timer: OFF"
  53. End If
  54.  
  55.  
  56. End Sub
  57.  
  58.  
  59. Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
  60. receivedData = ReceiveSerialData()
  61. Dim strArr
  62. strArr = receivedData.Split(" ")
  63.  
  64. ProgressBar1.Value = strArr(0)
  65. ProgressBar2.Value = strArr(1)
  66. ProgressBar3.Value = strArr(2)
  67.  
  68. ProgressBar4.Value = strArr(3)
  69. ProgressBar5.Value = strArr(4)
  70. ProgressBar6.Value = strArr(5)
  71. ProgressBar7.Value = strArr(6)
  72.  
  73.  
  74. RichTextBox1.Text &= receivedData
  75. End Sub
  76.  
  77.  
  78. Function ReceiveSerialData() As String
  79. Dim Incoming As String
  80. Try
  81. Incoming = SerialPort1.ReadExisting()
  82. If Incoming Is Nothing Then
  83. Return "nothing" & vbCrLf
  84. Else
  85. Return Incoming
  86. End If
  87. Catch ex As TimeoutException
  88. Return "Error: Serial Port read timed out."
  89. End Try
  90.  
  91. End Function
  92.  
  93.  
  94. Private Sub clear_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles clear_BTN.Click
  95. RichTextBox1.Text = ""
  96. End Sub
  97.  
  98.  
  99. End Class
Advertisement
Add Comment
Please, Sign In to add comment