Comunicação serial entre Visual Studio 2015 e Arduino

Public Class Form1
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 
        If SerialArduino.IsOpen() Then
            SerialArduino.Close()
        Else
            SerialArduino.Open()
            SerialArduino.Write("d")
        End If
 
    End Sub
 
    Private Sub btnLigar_Click(sender As Object, e As EventArgs) Handles btnLigar.Click
        SerialArduino.Write("l")
    End Sub
 
    Private Sub btnDesligar_Click(sender As Object, e As EventArgs) Handles btnDesligar.Click
        SerialArduino.Write("d")
    End Sub
 
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        If SerialArduino.IsOpen() Then
            SerialArduino.Close()
        End If
    End Sub
End Class
(Visual Studio 2015)
Public Class Form1
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 
        If SerialArduino.IsOpen() Then
            SerialArduino.Close()
        Else
            SerialArduino.Open()
            SerialArduino.Write("d")
        End If
 
    End Sub
 
    Private Sub btnLigar_Click(sender As Object, e As EventArgs) Handles btnLigar.Click
        SerialArduino.Write("l")
    End Sub
 
    Private Sub btnDesligar_Click(sender As Object, e As EventArgs) Handles btnDesligar.Click
        SerialArduino.Write("d")
    End Sub
 
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        If SerialArduino.IsOpen() Then
            SerialArduino.Close()
        End If
    End Sub
End Class
 
(coloque o nome dos botões em : btnDesligar e btnLigar)
(coloque o nome do SerialPort para : SerialArduino)
 
 

(Arduino code)

  1. int led = 13;
  2.  
  3. void setup(){
  4.   Serial.begin(9600);
  5.   pinMode(led, OUTPUT);
  6. }
  7.  
  8. void loop(){
  9.    char ler = Serial.read();
  10.  
  11.  
  12.    if (ler =='l')
  13.    {
  14.     digitalWrite(led, HIGH);
  15.     Serial.println("led ligado");
  16.    }
  17.    else if (ler == 'd')
  18.    {
  19.     digitalWrite(led,LOW);
  20.     Serial.println("led deligado");
  21.    }
  22. }