VIsual basic with arduino

Dear all,

I am trying to display the data get from arduino on Visual serial monitor.

I have created text box . and here my code. It not displaying anything.

Arduino CODE:

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  Serial.println("welcome");
  
  delay(1000);
}

visual basic

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' baudrate = 9600'
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.PortName = "COM3"

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim s As String
        s = SerialPort1.ReadLine()
        TextBox1.Text = s


    End Sub
End Class

My guess is that VB is a lot like VC#?

Then you need to put the

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim s As String
        s = SerialPort1.ReadLine()
        TextBox1.Text = s

in a tick timer. Right now it just reads it once during startup and then nothing.

Also, inside the timer, put:

            if (SerialPort1.BytesToRead > 0)
            {
        Dim s As String
        s = SerialPort1.ReadLine()
        TextBox1.Text = s
}

there. Now it just checks if there is data in the buffer. If there isn't any, it skips the reading part.

TextBox1.Text = sAfter doing this do you need to update or refresh the screen or textbox ? Do you see anything if you debug.print s ?

I can t see any thing here.
http://www.multiwingspan.co.uk/arduino.php?page=vb1
i tried this code but i didint get any thing also.It dont allow me to select port and set baudrate . if enter manually it can take input. But not receiving any data

Imports System.IO.Ports


Public Class Form1


    Dim WithEvents sp As New SerialPort



    Private Sub GetSerialPortNames()
        For Each sport As String In My.Computer.Ports.SerialPortNames
            cmbPort.Items.Add(sport)
        Next
    End Sub

    Sub ShowString(ByVal myString As String)
        textln.AppendText(myString)
    End Sub


    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myDelegate As New myMethodDelegate(AddressOf ShowString)











    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Try
            sp.BaudRate = cmbBaud.SelectedItem.ToString
            sp.PortName = cmbport.SelectedItem.ToString
            sp.Open()
            If sp.IsOpen Then
                btnConnect.Visible = False
                cmbport.Enabled = False
                cmbBaud.Enabled = False
                btnDisconnect.Visible = True
            End If
        Catch
            sp.Close()
        End Try
    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        Try
            sp.Close()
            btnConnect.Visible = True
            btnDisconnect.Visible = False
            cmbport.Enabled = True
            cmbBaud.Enabled = True
            Exit Sub
        Catch
            MessageBox.Show("Some kind of problem.")
        End Try
    End Sub




    Private Sub cmbport_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbport.SelectedIndexChanged
        Dim BaudRates() As String = {"300", "1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "57600", "115200"}
        cmbBaud.Items.AddRange(BaudRates)
        cmbBaud.SelectedIndex = 4
        Try
            GetSerialPortNames()
            cmbport.SelectedIndex = 0
        Catch
            MsgBox("No ports connected.")
        End Try
    End Sub



    Private Sub textln_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textln.TextChanged
        Dim str As String = sp.ReadExisting()
        Invoke(myDelegate, str)
    End Sub
End Class

What makes your textBox code read the serial port?

When, and how often, does that happen?

...R

As i said I am new to visual basic , I am trying to get some data for learning so i can proceed further. Once i make ensure serial communication work. I can modify that code.

@Robin 2
My code i pasted on top. I am printing just hello. I wanted to display it on text dialogue box.
I have followed step by step process given by multiwing
http://www.multiwingspan.co.uk/arduino.php?page=vb1
But i am facing some problem.

What does it means??

What makes your textBox code read the serial port? 

When, and how often, does that happen?

I have tried with processing . I dont how to create Serial data block like VB and display there.
I have 2 perameter

  1. I have sensor that read analog 0~5v and Converted to current value . which is serial printed on either VB or processing,
  2. I have digital pin if check button led must glow and if uncheck it must get off.

To understand or implimenting i need help.
I have Tried with Serial Monitor itself i.e Sending data over Serial monitor and control It . IT working fine for me.
I can control the led , off led.
Below code working fine.

int led=2;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(led,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
  
  while(Serial.available()==0);
  int val=Serial.read()-'0';
  if (val==1)
  {
    Serial.println("Led is ON");
    digitalWrite(led,HIGH);
    
  }else
  {
   Serial.println("Led is OFF");
    digitalWrite(led,LOW);
  }
 
  
}
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Shared _continue As Boolean


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "COM3"

        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Handshake = IO.Ports.Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!

    End Sub


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



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



End Class

Arduino CODE

int ledPin = 2; // the number of the LED pin

void setup() {
Serial.begin(9600); // set serial speed
pinMode(ledPin, OUTPUT); // set LED as output
digitalWrite(ledPin, LOW); //turn off LED
}


void loop(){
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
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // turn on LED
}
else if (val == 0) // test for command 0 then turn off LED
{
Serial.println("LED OFF");
digitalWrite(ledPin, LOW); // turn off LED
}
else // if not one of above command, do nothing
{
//val = val;
Serial.println("Wrong command");
}
Serial.println(val);
Serial.flush(); // clear serial port
}

For below code i am getting error. please let me know what i need to do. I got everything configure right manner

AMPS-N:
For below code i am getting error. please let me know what i need to do. I got everything configure right manner

Change         SerialPort1.PortName = "COM3"
to

        SerialPort1.PortName = 'COM3'

maybe?

I already tried it. It gives expression expected error

Posting pictures of your code sucks.

The code you posted above the picture shows that you are assigning COM3 as the port to be opened later. It is not clear why the exception mentions COM10.

But, opening the serial port resets the Arduino. You don't wait any time for the Arduino to reset before writing to it. Then, you close the serial port, resetting the Arduino again.

Knock that crap off. Add a button to your form to open the serial port, and one to close it (or reuse the same button after changing its title). The _Click_Click handlers (what WERE you thinking?) should NOT open or close the port.

@paul

I am reseting arduino. Whenever i run program . SO that Arduino get reset properly and Detect right port.
Arduino Serial Monitor displaying properly.

If you already tried these kind of stuff . Please give me example code.That i can tried,Since i am learner in visual basic i have posted my code based on google example i found it.

At present i am learnt how to create symbols text and label Them. Only problem is how to use them.

I Need small example where i can communicate with Serial data. take simple example of 1 printing Hello itself,

If use text line How can display and connect COMPort.If i create read message in txtline i should get hello for every 1S.

can you give simple example for this.

I Need small example where i can communicate with Serial data. take simple example of 1 printing Hello itself,

The problem is that you need a fair amount of code to deal with choosing the serial port, choosing the baud rate, opening the serial port, closing the serial port, writing to the serial port, and reading from the serial port.

It is not as simple as having one button to do all that. Resetting the Arduino is NOT a good idea. It is going to happen whenever you open the serial port and whenever you close the serial port, but it should NOT happen every time you write to the serial port.

If use text line How can display and connect COMPort.

The use of a text field and opening and closing the serial port are unrelated. You need a button to open the serial port (in the _Click callback) and one to close it.

If i create read message in txtline i should get hello for every 1S.

The serial port class has callbacks that can be implemented to handle arriving serial data in a number of ways. Reading serial data using a timer is not the correct way to get data.

Look at the properties tab when the SerialPort1 object is selected. Select the lightning bolt to see the events that you can supply handlers for.

Arduino Code

int Analog_Pin=5;
int newaverage;
float Output_Current;


#define RELAY1  7
#define MAX_TRIP_COUNT  5
float Current_Limit=0.60;
static int Trip_Count=0;
static int Tripped_Flag=1;

#include <avr/wdt.h>



void TakeReading()
{
  newaverage = analogRead(A5);
  // Serial.print("count:");
  //Serial.println(newaverage);
  Output_Current = 0.0336666666667*newaverage - 17.17; 
}

void Relay_Activate()
{
  for (unsigned long start = millis(); millis() - start < 10000;)
  {
    digitalWrite(RELAY1,HIGH);

    //Serial.println("RELAY GOT TRIPPED");
    Tripped_Flag=0;
    Trip_Count=0;
  }

}

void  Relay_Deactivate()
{
  digitalWrite(RELAY1,LOW);

  //  Serial.println("RELAY NOT TRIPPED");
  Tripped_Flag=1;

}

void Relay_Intialize()
{
  //Serial.println("RELAY GOT INTIALISE....");
  digitalWrite(RELAY1,LOW);

}


void Chk_Relay_Tripped()
{
  if(Output_Current>Current_Limit)
  {
    Trip_Count=Trip_Count+1; 
    // Serial.print("Trip_Count:");
    //Serial.println(Trip_Count);
    if(Trip_Count>=5)
    {
      Relay_Activate();
    } 
  }
  else
  {
    if(Trip_Count<=0)
    {
      Trip_Count=0;
    }
    else
    {
      Trip_Count=Trip_Count-1;
    } 

    Relay_Deactivate();
    // Serial.print("Trip_Count:");
    // Serial.println(Trip_Count);

  }

}

void setup() {
  Serial.begin(9600); // set serial speed

  pinMode(RELAY1,OUTPUT); 
  wdt_enable(WDTO_8S); 
  Relay_Intialize();

  pinMode(Analog_Pin,INPUT);

}




void loop(){

  wdt_reset();
  TakeReading();
  //Serial.println(Output_Current);
  Chk_Relay_Tripped();

  Serial.println(Output_Current);


  if (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) { 
    digitalWrite(RELAY1, HIGH); // turn on LED
  }
  else if (val == 0) // test for command 0 then turn off LED
  {

    digitalWrite(RELAY1, LOW); // turn off LED
  }


 // Serial.flush(); // clear serial port
  delay(1000);
}

VB code

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Shared _continue As Boolean


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        SerialPort1.PortName = "COM3"

        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.Handshake = IO.Ports.Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!

    End Sub


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



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




    Private Sub Current_Read_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Open()
        ' Dim str As String'
        ' str = SerialPort1.ReadExisting'
        Current_Read.Text = SerialPort1.ReadLine()


        SerialPort1.Close()
    End Sub


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "ABCD"
    End Sub
    
End Class

This code is working fine on VB script . I can control the LED . Problem i am facing is getting analog value. I got analog value one at time . after that in serial monitor it changing But Not in VB . Is anyone give reason here. What mistake I am doing

I can control the LED

Resetting the Arduino every time is not "controlling the LED".

    Private Sub Current_Read_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

What triggers this callback? Arrival of data on the not opened serial port? Not likely.

When you add a button to open the port, and one to close it, and stop opening and closing it all the time, I'll be back.

Can you elobate it . Where i need call this function once Arduino or VB script.

What triggers this callback? Arrival of data on the not opened serial port? Not likely.

My data coming from Serial ny arduino. Keep accumulating. What i should here to do it work

Where i need call this function once Arduino or VB script.

You need to call the .Open() method ONCE in the VB program.

My data coming from Serial ny arduino. Keep accumulating. What i should here to do it work

You don't have an open port, so no serial data is coming from the Arduino.

This demo shows how to communicate with an Arduino using Python. The process will be the same in Visual basic.

In Reply #5 you asked what I meant by

What makes your textBox code read the serial port?
When, and how often, does that happen?

If you don't understand my question you don't know enough about how Visual Basic works. And It is so long since I used it that I can't advise you now.

...R

What makes your textBox code read the serial port?

When, and how often, does that happen?

I already said my data send serially every 1S . Whatever there in Serial Buffer must be read Displayed .At current i am just sending only current value.

AMPS-N:

What makes your textBox code read the serial port?

When, and how often, does that happen?

I already said my data send serially every 1S . Whatever there in Serial Buffer must be read Displayed .At current i am just sending only current value.

You send the data every second, but what causes the VB code to read it ?