V5 Seriële verbinding
Het hele programma is hier onder te downloaden bij "Download alles" dit is de complete documentatie die je nodig hebt al wil je het programma aanpassen. Als je er wat leuks mee gedaan hebt, of verbeteringen heb kun je dat gewoon in het contacten formulier zetten. Ik zal er dan zo spoedig mogelijk naar kijken. Als je alleen het programma wilt downloaden kun je de .exe ook downloaden. Dit is alleen de toepassing en kan niet aangepast worden.
De COM5 poort in de dropdown box is de poort waar de Arduino aan verbonden is. Al druk je op de knop herlaad het programma alle poorten voor je, en selecteert de eerste poort die hij vindt als er nog geen is geselecteerd.
De Connect (verbind) knopt kun je mee verbinden, en word een verbreek knop als hij verbonden is.
De dunne lange balk eronder is voor de tekens die je naar de Arduino wilt sturen.
De knop hiernaast met het pijltje is de verzend knop.
Het grote tekstvak onderaan is de informatie die de Arduino terug geeft aan de PC.
Download Alles (Visual studio nodig!)
Download alleen het .exe bestand
De COM5 poort in de dropdown box is de poort waar de Arduino aan verbonden is. Al druk je op de knop herlaad het programma alle poorten voor je, en selecteert de eerste poort die hij vindt als er nog geen is geselecteerd.
De Connect (verbind) knopt kun je mee verbinden, en word een verbreek knop als hij verbonden is.
De dunne lange balk eronder is voor de tekens die je naar de Arduino wilt sturen.
De knop hiernaast met het pijltje is de verzend knop.
Het grote tekstvak onderaan is de informatie die de Arduino terug geeft aan de PC.
Download Alles (Visual studio nodig!)
Download alleen het .exe bestand
De volledige code zoals in Visual Studio
' This code is written by Jelle Wie WWW.Jellewie.Weebly.Com
'
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Serial_Data
Dim MSGBoxName As String
Dim MSG As String
Dim myPort As Array
Dim ArduinoAnswer As String
Dim ButtonSelected As String
Dim PrefUSB As String
'Put under this line your own strings
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
'Action - (when) Starting up
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReloadUSB()
MSGBoxName = "[J] JelleWie"
ButtonConnectDisconnect.Select()
RunOnStartup()
End Sub
'Button - Reload USB
Private Sub ComboBoxPoort_USBIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxUSB.Click
ReloadUSB()
End Sub
'Code - Reload USB
Sub ReloadUSB()
PrefUSB = ComboBoxUSB.SelectedItem
ComboBoxUSB.Items.Clear()
On Error GoTo ErrHand
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBoxUSB.Items.AddRange(myPort)
ComboBoxUSB.SelectionStart.ToString()
ComboBoxUSB.SelectedItem = PrefUSB
If ComboBoxUSB.SelectedItem = "" Then
ComboBoxUSB.SelectedItem = "COM10"
ComboBoxUSB.SelectedItem = "COM9"
ComboBoxUSB.SelectedItem = "COM8"
ComboBoxUSB.SelectedItem = "COM7"
ComboBoxUSB.SelectedItem = "COM6"
ComboBoxUSB.SelectedItem = "COM5"
ComboBoxUSB.SelectedItem = "COM4"
ComboBoxUSB.SelectedItem = "COM3"
ComboBoxUSB.SelectedItem = "COM2"
ComboBoxUSB.SelectedItem = "COM1"
ComboBoxUSB.SelectedItem = "COM0"
End If
ErrHand:
End Sub
'Button - Send
Private Sub ButtonSend_Click(sender As Object, e As EventArgs) Handles ButtonSend.Click
SerialSend()
End Sub
'Button - Enter Pressed (Send)
Private Sub TextBoxInput_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxInput.KeyDown
If e.KeyCode = Keys.Enter Then
SerialSend()
End If
End Sub
'Code - Serial send
Sub SerialSend()
RunOnSend()
If ButtonConnectDisconnect.Text = "Connect" Then
ButtonConnectDisconnect.Select()
ButtonSelected = ActiveControl.Name
MSG = "Where te hell should I write to?!" + Chr(13) + "try connecting first!"
Dim result As Integer = MessageBox.Show(MSG, MSGBoxName, MessageBoxButtons.OKCancel)
If result = DialogResult.Cancel Then
ElseIf result = DialogResult.OK Then
MsgBox("If your the programmer, Here is your solution" + Chr(13) + Chr(13) + "Put 'Enabled' in the properties of " + ButtonSelected + " to False" + Chr(13) + Chr(13) + "ADD " + Chr(13) + ButtonSelected + ".Enabled = True" + Chr(13) + "TO 'Sub RunOnConnect()'" + Chr(13) + Chr(13) + "ADD " + Chr(13) + ButtonSelected + ".Enabled =False" + Chr(13) + "TO 'Sub RunOnDisconnect()'")
End If
Else
RichTextBoxOutput.Text = ""
On Error GoTo ErrHand
SerialPort1.Write(TextBoxInput.Text)
End If
Exit Sub
ErrHand:
MsgBox("You couldn't resist to remove the cable didn't you?", , MSGBoxName)
Disconnect()
End Sub
'Button - Connect and Disconnect
Private Sub ButtonButtonConnectDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConnectDisconnect.Click
If ButtonConnectDisconnect.Text = "Connect" Then
'Connect
Connect()
Else
'Disconnect
ButtonConnectDisconnect.Text = "Connect"
ButtonConnectDisconnect.ForeColor = Color.Green
Disconnect()
End If
End Sub
'Code - Connect
Sub Connect()
If ComboBoxUSB.Text <> "" Then
On Error GoTo ErrHand
SerialPort1.PortName = ComboBoxUSB.Text
SerialPort1.BaudRate = 9600
SerialPort1.Open()
ComboBoxUSB.Enabled = False
ButtonSend.Enabled = True
TextBoxInput.Enabled = True
ButtonConnectDisconnect.Text = "Disconnect"
ButtonConnectDisconnect.ForeColor = Color.Red
TextBoxInput.Select()
RunOnConnect()
Else
MsgBox("I can not connect to nothing! what where you thinking..." + Chr(13) + "Please give me a COM poort to connect to, before letting me try to connect to it", , MSGBoxName)
ReloadUSB()
End If
Exit Sub
ErrHand:
MsgBox("The COM Port your trying to use, does not seems to work annymore" + Chr(13) + "Did you remove the cable again?", , MSGBoxName)
ReloadUSB()
ButtonConnectDisconnect.Select()
End Sub
'Code - Disconnect
Sub Disconnect()
ComboBoxUSB.Enabled = True
ButtonSend.Enabled = False
TextBoxInput.Enabled = False
ButtonConnectDisconnect.Text = "Connect"
ButtonConnectDisconnect.ForeColor = Color.Green
ButtonConnectDisconnect.Select()
RunOnDisconnect()
On Error GoTo ErrHand
SerialPort1.Close()
Exit Sub
ErrHand:
ReloadUSB()
End Sub
'Action - Recieved serial data
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
System.Threading.Thread.Sleep(5)
ReceivedText(SerialPort1.ReadExisting())
End Sub
'Code - Recieved serial data
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
On Error GoTo ErrHand
If Me.RichTextBoxOutput.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBoxOutput.Text &= [text] 'append text
ArduinoAnswer = [text]
RunOnDataRecieved()
End If
Exit Sub
ErrHand:
MsgBox("Error while recieving data", , MSGBoxName)
End Sub
'==================================================
'==================================================
'========== Your Codes ==========
'==================================================
'==================================================
'Action - (when) sending data
Sub RunOnSend()
'Code to run befor sending data
End Sub
'Action - (when) starting up
Sub RunOnStartup()
'This code will run once starting up
MsgBox("Welcome to this awsome program", , MSGBoxName)
End Sub
'Action - (when) Connect
Sub RunOnConnect()
'This is to enable the Example button when there is a connection
Button1.Enabled = True
'You can use the next lines to send a commando to the Arduino when connecting, sso you can reset it
'TextBoxInput.Text = "Reset"
'SerialSend()
End Sub
'Action - (when) Disconnect
Sub RunOnDisconnect()
'This diables the Example button. so you can not send data when there is no connection
Button1.Enabled = False
End Sub
'Action - (when) Serial data recieved
Sub RunOnDataRecieved()
'This code will run when recieved data
' "ArduinoAnswer" is the data we recieved
MsgBox("I recieved the next text:" + Chr(13) + ArduinoAnswer, , MSGBoxName)
End Sub
'==================================================
'==================================================
'========== More Codes ==========
'==================================================
'==================================================
'Button - example button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Configure what to send
TextBoxInput.Text = "This is a Example"
'Send it
SerialSend()
End Sub
End Class
'
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Serial_Data
Dim MSGBoxName As String
Dim MSG As String
Dim myPort As Array
Dim ArduinoAnswer As String
Dim ButtonSelected As String
Dim PrefUSB As String
'Put under this line your own strings
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
'Action - (when) Starting up
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReloadUSB()
MSGBoxName = "[J] JelleWie"
ButtonConnectDisconnect.Select()
RunOnStartup()
End Sub
'Button - Reload USB
Private Sub ComboBoxPoort_USBIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxUSB.Click
ReloadUSB()
End Sub
'Code - Reload USB
Sub ReloadUSB()
PrefUSB = ComboBoxUSB.SelectedItem
ComboBoxUSB.Items.Clear()
On Error GoTo ErrHand
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBoxUSB.Items.AddRange(myPort)
ComboBoxUSB.SelectionStart.ToString()
ComboBoxUSB.SelectedItem = PrefUSB
If ComboBoxUSB.SelectedItem = "" Then
ComboBoxUSB.SelectedItem = "COM10"
ComboBoxUSB.SelectedItem = "COM9"
ComboBoxUSB.SelectedItem = "COM8"
ComboBoxUSB.SelectedItem = "COM7"
ComboBoxUSB.SelectedItem = "COM6"
ComboBoxUSB.SelectedItem = "COM5"
ComboBoxUSB.SelectedItem = "COM4"
ComboBoxUSB.SelectedItem = "COM3"
ComboBoxUSB.SelectedItem = "COM2"
ComboBoxUSB.SelectedItem = "COM1"
ComboBoxUSB.SelectedItem = "COM0"
End If
ErrHand:
End Sub
'Button - Send
Private Sub ButtonSend_Click(sender As Object, e As EventArgs) Handles ButtonSend.Click
SerialSend()
End Sub
'Button - Enter Pressed (Send)
Private Sub TextBoxInput_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxInput.KeyDown
If e.KeyCode = Keys.Enter Then
SerialSend()
End If
End Sub
'Code - Serial send
Sub SerialSend()
RunOnSend()
If ButtonConnectDisconnect.Text = "Connect" Then
ButtonConnectDisconnect.Select()
ButtonSelected = ActiveControl.Name
MSG = "Where te hell should I write to?!" + Chr(13) + "try connecting first!"
Dim result As Integer = MessageBox.Show(MSG, MSGBoxName, MessageBoxButtons.OKCancel)
If result = DialogResult.Cancel Then
ElseIf result = DialogResult.OK Then
MsgBox("If your the programmer, Here is your solution" + Chr(13) + Chr(13) + "Put 'Enabled' in the properties of " + ButtonSelected + " to False" + Chr(13) + Chr(13) + "ADD " + Chr(13) + ButtonSelected + ".Enabled = True" + Chr(13) + "TO 'Sub RunOnConnect()'" + Chr(13) + Chr(13) + "ADD " + Chr(13) + ButtonSelected + ".Enabled =False" + Chr(13) + "TO 'Sub RunOnDisconnect()'")
End If
Else
RichTextBoxOutput.Text = ""
On Error GoTo ErrHand
SerialPort1.Write(TextBoxInput.Text)
End If
Exit Sub
ErrHand:
MsgBox("You couldn't resist to remove the cable didn't you?", , MSGBoxName)
Disconnect()
End Sub
'Button - Connect and Disconnect
Private Sub ButtonButtonConnectDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConnectDisconnect.Click
If ButtonConnectDisconnect.Text = "Connect" Then
'Connect
Connect()
Else
'Disconnect
ButtonConnectDisconnect.Text = "Connect"
ButtonConnectDisconnect.ForeColor = Color.Green
Disconnect()
End If
End Sub
'Code - Connect
Sub Connect()
If ComboBoxUSB.Text <> "" Then
On Error GoTo ErrHand
SerialPort1.PortName = ComboBoxUSB.Text
SerialPort1.BaudRate = 9600
SerialPort1.Open()
ComboBoxUSB.Enabled = False
ButtonSend.Enabled = True
TextBoxInput.Enabled = True
ButtonConnectDisconnect.Text = "Disconnect"
ButtonConnectDisconnect.ForeColor = Color.Red
TextBoxInput.Select()
RunOnConnect()
Else
MsgBox("I can not connect to nothing! what where you thinking..." + Chr(13) + "Please give me a COM poort to connect to, before letting me try to connect to it", , MSGBoxName)
ReloadUSB()
End If
Exit Sub
ErrHand:
MsgBox("The COM Port your trying to use, does not seems to work annymore" + Chr(13) + "Did you remove the cable again?", , MSGBoxName)
ReloadUSB()
ButtonConnectDisconnect.Select()
End Sub
'Code - Disconnect
Sub Disconnect()
ComboBoxUSB.Enabled = True
ButtonSend.Enabled = False
TextBoxInput.Enabled = False
ButtonConnectDisconnect.Text = "Connect"
ButtonConnectDisconnect.ForeColor = Color.Green
ButtonConnectDisconnect.Select()
RunOnDisconnect()
On Error GoTo ErrHand
SerialPort1.Close()
Exit Sub
ErrHand:
ReloadUSB()
End Sub
'Action - Recieved serial data
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
System.Threading.Thread.Sleep(5)
ReceivedText(SerialPort1.ReadExisting())
End Sub
'Code - Recieved serial data
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
On Error GoTo ErrHand
If Me.RichTextBoxOutput.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBoxOutput.Text &= [text] 'append text
ArduinoAnswer = [text]
RunOnDataRecieved()
End If
Exit Sub
ErrHand:
MsgBox("Error while recieving data", , MSGBoxName)
End Sub
'==================================================
'==================================================
'========== Your Codes ==========
'==================================================
'==================================================
'Action - (when) sending data
Sub RunOnSend()
'Code to run befor sending data
End Sub
'Action - (when) starting up
Sub RunOnStartup()
'This code will run once starting up
MsgBox("Welcome to this awsome program", , MSGBoxName)
End Sub
'Action - (when) Connect
Sub RunOnConnect()
'This is to enable the Example button when there is a connection
Button1.Enabled = True
'You can use the next lines to send a commando to the Arduino when connecting, sso you can reset it
'TextBoxInput.Text = "Reset"
'SerialSend()
End Sub
'Action - (when) Disconnect
Sub RunOnDisconnect()
'This diables the Example button. so you can not send data when there is no connection
Button1.Enabled = False
End Sub
'Action - (when) Serial data recieved
Sub RunOnDataRecieved()
'This code will run when recieved data
' "ArduinoAnswer" is the data we recieved
MsgBox("I recieved the next text:" + Chr(13) + ArduinoAnswer, , MSGBoxName)
End Sub
'==================================================
'==================================================
'========== More Codes ==========
'==================================================
'==================================================
'Button - example button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Configure what to send
TextBoxInput.Text = "This is a Example"
'Send it
SerialSend()
End Sub
End Class