Controlling Arduino Using VB.NET

Hello there! I just want to ask how to control multiple pins using VB.NET? I have hard time dealing with it,i use the method i saw on google which control LED using VB.net but when i revise it and manipulate it,on the VB.NET side,it gets error already.
My purpose is to light 4 LED's using different pins, then i can turn on or off them individually.

I just want to ask how to control multiple pins using VB.NET?

One at a time.

I have hard time dealing with it

So do I. Such a useless language, despite decades of trying to make it better.

i use the method i saw on google which control LED using VB.net but when i revise it and manipulate it,on the VB.NET side,it gets error already.

I'm going to go out on a limb here, and guess that you are doing something (besides not posting code) wrong.

My purpose is to light 4 LED's using different pins, then i can turn on or off them individually.

Permission is granted.

Codes for VB.Net
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com03"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
End Sub

Private Sub Led1On_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub

Private Sub Led1Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End Sub

Private Sub Led2On_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("2")
SerialPort1.Close()
End Sub

Private Sub Led2Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("3")
SerialPort1.Close()
End Sub
Private Sub Led3On_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("4")
SerialPort1.Close()
End Sub

Private Sub Led3Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("5")
SerialPort1.Close()
End Sub
Private Sub Led4On_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("6")
SerialPort1.Close()
End Sub

Private Sub Led4Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("7")
SerialPort1.Close()
End Sub
End Class

Arduino Codes:

int ledPin1 = 10; // the number of the LED pin
int ledPin2 = 11;
int ledPin3 = 12;
int ledPin4 = 13;

void setup() {
Serial.begin(9600); // set serial speed
pinMode(ledPin1, OUTPUT); // set LED1 as output
digitalWrite(ledPin1, LOW); //turn off LED1
pinMode(ledPin2, OUTPUT); // set LED2 as output
digitalWrite(ledPin2, LOW); //turn off LED2
pinMode(ledPin3, OUTPUT); // set LED3 as output
digitalWrite(ledPin3, LOW); //turn off LED3
pinMode(ledPin4, OUTPUT); // set LED4 as output
digitalWrite(ledPin4, LOW); //turn off LED4
}

void loop(){
led1();
led2();
led3();
led4();
}
void led1();
{
while (Serial.available() == 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number

if (val == 1) { // test for command 1 then turn on LED
digitalWrite(ledPin, HIGH); // turn on LED1
}
else if (val == 0) // test for command 0 then turn off LED
{
digitalWrite(ledPin, LOW); // turn off LED1
}
else // if not one of above command, do nothing
{
//val = val;
}
Serial.println(val);
Serial.flush(); // clear serial port
}

void led2();
{
while (Serial.available() == 0);
int val = Serial.read() - '0';

if (val == 2) {
digitalWrite(ledPin, HIGH);
}
else if (val == 3)
{
digitalWrite(ledPin, LOW);
}
else
{
}
Serial.println(val);
Serial.flush(); // clear serial port
}

void led3();
{
while (Serial.available() == 0);
int val = Serial.read() - '0';

if (val == 4)
{
digitalWrite(ledPin, HIGH);
}
else if (val == 5)
{
digitalWrite(ledPin, LOW);
}
else
{
//val = val;
}
Serial.flush();
}

void led4();
{
while (Serial.available() == 0);
int val = Serial.read() - '0';

if (val == 6)
{
digitalWrite(ledPin, HIGH);
}
else if (val == 7)
{
digitalWrite(ledPin, LOW);
}
else
{
//val = val;
}
Serial.flush();
}

Opening the serial port resets the Arduino. You send it data and close the serial port before the Arduino is ready to accept data. I'm not surprised that the VB code accomplishes nothing.

In each of the ledN() functions, you block until there is serial data to read. Since the VB app has closed the port, that will never happen, so it is not surprising that the Arduino code does nothing.

For a moment, let's assume that you fix the VB code to open the port once (its name is NOR "com03", by the way). The loop() function will be called, and will call led1(), which will wait for serial data to arrive. Now, suppose that the user of the VB app clicks the Led3Off icon, and the VB app sends "5". That gets converted to 5, which is not 0 and is not 1. So, led1() does nothing except return. Then, led2() gets called, and waits for the user to press another icon in the VB app. Suppose that the now press the Led1On icon, so the VB app sends a 0. That is not a 2 or a 3, so led2() does nothing except return.

You need to read the serial data in ONE place, and then determine which function to call.

Blocking until all pending output data has been sent is pointless. That is what flush() does.

As Paul says above the way you have the code is not really a good solution. Have a look at Arduino and Visual Basic: Receiving Data From the Arduino and Visual Basic: Controlling an Arduino.
These are very simply examples and once you have things working you can make them more efficient.

Thank you very much! it will be a big help :slight_smile: