Thank you for visiting → Compete For Nothing.com ← If you find anything useful leave me a comment and share. Interested in Rapberry Pi, Arduino, C#, Unity3D, Game Creation, VB.NET, learning and most important of all Sharing!.
Close Notification
Open Notification

VB.net Serial Communication Arduino

VB.net Serial Communication Arduino
GARD Pro Not Registered

Serial communication is easy using Visual Studio VB.NET or SharpDevelop.

GARD Pro Not Registered

This is a simple application written in SharpDevelop. It is very easy
to interface with serial Ports on windows. This code should work
on VB.NET.

GARD Pro Not Registered

Now for the code…

GARD Pro Not Registered
' Created by SharpDevelop.
' User: Warren
' Date: 3/2/2012
' Time: 12:15 PM
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'

Public Partial Class MainForm
	Dim WithEvents SerialPort As New IO.Ports.SerialPort

	Public Sub New()
		' The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()

		'
		' TODO : Add constructor code after InitializeComponents
		'
	End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Try
			GetSerialPortNames()
		Catch
			msgbox("No ports seem to be connected.")
		End Try

		'load combo box with baudrates
		Dim BaudRates() As String = {"300", "1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "57600", "115200"}
		cmboBaudrate.Items.AddRange(BaudRates)

		'start combobox with first available value
		cmboComm.SelectedIndex = 0
		cmboBaudrate.SelectedIndex = 0
    End Sub

    Private Sub ConnectSerial()
    	If btnConnect.Text = "Disconnect" Then
    		Try
	    		SerialPort.Close()
	    		btnConnect.Text = "Connect"
				cmboComm.Enabled = True
				cmboBaudrate.Enabled = True
				btnRefresh.Enabled = True
	    		Exit Sub
    		Catch
    			msgbox("Error..lost port connection??")
    		End Try
    	End If

    	Try
			SerialPort.BaudRate = cmboBaudrate.SelectedItem.ToString
		  	SerialPort.PortName = cmboComm.SelectedItem.ToString
		  	SerialPort.Open()

 		  	If SerialPort.IsOpen Then
				btnConnect.Text = "Disconnect"
				cmboComm.Enabled = False
				cmboBaudrate.Enabled = False
				btnRefresh.Enabled = False
				Exit Sub
		  	End If
    	Catch
    		SerialPort.Close()
    	End Try
    End Sub

    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myD1 As New myMethodDelegate(AddressOf myShowStringMethod)

    Sub myShowStringMethod(ByVal myString As String)
	  SerialText.AppendText(myString)
    End Sub

    Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
	  Dim str As String = SerialPort.ReadExisting()
	  Invoke(myD1, str)
    End Sub

	Sub GetSerialPortNames()
	    ' Show all available COM ports.
	    For Each sp As String In My.Computer.Ports.SerialPortNames
	        cmboComm.Items.Add(sp)
	    Next
	End Sub

	Sub BtnConnectClick(sender As Object, e As EventArgs)
		SerialText.text = Nothing
		ConnectSerial()
	End Sub

	Sub BtnRefreshClick(sender As Object, e As EventArgs)
		cmboComm.Items.Clear()
		GetSerialPortNames()
	End Sub

	Sub BtnAboutClick(sender As Object, e As EventArgs)
		msgbox("FREE SOFTWARE from www.competefornothing.com")
	End Sub

	Sub BtnSendClick(sender As Object, e As EventArgs)
		If SerialPort.IsOpen Then
			SerialPort.Write(txtSend.Text)
		Else
			ConnectSerial()
			SerialPort.Write(txtSend.Text)
		End If
	End Sub

	Sub TxtSendKeyDown(sender As Object, e As KeyEventArgs)
		If e.KeyCode = Keys.Enter Then
    	e.SuppressKeyPress = True
	    	If SerialPort.IsOpen Then
	    		SerialPort.Write(txtSend.Text)
			Else
				ConnectSerial()
				SerialPort.Write(txtSend.Text)

			End If
			txtSend.Clear()
		End If
	End Sub
End Class

Now some code for the Arduino to test the send and receive.

//Code from arduino.cc

int incomingByte = 0;
void setup(){
   Serial.begin(9600); 
}

void loop(){
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

Dogecoin
Donate Dogecoins: DGkJRWQCyBUeXbenYz18LRN9X67aora9nz Whats This?

6 Comments

  1. CompeteForNothing » VB.net Serial Communication Arduino | Arduino Geeks | Scoop.it · August 4, 2012 Reply

    […] Download Arduino-Tracker Source Code. This is a simple application written in SharpDevelop. It is very easy to interface with serial Ports on windows. This code should work on VB.NET also. Now for the code… ' Created by …  […]

  2. Angelyca · August 25, 2012 Reply

    If you can’t manage to get the entrie circuit flexible, a compromise would to make the strips connecting the boards to be flexible. Key Factor: Since its flexible, the number 1 priority should be durability. These boards should last as long as the normal Arduino boards, or your customers will feel cheated. To do this, you may need to compromise on flexibility so that perhaps each square is stiff, but the ribbon connecting them is flexible.Potential Uses: I think I would put it on a RC battery inside of an RC car. They have pretty small dimensions, but since this board is so small, I would be able to squeeze it where any other board wouldn’t fit. I would then connect the driving servo and the motor controller (ESC) to it and drive it around I would then put a review up on my site, showing the flexibility and durability of the newest Arduino.

  3. VB.net Serial Communication Arduino | CompeteForNothing | Arduino, vb.net, Unity3D, FPV | Scoop.it · January 6, 2013 Reply

    […] Clear() GetSerialPortNames() End Sub Sub BtnAboutClick(sender As Object, e As EventArgs) msgbox("FREE SOFTWARE from http://www.competefornothing.com") End Sub Sub BtnSendClick(sender As Object, e As EventArgs) If …  […]

  4. Grant · January 6, 2013 Reply

    How to make the different color of the character display depending on the speed of the character. I want to show fast arriving characters in blue and slow arrive characters in red. Characters with a medium speed should be green.

  5. josh · June 12, 2014 Reply

    Hello

    My name is Josh from NJ ,USA

    i had downloaded your program ( Arduino Serial Communication)

    it is works fine when receive the data ,, but sending the data is not work ( i found the problem ,it is the CR but i do not know how to make program )

    i need your help for my little project , i have willing to pay your help , please contact to my email

    thanks

Leave a Reply to CompeteForNothing » VB.net Serial Communication Arduino | Arduino Geeks | Scoop.it Cancel reply