Komunikasi Serial Arduino Vb

  1. Komunikasi Serial Vb6 Dengan Arduino
  2. Komunikasi Serial Arduino Dengan Vb
  3. Komunikasi Serial Arduino Dengan Vb.net

I know this has been done in the past a few times so here's one more. This is a skeletal Visual Basic 2010 and Arduino Sketch that I mixed together to test the PC to Arduino Uno connection via serial. It simply turns LED 13 on or off.
The Visual Basic 2010 code assumes you have Form1 with 2 buttons Button1 and Button2 and SerialPort1 controls. Button1 sends a 1 and Button2 sends a 0 to the serial port COM10 (change this to match your PC to Arduino port setting)
See attached photo of my simple form design.
The Arduino Uno Sketch code simply waits and reads the serial port. If it see 1 it will turn PIN 13 on and if it sees 0 it will turn PIN 13 off. If you have an LED on PIN 13, you can turn it on and off. On the Arduino Uno, PIN 13 is attached to a an on-board LED.
I used COM10 as a serial port but you can (and must) change it to match your Arduino serial port.
The purpose of this code is to simplify explanation of how to connect VB to Arduino. You can add error processing and more intelligence based on your particular needs.
Make sure you drag the Serial Port control icon from the Toolbox onto your form. It should have the name SerialPort1
WARNING: On my PC I had to close the Arduino IDE Serial Monitor window while runing the VB program, else I run into all sorts of error message about COM port access denied and the program will fail.
You can download Visual Basic Express 2010 for free from Microsoft
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express
'------------ START OF VB 2010 CODE -----------------
' NOTE: I am using COM10 so you need to change the Visual Basic code to match your COM port
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = 'com10' 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write('1')
SerialPort1.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write('0')
SerialPort1.Close()
End Sub
End Class
'------------ END OF VB 2010 CODE -----------------
//------------- START OF ARDUINO SKETCH -----------------
//
// Mixed by: Hazim Bitar
// Based on: Science Guy 14 youTube tutorial http://youtu.be/g0pSfyXOXj8
int ledPin = 13; // the number of the LED pin
void setup() {
Serial.begin(9600); // set serial speed
pinMode(ledPin, OUTPUT); // set LED as output
digitalWrite(ledPin, LOW); //turn off LED
}
void loop(){
while (Serial.available() 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number
if (val 1) { // test for command 1 then turn on LED
Serial.println('LED on');
digitalWrite(ledPin, HIGH); // turn on LED
}
else if (val 0) // test for command 0 then turn off LED
{
Serial.println('LED OFF');
digitalWrite(ledPin, LOW); // turn off LED
}
else // if not one of above command, do nothing
{
//val = val;
}
Serial.println(val);
Serial.flush(); // clear serial port
}
//------------- END OF ARDUINO SKETCH -----------------

The serial port on our computers are with us for quite sometime now. It is more favored than the parallel port for interfacing with the outside world because it require lesser wires and can go farther distance and can even be used with newer technologies such as Bluetooth using its serial port capability. In this tutorial is a quick start guide in communicating with the serial port using VB.net without any previous knowledge.

Komunikasi yang digunakan yaitu komunikasi serial dengan memanfaatkan ComPort yang ada pada VB, kemudian data yang dikirim oleh SFE-Duino ke VB akan. Read VB comport; Untuk program Arduino mengirim data serial ke VB: void setup() { // Open serial communications and wait for port to open:. Home Komunikasi Serial Port Mengontrol Indikator (LED) Jalankan aplikasi anda, dengan mengatur port serial sesuai dengan port Arduino yang terdeteksi pada computer. Arduino serial port led. Video embeddedUsing Visual Basic 2010 to Control Arduino Uno by techbitar in arduino. PIN 13 is attached to a an onboard.

Before we start, VB.net 2010 must be installed. The installer can be freely downloaded from Microsoft.

Start Visual Basic 2010 Express and you will be prompted with the Start Page Window

Select on New Project and select Windows Form Application and give the Project a name, in this case I named it Serial Port Interface.

A blank form will be displayed named Form1. This is the main form which will contain all other controls.

Click on the toolbox to show the controls, you may want to click on the pin to disable the autohide feature

Click on the form and change its name to frmMain (for easier identification specially when adding more forms). Also, change its name, in this case VB.net Terminal – Philrobotics. This name will be the one that will be displayed on the title bar. Since this program is only a small program, we can disable the Maximize button by setting MaximizeBox value on the form’s property to false.

Let’s start to add some controls to our form. From the common Controls, add (2) two labels, (2) two combo box and (2) two buttons. Change the text of the first label to “Com Port:” and the second one to “Baud Rate:”. For the combo box change its name to cmbPort and cmbBaud for the purposes of easier identification when we will add the codes to our controls. Also, change the text of the two buttons, the first to “Connect” and the second to “Disconnect” and change their name to btnConnect and btnDisconnect. As you can see, I use cmb and btn prefix to refer to combo box and button same reason as above, for easier identification.

Next, from the Containers add two Group box and change its text to “Transmit Data” and “Received Data”. Inside the Transmit Data group box, add a textbox and a button. Change the name of the textbox to txtTransmit and the button to btnSend, also change its text to Send. On the Received Data group box, add a Rich Text Box and change its name to rtbReceived. Lastly and the most important, add the SerialPort from the components.

Now, we come to the next part, adding the instructions to our program. To start coding double click the form or press F7 to view the code. To add a code to individual controls just go back to the designer view or press Shift+F7 and double click a control to add code into it.

For instance if we double click on the Send button on the designer view the Code Editor will take us to

Komunikasi serial arduino dengan vb.net

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

On this Part is where we enter our codes.

End Sub

To complete our program put the following codes to the code editor:

Komunikasi Serial Vb6 Dengan Arduino

‘Serial Port Interfacing with VB.net 2010 Express Edition
‘Copyright (C) 2010 Richard Myrick T. Arellaga

‘This program is free software: you can redistribute it and/or modify
‘it under the terms of the GNU General Public License as published by
‘the Free Software Foundation, either version 3 of the License, or
‘(at your option) any later version.

‘This program is distributed in the hope that it will be useful,
‘but WITHOUT ANY WARRANTY; without even the implied warranty of
‘MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
‘GNU General Public License for more details.

‘ You should have received a copy of the GNU General Public License
‘ along with this program. If not, see <http://www.gnu.org/licenses/&gt;.

Komunikasi Serial Arduino Dengan Vb

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
Dim myPort As Array ‘COM Ports detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) ‘Added to prevent threading errors during receiveing of data

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() ‘Get all com ports available
cmbBaud.Items.Add(9600) ‘Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)

