Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Dweeno Link 2017 v2.0
- 'Note: this is the form code for a VB.net project.
- Imports System.ComponentModel
- Imports System.IO
- Public Class Form1
- Dim WithEvents MySerialPort As New Ports.SerialPort
- Dim outgoingData As String = ""
- Dim incomingData As String = ""
- Dim currentButtonFilePath As String = ""
- Dim buttonFileName As String = ""
- Dim UserConfig_Button_List As New List(Of Button)
- Dim UserConfig_ButtonText_List As New List(Of TextBox)
- Dim UserConfig_ButtonCommand_List As New List(Of TextBox)
- 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 = Ports.Parity.None
- MySerialPort.StopBits = Ports.StopBits.One
- MySerialPort.DataBits = 8
- MySerialPort.Handshake = Ports.Handshake.None
- MySerialPort.RtsEnable = True
- MySerialPort.ReadTimeout = 1000
- ComboBox1.Items.Add(115200)
- ComboBox1.Items.Add(57600)
- ComboBox1.Items.Add(38400)
- ComboBox1.Items.Add(28800)
- ComboBox1.Items.Add(19200)
- ComboBox1.Items.Add(14400)
- ComboBox1.Items.Add(9600)
- ComboBox1.SelectedIndex = 0
- AddHandler MySerialPort.DataReceived, AddressOf DataReceivedHandler
- 'OpenFileDialog2.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
- 'currentButtonFilePath = Application.StartupPath()
- currentButtonFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
- UserConfig_Button_List.Clear()
- UserConfig_Button_List.Add(UPButton1)
- UserConfig_Button_List.Add(UPButton2)
- UserConfig_Button_List.Add(UPButton3)
- UserConfig_Button_List.Add(UPButton4)
- UserConfig_Button_List.Add(UPButton5)
- UserConfig_Button_List.Add(UPButton6)
- UserConfig_Button_List.Add(UPButton7)
- UserConfig_Button_List.Add(UPButton8)
- UserConfig_Button_List.Add(UPButton9)
- UserConfig_Button_List.Add(UPButton10)
- UserConfig_Button_List.Add(UPButton11)
- UserConfig_Button_List.Add(UPButton12)
- UserConfig_Button_List.Add(UPButton13)
- UserConfig_Button_List.Add(UPButton14)
- UserConfig_Button_List.Add(UPButton15)
- UserConfig_Button_List.Add(UPButton16)
- UserConfig_Button_List.Add(UPButton17)
- UserConfig_Button_List.Add(UPButton18)
- UserConfig_Button_List.Add(UPButton19)
- UserConfig_Button_List.Add(UPButton20)
- UserConfig_ButtonText_List.Clear()
- UserConfig_ButtonText_List.Add(UBText_TextBox1)
- UserConfig_ButtonText_List.Add(UBText_TextBox2)
- UserConfig_ButtonText_List.Add(UBText_TextBox3)
- UserConfig_ButtonText_List.Add(UBText_TextBox4)
- UserConfig_ButtonText_List.Add(UBText_TextBox5)
- UserConfig_ButtonText_List.Add(UBText_TextBox6)
- UserConfig_ButtonText_List.Add(UBText_TextBox7)
- UserConfig_ButtonText_List.Add(UBText_TextBox8)
- UserConfig_ButtonText_List.Add(UBText_TextBox9)
- UserConfig_ButtonText_List.Add(UBText_TextBox10)
- UserConfig_ButtonText_List.Add(UBText_TextBox11)
- UserConfig_ButtonText_List.Add(UBText_TextBox12)
- UserConfig_ButtonText_List.Add(UBText_TextBox13)
- UserConfig_ButtonText_List.Add(UBText_TextBox14)
- UserConfig_ButtonText_List.Add(UBText_TextBox15)
- UserConfig_ButtonText_List.Add(UBText_TextBox16)
- UserConfig_ButtonText_List.Add(UBText_TextBox17)
- UserConfig_ButtonText_List.Add(UBText_TextBox18)
- UserConfig_ButtonText_List.Add(UBText_TextBox19)
- UserConfig_ButtonText_List.Add(UBText_TextBox20)
- UserConfig_ButtonCommand_List.Clear()
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox1)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox2)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox3)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox4)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox5)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox6)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox7)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox8)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox9)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox10)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox11)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox12)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox13)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox14)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox15)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox16)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox17)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox18)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox19)
- UserConfig_ButtonCommand_List.Add(UBCom_TextBox20)
- 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.BaudRate = Convert.ToInt32(ComboBox1.SelectedItem.ToString)
- 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 = ""
- If CheckBox1.CheckState = CheckState.Unchecked Then
- Send_Button.Enabled = False
- End If
- 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
- Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
- If ListBox1.SelectedIndex <> -1 Then
- If CheckBox1.CheckState = CheckState.Checked Then
- Send_Button.Enabled = True
- End If
- End If
- End Sub
- '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- 'below is code for the file-configurable send buttons.
- Private Sub Button7_Click(sender As Object, e As EventArgs) Handles LoadUBF_Button.Click
- OpenFileDialog1.InitialDirectory = currentButtonFilePath
- OpenFileDialog1.ShowDialog()
- End Sub
- Private Sub OpenFileDialog1_FileOk(sender As Object, e As CancelEventArgs) Handles OpenFileDialog1.FileOk
- 'OpenFileDialog1 is used for reading button-config files only.
- UBFpath_TextBox.Text = Path.GetFileName(OpenFileDialog1.FileName)
- currentButtonFilePath = Path.GetFullPath(OpenFileDialog1.FileName)
- ResetButtonNamesArray()
- ResetButtonCommandsArray()
- If Read_ButtonsFile() = True Then
- RefreshUserButtonText()
- End If
- End Sub
- Private Sub ResetButtonNamesArray()
- For x = 0 To 19
- UserConfig_ButtonText_List(x).Text = "[not set]"
- Next
- End Sub
- Private Sub ResetButtonCommandsArray()
- For x = 0 To 19
- UserConfig_ButtonCommand_List(x).Text = "999:*"
- Next
- End Sub
- Private Sub UserConfigButton_EventSub(buttonNumber As Integer)
- If MySerialPort.IsOpen() Then
- If Send_Button.Enabled = True Then
- If Not String.IsNullOrWhiteSpace(UserConfig_ButtonCommand_List(buttonNumber).Text) Then
- incomingData = ""
- Sending_TextBox.Text = UserConfig_ButtonCommand_List(buttonNumber).Text
- outgoingData = Sending_TextBox.Text
- SendOutgoingData()
- Receiving_TextBox.Text = ""
- If CheckBox1.CheckState = CheckState.Unchecked Then
- Send_Button.Enabled = False
- End If
- Else
- MessageBox.Show("error: no command to send")
- End If
- End If
- Else
- MessageBox.Show("error: serial port not open")
- End If
- End Sub
- Private Sub RefreshUserButtonText()
- For x = 0 To 19
- UserConfig_Button_List(x).Text = UserConfig_ButtonText_List(x).Text
- Next
- End Sub
- Private Function Read_ButtonsFile() As Boolean
- Dim buttonFileLine As String = ""
- Dim lineFields As String()
- Dim readLines As Boolean = True
- Dim counter As Integer = 0
- If File.Exists(OpenFileDialog1.FileName) = True Then
- Using fileIn As New StreamReader(OpenFileDialog1.FileName)
- Do While readLines = True
- Try
- buttonFileLine = fileIn.ReadLine()
- If String.Compare(buttonFileLine, "EOF", True) = 0 Then
- readLines = False
- ElseIf String.IsNullOrWhiteSpace(buttonFileLine) Then
- readLines = False
- MessageBox.Show("Error!" & Environment.NewLine & "button config file contains empty line")
- Else
- Try
- lineFields = buttonFileLine.Split("|")
- UserConfig_ButtonText_List(counter).Text = lineFields(2)
- UserConfig_ButtonCommand_List(counter).Text = lineFields(3)
- counter += 1
- Catch ex As ArgumentOutOfRangeException
- counter += 1
- MessageBox.Show("Error!" & Environment.NewLine & "button config file: read failure" & Environment.NewLine & "error on line # " & counter.ToString())
- End Try
- End If
- Catch ex As EndOfStreamException
- readLines = False
- MessageBox.Show("Error!" & Environment.NewLine & "button config file stream" & Environment.NewLine & "end-of-file marker not present")
- End Try
- Loop
- End Using
- Return True
- Else
- Return False
- End If
- End Function
- Private Sub SaveUBF_Button_Click(sender As Object, e As EventArgs) Handles SaveUBF_Button.Click
- SaveFileDialog1.InitialDirectory = currentButtonFilePath
- SaveFileDialog1.ShowDialog()
- End Sub
- Private Sub SaveFileDialog1_FileOk(sender As Object, e As CancelEventArgs) Handles SaveFileDialog1.FileOk
- Write_ButtonsFile()
- End Sub
- Private Sub Write_ButtonsFile()
- Using fileOut As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(SaveFileDialog1.FileName, False)
- Dim counter As Integer = 0
- For x = 0 To 19
- counter = x + 1
- fileOut.WriteLine("button|" & counter.ToString() & "|" & UserConfig_ButtonText_List(x).Text & "|" & UserConfig_ButtonCommand_List(x).Text)
- Next
- fileOut.WriteLine("EOF")
- currentButtonFilePath = SaveFileDialog1.FileName
- UBFpath_TextBox.Text = currentButtonFilePath
- End Using
- End Sub
- Private Sub UPButtons_UseEdits_Button_Click(sender As Object, e As EventArgs) Handles UPButtons_UseEdits_Button.Click
- RefreshUserButtonText()
- End Sub
- Private Sub UPButton1_Click(sender As Object, e As EventArgs) Handles UPButton1.Click
- UserConfigButton_EventSub(0)
- End Sub
- Private Sub UPButton2_Click(sender As Object, e As EventArgs) Handles UPButton2.Click
- UserConfigButton_EventSub(1)
- End Sub
- Private Sub UPButton3_Click(sender As Object, e As EventArgs) Handles UPButton3.Click
- UserConfigButton_EventSub(2)
- End Sub
- Private Sub UPButton4_Click(sender As Object, e As EventArgs) Handles UPButton4.Click
- UserConfigButton_EventSub(3)
- End Sub
- Private Sub UPButton5_Click(sender As Object, e As EventArgs) Handles UPButton5.Click
- UserConfigButton_EventSub(4)
- End Sub
- Private Sub UPButton6_Click(sender As Object, e As EventArgs) Handles UPButton6.Click
- UserConfigButton_EventSub(5)
- End Sub
- Private Sub UPButton7_Click(sender As Object, e As EventArgs) Handles UPButton7.Click
- UserConfigButton_EventSub(6)
- End Sub
- Private Sub UPButton8_Click(sender As Object, e As EventArgs) Handles UPButton8.Click
- UserConfigButton_EventSub(7)
- End Sub
- Private Sub UPButton9_Click(sender As Object, e As EventArgs) Handles UPButton9.Click
- UserConfigButton_EventSub(8)
- End Sub
- Private Sub UPButton10_Click(sender As Object, e As EventArgs) Handles UPButton10.Click
- UserConfigButton_EventSub(9)
- End Sub
- Private Sub UPButton11_Click(sender As Object, e As EventArgs) Handles UPButton11.Click
- UserConfigButton_EventSub(10)
- End Sub
- Private Sub UPButton12_Click(sender As Object, e As EventArgs) Handles UPButton12.Click
- UserConfigButton_EventSub(11)
- End Sub
- Private Sub UPButton13_Click(sender As Object, e As EventArgs) Handles UPButton13.Click
- UserConfigButton_EventSub(12)
- End Sub
- Private Sub UPButton14_Click(sender As Object, e As EventArgs) Handles UPButton14.Click
- UserConfigButton_EventSub(13)
- End Sub
- Private Sub UPButton15_Click(sender As Object, e As EventArgs) Handles UPButton15.Click
- UserConfigButton_EventSub(14)
- End Sub
- Private Sub UPButton16_Click(sender As Object, e As EventArgs) Handles UPButton16.Click
- UserConfigButton_EventSub(15)
- End Sub
- Private Sub UPButton17_Click(sender As Object, e As EventArgs) Handles UPButton17.Click
- UserConfigButton_EventSub(16)
- End Sub
- Private Sub UPButton18_Click(sender As Object, e As EventArgs) Handles UPButton18.Click
- UserConfigButton_EventSub(17)
- End Sub
- Private Sub UPButton19_Click(sender As Object, e As EventArgs) Handles UPButton19.Click
- UserConfigButton_EventSub(18)
- End Sub
- Private Sub UPButton20_Click(sender As Object, e As EventArgs) Handles UPButton20.Click
- UserConfigButton_EventSub(19)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement