Komunikasi Serial Arduino Dengan Vb

Komunikasi Serial Arduino Dengan Vb 6,8/10 1518 votes
-->

Jul 18, 2012  Kali ini saya akan membagikan sedikit pengetahuan tentang komunikasi serial sederhana, dimana kita akan mengkomunikasikan mikrokontroler (Arduino) dengan Komputer menggunakan software Microsoft Visual Basic 6.0.Pada proyek ini kita membutuhkan beberapa peralatan, yaitu. Komunikasi serial adalah komunikasi yang pengiriman datanya per-bit secara berurutan dan bergantian. Komunikasi ini mempunyai suatu kelebihan yaitu hanya membutuhkan satu jalur dan kabel yang sedikit dibandingkan dengan komunikasi paralel.

Komunikasi Serial Arduino Dengan Vb6

This topic describes how to use My.Computer.Ports to receive strings from the computer's serial ports in Visual Basic.

Arduino

To receive strings from the serial port

Komunikasi Serial Arduino Dengan VbDengan
  1. Initialize the return string. Bollywood hd movies torrent.

  2. Determine which serial port should provide the strings. This example assumes it is COM1.

  3. Use the My.Computer.Ports.OpenSerialPort method to obtain a reference to the port. For more information, see OpenSerialPort.

    The Try..Catch..Finally block allows the application to close the serial port even if it generates an exception. All code that manipulates the serial port should appear within this block.

  4. Create a Do loop for reading lines of text until no more lines are available.

  5. Use the ReadLine() method to read the next available line of text from the serial port.

  6. Use an If statement to determine if the ReadLine() method returns Nothing (which means no more text is available). If it does return Nothing, exit the Do loop.

  7. Add an Else block to the If statement to handle the case if the string is actually read. The block appends the string from the serial port to the return string.

  8. Return the string.

Example

This code example is also available as an IntelliSense code snippet. 30 bore pistol license in pakistan prices. In the code snippet picker, it is located in Connectivity and Networking. For more information, see Code Snippets.

Compiling the Code

This example assumes the computer is using COM1.

Robust Programming

This example assumes the computer is using COM1. For more flexibility, the code should allow the user to select the desired serial port from a list of available ports. For more information, see How to: Show Available Serial Ports.

This example uses a Try..Catch..Finally block to make sure that the application closes the port and to catch any timeout exceptions. For more information, see Try..Catch..Finally Statement.

See also

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 -----------------

Posted on