Menu Horisontal

Tampilkan postingan dengan label Arduino. Tampilkan semua postingan
Tampilkan postingan dengan label Arduino. Tampilkan semua postingan

Kamis, 07 Maret 2013

How to Communicate to the Arduino in Visual Basic .NET

Sumber : http://social.technet.microsoft.com/wiki/contents/articles/14194.how-to-communicate-to-the-arduino-in-visual-basic-net.aspx

Communicating to the Arduino in the .NET platform is pretty much straightforward: The Arduino uses a virtual serial port to allow programs to be written onto it, but we can also use this port to get and send data to and from the Arduino.
Materials:

  • Arduino or Arduino Compatible Board (UNO, Mega, Etc.)
  • Compatible USB Cable to Connect the Arduino to the Host Computer
  • The Arduino Software (Available Here )
  • VB.NET 2010 or 2012 (Available Here )
I am assuming that you already have a simple knowledge of the .NET and Arduino (software & hardware) platforms. If not, there are many tutorials on the internet to help get you started. Let's begin by putting the program on the Arduino.


Step 1: Adding a Program to Send Data From the Arduino
Let's start by opening up the Arduino software, and copying/pasting in the following code:

void setup() {              
    Serial.begin(9600); 
}
 
//Sends the Number 1234 Over the Serial Port Once Every Second
void loop() {
Serial.write(1234)
delay(1000)
}

This program opens up a serial port, and sends the number string, 1234, over the port once every second in an infinite loop. The number '1234' has no special meaning, you could place any number there in place of it. Then plug in your Arduino board and upload the code to it. Leave it plugged in.

Step 2: Creating the .NET Program
Creating the .NET code in VB.NET is actually quite simple and easy to understand. Open a new VB.NET console program, clear all of the code, and paste in the following:

Module Module1
    Sub Main()
        Dim userRespond
        Dim fetchedData
        Dim writeString
        Console.WriteLine("Type R to Read Serial Port.")
        userRespond = Console.ReadLine
        If userRespond = "R" Or userRespond = "r" Then
            Dim port As New System.IO.Ports.SerialPort
            port.PortName = "COM4"
            port.Open()
            port.BaudRate = 9600
            fetchedData = port.ReadByte
            Console.WriteLine(fetchedData)
            Console.ReadLine()
  Else
            Console.WriteLine("Not a Recognized Command.")
        End If
    End Sub
End Module

This code should be fairly simple. It opens a new console window, then it asks the user if they want to get the data from the port, next (if they said yes), it opens up a new COM port and gets the data, finally it writes it to the console. You may need to change COM4 to the correct port for your Arduino. It lists what port Arduino is on in the Arduino software bottom right corner. You can not write a new program to the Arduino when this software is running, for it gives an error that says that the COM port is busy.

Step 3: Testing
To use this program, be sure your Arduino is plugged in, and it is running the code from step 1. Now, run the program from above and type in R. It may take a second (the Arduino only sends it once per second), but the number 1234 will come up! If you change the number 1234 in the Arduino code to "hello" and upload it and run the .NET program, it will only give you the decimal value for a lower-case h. This is because this program can only read one byte at a time. See if you can change it up to read the whole word and convert it to unicode! Post your inventions!

Senin, 31 Desember 2012

Tutorial Starduino: Mengontrol 4 Relay Dari Komputer Via Komunikasi Serial RS232 Dengan Perantaraan Starduino Board

Sumber : http://telinks.wordpress.com/category/softwareprogramming/vb-net/
Sumber : http://teknikelektrolinks.com/
Pada tutorial Starduino kali ini, kita akan membahas tentang bagaimana mengontrol relay dari komputer melalui port komunikasi serial RS232 dengan menggunakan Starduino Board.
Gambar di samping adalah foto pengujian Starduino Board untuk mengontrol relay dan menampilkan statusnya di LCD 16×2.
Starduino Board, Modul Relay DRV04, dan Modul LCD 16×2 dapat dipesan melalui email ke info@teknikelektrolinks.com atau SMS ke 081231784018 atau BB ke 24B15B64.
Untuk pembelian paket Starduino + Modul DRV04 akan mendapatkan source code sketch Arduino pengontrolan relay melalui port serial + source code program VB.NET lengkap dengan Installer VB.NET 2005 Express Edition.
Untuk memudahkan eksperimen,  berikut adalah tabel perintah pengontrolan relay.
Perintah Aksi
‘A’ RELAY 1 ON
‘1’ RELAY 1 OFF
‘B’ RELAY 2 ON
‘2’ RELAY 2 OFF
‘C’ RELAY 3 ON
‘3’ RELAY 3 OFF
‘D’ RELAY 4 ON
‘4’ RELAY 5 OFF
Berikut adalah looping utama program yang berfungsi menerima data dari port serial dan mengeksekusi perintah sesuai data yang diterima dari komputer.
if (Serial.available())
{
  char inChar = Serial.read();
  if (inChar==’A')
    digitalWrite(RL1, HIGH);
  else if (inChar==’1′)
    digitalWrite(RL1, LOW);
  else if (inChar==’B')
    digitalWrite(RL2, HIGH);
  else if (inChar==’2′)
    digitalWrite(RL2, LOW);
  else if (inChar==’C')
    digitalWrite(RL3, HIGH);
  else if (inChar==’3′)
    digitalWrite(RL3, LOW);
  else if (inChar==’D')
    digitalWrite(RL4, HIGH);
  else if (inChar==’4′)
    digitalWrite(RL4, LOW);
}
Fungsi Serial.available() berfungsi mengembalikan jumlah karakter yang ada di dalam buffer port serial. Jika fungsi Serial.available() bernilai 0 (nol), maka tidak ada karakter dalam buffer. Jika fungsi Serial.available() > 0, maka ada karakter yang diterima dalam buffer port serial.
Fungsi Serial.read() berfungsi membaca/mengambil data karakter dari buffer port serial. Perintah char inChar = Serial.read() berfungsi mendeklarasikan variable inChar sebagai penampung data karakter hasil pembacaan fungsi Serial.read().
Selanjutnya, karakter dibandingkan dengan konstanta karakter sesuai dengan tabel perintah di atas untuk mengetahui perintah apa yang harus dieksekusi oleh kontroler.
Jika inChar==’A’, maka aktifkan RELAY-1 dan jika inChar==’1’, maka matikan RELAY-1. Pembandingan yang sama juga berlaku untuk perintah ‘B’, ‘2’, ‘C’, ‘3’, ‘D’, dan ‘4’.
Setelah program di upload ke Starduino, maka pengujian dapat dilakukan dengan mengirimkan karakter ‘A’, ‘1’, ‘B’, dan seterusnya dari komputer.
Untuk mengirimkan data secara manual bisa menggunakan terminal bawaan IDE Arduino atau program terminal seperti Hyperterminal.
Dapat juga pengontrolan dilakukan dengan menggunakan program aplikasi yang dapat Anda peroleh jika membeli paket Starduino Board + Modul DRV04. Berikut adalah tampilan program aplikasi Starduino+DRV04 yang dibuat menggunakan VB.NET 2005 Express Edition.
starduino_drv04_vbnet
Demikian sekelumit penjelasan mengenai pengontrolan relay dari komputer melalui port esrial RS232 dengan perantaraan Starduino Board.
Selamat belajar dan selamat berkarya!