Simple VB.net application

This is just a simple visual basic express 2010 application I made borrowing some code from other forum posts. All it does is turn on an LED when a button is pressed, but it took me a long time to figure this out and with this I could make some more complicated applications :stuck_out_tongue:

Visual basic code:

Imports System.IO.Ports


Public Class Form1

    Public s As New SerialPort


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        s.Close()
        s.PortName = "com4"  'will need to change to your port number
        s.BaudRate = 9600
        s.DataBits = 8
        s.Parity = Parity.None
        s.StopBits = StopBits.One
        s.Handshake = Handshake.None
        s.Encoding = System.Text.Encoding.Default 'very important!
        s.Open()
        s.RtsEnable = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "On" Then
            s.Write(Chr(50))
            Button1.Text = "Off"
        Else
            s.Write(Chr(10))
            Button1.Text = "On"
        End If
    End Sub
End Class

Arduino Code:

int SerialValue = 0;

void setup(){
  
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  
}

void loop(){
  SerialValue = Serial.read();
  if(SerialValue == 50){
    digitalWrite(13, HIGH);
  }
  if(SerialValue == 10){
    digitalWrite(13, LOW);
  }
}

Please do feel free to pick holes in my code, let me know of bad practise or unnecessary bits of code :slight_smile:

And thats how we talk to visual basic.

Much easier than using Fermata.

http://www.codeproject.com/KB/system/ArduinoVB.aspx

there would be a nice lib for that: Arduino <> Firmata <> Visual Basic .NET

but your approach is a lot simpler if you only want to make some simple on/off action :slight_smile:

Is there any reason why I would get a 'The port is closed' error? I've chosen the correct port(com4).

Cheers

Check you've included s.Open() in your code? Also make sure that nothing else is using the serial port such as the Arduino Serial Monitor and try using a different USB port, if none of that works then I don't know :stuck_out_tongue:

Check Visual Arduino For .NET Developers.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290689533

I wasn't aware of that, looks fairly cool!