Serial Communication Between Arduino and Visual Basic (Source Code)

6
PLATE 1 IN I/O Submitted By: Podunas, Elthon John S. Aquino, Renz Marion P. Maloloy-on, Alvien . !a"as, Billy #ilson E. $alenzuela, %ary $. A&ero, Aldri'h !. Ba'us, Mar' A.

description

The source code in Visual Basic and Arduino

Transcript of Serial Communication Between Arduino and Visual Basic (Source Code)

PLATE 1 IN I/O

Submitted By:Podunas, Elthon John S.Aquino, Renz Marion P.Maloloy-on, Alvien G.Lawas, Billy Wilson E.Valenzuela, Kary V.Ajero, Aldrich L.Bacus, Marc A.Visual Basic 2010 Source Code

Imports SystemImports System.IO.Ports

Public Class Form1

Dim comPORT As String Dim receivedData As String = ""

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load comPORT = "" For Each sp As String In My.Computer.Ports.SerialPortNames comPort_ComboBox.Items.Add(sp) Next End Sub

Private Sub comPort_ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged If (comPort_ComboBox.SelectedItem "") Then comPORT = comPort_ComboBox.SelectedItem End If End Sub

Private Sub connect_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles connect_BTN.Click If (connect_BTN.Text = "Connect") Then If (comPORT "") Then SerialPort1.Close() SerialPort1.PortName = comPORT 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! SerialPort1.ReadTimeout = 10000 SerialPort1.Open() connect_BTN.Text = "Disconnect" Timer1.Enabled = True DC.Text = "COM Port: Connected" Else MsgBox("Select a COM port first") End If Else SerialPort1.Close() connect_BTN.Text = "Connect" Timer1.Enabled = False DC.Text = "COM Port: Disconnected" End If End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick receivedData = ReceiveSerialData() RichTextBox1.Text &= receivedData End Sub

Function ReceiveSerialData() As String Dim Incoming As String Try Incoming = SerialPort1.ReadExisting() If Incoming Is Nothing Then Return "nothing" & vbCrLf Else Return Incoming End If Catch ex As TimeoutException Return "Error: Serial Port read timed out." End Try

End Function

Private Sub clear_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles clear_BTN.Click RichTextBox1.Text = "" End Sub

Private Sub LED_On_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LED_On.Click Try SerialPort1.Write("1") LED.Text = "LED: On" Catch ex As Exception MsgBox("Select a COM port first") End Try End Sub

Private Sub LED_Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LED_Off.Click Try SerialPort1.Write("0") LED.Text = "LED: Off" Catch ex As Exception MsgBox("Select a COM port first") End Try End Sub

Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click If connect_BTN.Text = "Connect" Then MsgBox("Select a COM port first") Else If Send2Arduino.Text = "ON" Then SerialPort1.Write("1") LED.Text = "LED: On" ElseIf Send2Arduino.Text = "Om" Then SerialPort1.Write("1") LED.Text = "LED: On" ElseIf Send2Arduino.Text = "on" Then SerialPort1.Write("1") LED.Text = "LED: On" ElseIf Send2Arduino.Text = "OFF" Then SerialPort1.Write("0") LED.Text = "LED: Off" ElseIf Send2Arduino.Text = "Off" Then SerialPort1.Write("0") LED.Text = "LED: Off" ElseIf Send2Arduino.Text = "off" Then SerialPort1.Write("0") LED.Text = "LED: Off" Else : MsgBox("Illegal Command") End If End If End SubEnd Class

Arduino Source Codeint buttonPin = 11;const int ledPin = 12; int buttonState = 0; int x=0;

void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); Serial.begin(9600); x=0; }void loop() {buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { x++; if (x==1) { Serial.println("Plate"); delay(1000); } if (x==2) { Serial.println("in"); delay(1000); } if (x==3) { Serial.println("I/O"); delay(1000); } if (x==4) { Serial.println("Submitted to:"); delay(1000); } if (x==4) { Serial.println("Engineer Angelia"); delay(1000); } if (x>5) { Serial.println("Gwapo mo!"); delay(1000); } }int val; if (Serial.available()) { delay(100); while (Serial.available() > 0) { val=Serial.read(); if (val=='1') {digitalWrite(12,HIGH); } else if (val=='0'){digitalWrite(12,LOW); } } } }