For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) ‘Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) ‘Set cmbBaud text to the first Baud rate on the list

btnDisconnect.Enabled = False ‘Initially Disconnect Button is Disabled

End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text ‘Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = cmbBaud.Text ‘Set Baud rate to the selected value on

‘Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 ‘Open our serial port
SerialPort1.Open()

btnConnect.Enabled = False ‘Disable Connect button
btnDisconnect.Enabled = True ‘and Enable Disconnect button

End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close() ‘Close our Serial Port

btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
SerialPort1.Write(txtTransmit.Text & vbCr) ‘The text contained in the txtText will be sent to the serial port as ascii
‘plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
End Sub

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) ‘Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
‘compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub

Komunikasi Serial Arduino Dengan Vb.net

Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text ‘pop a message box to user if he is changing ports
Else ‘without disconnecting first.
MsgBox(”Valid only if port is Closed”, vbCritical)
End If
End Sub

Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text ‘pop a message box to user if he is changing baud rate
Else ‘without disconnecting first.
MsgBox(”Valid only if port is Closed”, vbCritical)
End If
End Sub
End Class

After putting all the codes we are now ready to compile our project. To do this go to, Debug -> Build SerialPortInterface. If there are no errors, It will indicate Build Succeeded at the status bar.

The executable file of the compiled project is usually located at the binRelease of the project folder.

To test if our program is working, short the TX and RX pin of your serial port and press F5 or click on the green triangle. Each data that we send must also be received by our program. In my setup I’m using a USB-to-Serial Converter and is allocated at COM18.

Posted :