Advertisement
skizziks_53

Dweeno Link 2017 v1.0

Aug 16th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. 'Dweeno Link 2017 v1.0
  2. 'Note: this is the form code for a VB.net project.
  3.  
  4. Imports System.ComponentModel
  5.  
  6. Public Class Form1
  7.  
  8. Dim WithEvents MySerialPort As New IO.Ports.SerialPort
  9. Dim outgoingData As String
  10. Dim incomingData As String
  11.  
  12. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  13. 'The BaudRate of the serial port (set immediately below) and the baud rate set in the Arduino sketch must match.
  14. 'Most Arduino examples leave the baud rate at the lowest setting of 9600, but even the China clone boards can usually run at 115200.
  15. MySerialPort.BaudRate = 115200
  16. 'Don't change the settings below unless you know what they do.
  17. MySerialPort.Parity = IO.Ports.Parity.None
  18. MySerialPort.StopBits = IO.Ports.StopBits.One
  19. MySerialPort.DataBits = 8
  20. MySerialPort.Handshake = IO.Ports.Handshake.None
  21. MySerialPort.RtsEnable = True
  22. MySerialPort.ReadTimeout = 1000
  23. AddHandler MySerialPort.DataReceived, AddressOf DataReceivedHandler
  24. End Sub
  25.  
  26. Private Sub DataReceivedHandler(sender As Object, e As EventArgs)
  27. Try
  28. incomingData = MySerialPort.ReadExisting()
  29. MySerialPort.DiscardInBuffer()
  30. DisplayPortData()
  31. Catch x As Exception
  32. MessageBox.Show("Exception = " & x.Message)
  33. End Try
  34. End Sub
  35.  
  36. Private Sub DisplayPortData()
  37. 'This sub is what allows the serial port thread to move data back into the main thread.
  38. If InvokeRequired Then
  39. Invoke(New MethodInvoker(AddressOf DisplayPortData))
  40. Else
  41. Receiving_TextBox.Text = incomingData
  42. Dim stringVal As String = incomingData.Substring(incomingData.Length - 1)
  43. If String.Compare(stringVal, "*", True) = 0 Then
  44. Send_Button.Enabled = True
  45. 'This is where you would add the ability to process through a list of commands, by re-sending the 'next' command in a list of commands.
  46. Else
  47. MessageBox.Show("error! = [" & incomingData & "]")
  48. End If
  49. End If
  50. End Sub
  51.  
  52. Private Sub RSP_Button_Click(sender As Object, e As EventArgs) Handles RSP_Button.Click
  53. ListBox1.Items.Clear()
  54. For Each sp As String In My.Computer.Ports.SerialPortNames
  55. ListBox1.Items.Add(sp)
  56. Next
  57. End Sub
  58.  
  59. Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
  60. If Not String.IsNullOrWhiteSpace(CType(ListBox1.SelectedItem, String)) Then
  61. MySerialPort.Close()
  62. MySerialPort.PortName = CType(ListBox1.SelectedItem, String)
  63. SelectedPort_TextBox.Text = CType(ListBox1.SelectedItem, String)
  64. MySerialPort.Open()
  65. Send_Button.Enabled = True
  66. End If
  67. End Sub
  68.  
  69. Private Sub Send_Button_Click(sender As Object, e As EventArgs) Handles Send_Button.Click
  70. If MySerialPort.IsOpen() Then
  71. incomingData = ""
  72. outgoingData = Sending_TextBox.Text
  73. 'If you wanted the ability to automatically progress through a list of commands,
  74. 'then this is where you would start the process by sending the first command in the list of commands to send.
  75. SendOutgoingData()
  76. Receiving_TextBox.Text = ""
  77. Send_Button.Enabled = False
  78. End If
  79. End Sub
  80.  
  81. Private Sub SendOutgoingData()
  82. MySerialPort.Write(outgoingData)
  83. MySerialPort.DiscardOutBuffer()
  84. End Sub
  85.  
  86. Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
  87. If MySerialPort.IsOpen() Then
  88. MySerialPort.Close()
  89. End If
  90. End Sub
  91.  
  92. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement