Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Dweeno Link 2017 v1.0
- 'Note: this is the form code for a VB.net project.
- Imports System.ComponentModel
- Public Class Form1
- Dim WithEvents MySerialPort As New IO.Ports.SerialPort
- Dim outgoingData As String
- Dim incomingData As String
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- 'The BaudRate of the serial port (set immediately below) and the baud rate set in the Arduino sketch must match.
- 'Most Arduino examples leave the baud rate at the lowest setting of 9600, but even the China clone boards can usually run at 115200.
- MySerialPort.BaudRate = 115200
- 'Don't change the settings below unless you know what they do.
- MySerialPort.Parity = IO.Ports.Parity.None
- MySerialPort.StopBits = IO.Ports.StopBits.One
- MySerialPort.DataBits = 8
- MySerialPort.Handshake = IO.Ports.Handshake.None
- MySerialPort.RtsEnable = True
- MySerialPort.ReadTimeout = 1000
- AddHandler MySerialPort.DataReceived, AddressOf DataReceivedHandler
- End Sub
- Private Sub DataReceivedHandler(sender As Object, e As EventArgs)
- Try
- incomingData = MySerialPort.ReadExisting()
- MySerialPort.DiscardInBuffer()
- DisplayPortData()
- Catch x As Exception
- MessageBox.Show("Exception = " & x.Message)
- End Try
- End Sub
- Private Sub DisplayPortData()
- 'This sub is what allows the serial port thread to move data back into the main thread.
- If InvokeRequired Then
- Invoke(New MethodInvoker(AddressOf DisplayPortData))
- Else
- Receiving_TextBox.Text = incomingData
- Dim stringVal As String = incomingData.Substring(incomingData.Length - 1)
- If String.Compare(stringVal, "*", True) = 0 Then
- Send_Button.Enabled = True
- '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.
- Else
- MessageBox.Show("error! = [" & incomingData & "]")
- End If
- End If
- End Sub
- Private Sub RSP_Button_Click(sender As Object, e As EventArgs) Handles RSP_Button.Click
- ListBox1.Items.Clear()
- For Each sp As String In My.Computer.Ports.SerialPortNames
- ListBox1.Items.Add(sp)
- Next
- End Sub
- Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
- If Not String.IsNullOrWhiteSpace(CType(ListBox1.SelectedItem, String)) Then
- MySerialPort.Close()
- MySerialPort.PortName = CType(ListBox1.SelectedItem, String)
- SelectedPort_TextBox.Text = CType(ListBox1.SelectedItem, String)
- MySerialPort.Open()
- Send_Button.Enabled = True
- End If
- End Sub
- Private Sub Send_Button_Click(sender As Object, e As EventArgs) Handles Send_Button.Click
- If MySerialPort.IsOpen() Then
- incomingData = ""
- outgoingData = Sending_TextBox.Text
- 'If you wanted the ability to automatically progress through a list of commands,
- 'then this is where you would start the process by sending the first command in the list of commands to send.
- SendOutgoingData()
- Receiving_TextBox.Text = ""
- Send_Button.Enabled = False
- End If
- End Sub
- Private Sub SendOutgoingData()
- MySerialPort.Write(outgoingData)
- MySerialPort.DiscardOutBuffer()
- End Sub
- Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
- If MySerialPort.IsOpen() Then
- MySerialPort.Close()
- End If
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement