Contoh Code Arduino & VB.Net mengirim hasil 4 counter ke PC dgn Serial Port

Pada contoh berikut ini Arduino mengrim data  counter 4 digit dari 4 switch (a,b,c dan d)  ke PC .misal :  “a1234b2345c2222d7777”  .

Code Arduino

/*

The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
#include <LiquidCrystal.h>
#define delaylcd 20

int counter1=0;
int counter2=0;
int counter3=0;
int counter4=0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensor1 = 6;
const int sensor2 = 7;
const int sensor3 = 8;
const int sensor4 = 9;

// variables will change:
int sensor1State = 0;
int sensor2State = 0;
int sensor3State = 0;
int sensor4State = 0;

void setup()
{
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);

lcd.begin(16, 2);
Serial.begin(9600);

lcd.setCursor(0, 0);
lcd.print(“counter1 – 2 – 3 – 4”);
}

void loop()
{
//===========================
// sensor1State=0;
delay(delaylcd);

if (digitalRead(sensor1) == LOW)
{
delay(delaylcd);
counter1++;
lcd.setCursor(0, 1);
lcd.print(counter1);
Serial.print(‘a’);
Serial.print(counter1);
Serial.print(counter1/1000 %10 );
Serial.print( counter1/100 %10);
Serial.print( counter1/10 %10);
Serial.print( counter1/1 %10);
lagi:
delay(delaylcd);
if(digitalRead(sensor1) == LOW)
{ goto lagi;}
delay(delaylcd);

}
//======================================
//===========================

if (digitalRead(sensor2) == LOW)
{
delay(delaylcd);
counter2++;
lcd.setCursor(4, 1);
lcd.print(counter2);
Serial.print(‘b’);
Serial.print(counter2/1000 %10);
Serial.print( counter2/100 %10);
Serial.print( counter2/10 %10);
Serial.print( counter2/1 %10);
lagi2:
if(digitalRead(sensor2) == LOW)
{ goto lagi2;}
delay(delaylcd);

}
//======================================

//===========================
if (digitalRead(sensor3) == LOW)
{
delay(delaylcd);
counter3++;
lcd.setCursor(9, 1);
lcd.print(counter3);
Serial.print(‘c’);
Serial.print(counter3/1000 %10);
Serial.print( counter3/100 %10);
Serial.print( counter3/10 %10);
Serial.print( counter3/1 %10 );
lagi3:
if(digitalRead(sensor3) == LOW)
{ goto lagi3;}
delay(delaylcd);

}
//======================================
//=========================================
//===========================
if (digitalRead(sensor4) == LOW)
{
delay(delaylcd);
counter4++;
lcd.setCursor(13, 1);
lcd.print(counter4);
Serial.print(‘d’);
Serial.print(counter4/1000 %10 );
Serial.print( counter4/100 %10 );
Serial.print( counter4/10 %10 );
Serial.print( counter4/1 %10);
lagi4:
if(digitalRead(sensor4) == LOW)
{ goto lagi4;}
delay(delaylcd);

}
//======================================
} //end of loop

Kode VB Net

berikut potongan kode untukmnerima data dengan VB.net

Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived
datastring = COMPort.ReadExisting

Me.Invoke(New MethodInvoker(AddressOf TampilkanData)) ‘ Start “Display” on the UI thread

End Sub

Private Sub TampilkanData()
If datastring.Substring(0, 1) = “a” Then
TextBox1.Text = datastring.Substring(1, 4)
End If

If datastring.Substring(0, 1) = “b” Then
TextBox2.Text = datastring.Substring(1, 4)
End If
If datastring.Substring(0, 1) = “c” Then
TextBox3.Text = datastring.Substring(1, 4)
End If
If datastring.Substring(0, 1) = “d” Then
TextBox4.Text = datastring.Substring(1, 4)
End If
txtReceived.AppendText(datastring)
End Sub

cara lain untuk memisahkan string selain dengan substring:

Imports vb = Microsoft.VisualBasic

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim datasplit() As String
Dim dataserial As String = “123#456 #789#777”
datasplit = vb.Split(dataserial, “#”)
TextBox1.Text = datasplit(0)
TextBox2.Text = datasplit(1)
TextBox3.Text = datasplit(2)
TextBox4.Text = datasplit(3)
End Sub
End Class

hasil darikode tsb   texbox1 berisi  123 ,  textbox2 berisi 456 dst…

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 08/04/2015, in Daftar isi and tagged . Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: