Pages: [1]   Go Down
Author Topic: visual basic serial communication  (Read 11224 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
« Last Edit: January 19, 2011, 05:47:32 am by cbrandy » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46234
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.

Code:
      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.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...   :-[
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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  smiley

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,

Quote
Have you verified that the Arduino is actually sending anything?

______
Rob



Logged

Rob Gray aka the GRAYnomad www.robgray.com

Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
But when I ran the code you posted,
Suitably modified I hope.

Quote
But the value from temp didn't change when I put heat on it.
Was the value reasonable, or rediculous like 5000 degrees.

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

Quote
In my code showed above,

Code:
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.

Quote
how can I do this?
Something like

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

______
Rob

« Last Edit: January 19, 2011, 12:06:10 pm by graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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   smiley
Logged

Pages: [1]   Go Up
Jump to: