visual basic serial communication

Hi All.
I've been searching all over the web for this problem. I Can't find anny tutorials that works.

I have a Arduino Duemilanove, with a temp sensor, 2 led lights and two pushbuttoms.

What I wanna do, is to make a software for windows in visual basic.
And I need to communicate between Arduino and computer.

I've made a soft and managed to turn led on of by pressing buttoms on skreen, but I don't manage to show temp sensor value in visual basic.
Later I'd like to have several temp sensors showing on screen.

Annyone know how to read serial communication from arduino?
HEEEELP :-/

-Andy

  • Code *

  • arduino *
    #include <math.h>

int ledPin = 10; // the number of the LED pin
int buttonPin2 = 2; // the number of the pushbutton pin
int incomingByte;

double Thermistor(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = (Temp - 273.15); // Celcius
//Temp = ((Temp * 9.0)/ 5.0 + 32.0); // Fahrenheit
return Temp;
}

void setup() {
Serial.begin(19200);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
}

void loop(){
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == 'A') {
digitalWrite(ledPin, HIGH);
}
if (incomingByte == 'B') {
digitalWrite(ledPin, LOW);
}
}
Serial.println(int(Thermistor(analogRead(0))));
//delay(100);
}


  • Visual Basic *
    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
With SerialPort2
.Close()
'Set the serial port you want to use.
.PortName = "COM6" 'You MUST put 'COM' before the COM number.

'Set baud rate, parity, data bits, and stop bits.
.BaudRate = 19200
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One

'Set DTR and RTS
.DtrEnable = True
.RtsEnable = True

'Set number of bytes before DataRecieved event is raised.
.ReceivedBytesThreshold = 1

'Open serial port.
.Open()
End With
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort2.Write("A")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort2.Write("B")
End Sub

End Class

:smiley:

Temp  = log((([glow]10240000[/glow]/RawADC) - 10000));

RawADC is an int. 10000 is an int 10240000 is NOT an int. You should add UL to the end of the value, to tell the compiler that 10240000 is an unsigned long value, so it doesn't try to interpret it as an int.

       With SerialPort2

You have some object named SerialPort2. You call a bunch of methods on the object. If you press F1, select the Index button, if needed, select the drop down list for Filtered by:, select Visual Basic, and type SerialPort into the Look for: field, you can select "SerialPort class" to see all the methods that the SerialPort class has. There are several related to reading from the serial port.

There is an event, DataReceived, that you can register an event handler for. Whenever serial data arrives, your handler will be called, and can deal with that data.

Here is a very simple VB prog I wrote to test a connection to an Arduino.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Long
        Dim s As String

        If Not SerialPort1.IsOpen() Then
            SerialPort1.Open()
        End If

        TextBox1.Text = ""
        TextBox1.Refresh()
        Do While i < 50
            SerialPort1.Write("<hello>")
            s = SerialPort1.ReadLine()
            TextBox1.Text = TextBox1.Text + s + vbCrLf
            TextBox1.Refresh()
            i = i + 1
        Loop

    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If SerialPort1.IsOpen() Then
            SerialPort1.Close()
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "Com33"
        SerialPort1.Open()
    End Sub
End Class

Just have a button and testbox plus drag a Serialport object onto the form. You don't need code to set the serial parameteres, it's all in the parameters window.


Rob

Thanks guys.
I coult send value to Arduino, but I cant retrieve values to place in VB...

I tried the code you posted, no luck... I stil can't retrieve values.

-Andy

Have you verified that the Arduino is actually sending anything?

Are you using the correct COM #?

That VB snippet won't work out of the box with your Arduino code, it is just an example of how to do a simple Tx and Rx in VB.


Rob

Hi Graynomad.
I get the value when I use serial command in Arduino compiler.
The serialport I use is com6.

Its just the Rx I don't get... :-[

I get the value when I use serial command in Arduino compiler.

Meaning? What value? What "serial" command? Be specific, we haven't just gone through the same thought processes that you have. For example I've spent the last few minutes looking into isolated RS-485 chips :slight_smile:

Something like "I get the 'A' when using the serialRead() command" is more useful.

If that is the case then for the purposes of this post your loop is

void loop(){
Serial.println(int(Thermistor(analogRead(0))));
}

Change it to

Serial.println("x");

and see if you get the xs on the IDE serial monitor.

As I said,

Have you verified that the Arduino is actually sending anything?


Rob

Hi Graynomad
What I mean, is when I use arduino compiler and hit serial monitor, I get the temperature.

But when I ran the code you posted, I got some value from temp now. But the value from temp didn't change when I put heat on it.

If I understand it right, when I program arduino like "Serial.print"
This is what it sends to serial port?

Is there a way in VB to take spesific code from serial signal.
Like in this code I use one sensor sending value to VB.
If I had several sensors, like sensor 1-sensor6 could I split the code so I could place the value where I need it, and how?

Back to question.

The temp sensor I have, I'd like to show in a TextBox. Refreshing every 20 sec.
In my code showed above, how can I do this?

Best,
-Andy :slight_smile:

What I mean, is when I use arduino compiler and hit serial monitor, I get the temperature.

That's good, sounds like the Arduino is sending the data correctly.

But when I ran the code you posted,

Suitably modified I hope.

But the value from temp didn't change when I put heat on it.

Was the value reasonable, or rediculous like 5000 degrees.

If I understand it right, when I program arduino like "Serial.print"
This is what it sends to serial port?

Correct.

In my code showed above,

Set DTR and RTS
.DtrEnable = True
.RtsEnable = True

I don't think you want to enable the hardware handshaking, get rid of these lines. VB is probably waiting for DTR or RTS to be set.

how can I do this?

Something like

s = SerialPort1.ReadLine()
TextBox1.Text = TextBox1.Text + s + vbCrLf

Rob

Thanks Rob.
Now I've got some code to work.
Each time I push the buttom, I'll get some temp value :smiley:

Next I'll try to get the temp to show from startup and update in a periode of 30sec.

Again Rob. Thank you for helping me :slight_smile: