Pages: [1]   Go Down
Author Topic: Output of Arduino to VB.net  (Read 1812 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is it possible to send binary data or hex data from arduino to VB.net?...


for example..i want to send 00010(in bin) or 02h in hex to vb.net..i read somewhere that its just connecting the serial port to vb.net by i dont know where to start..what would be the code if i want to send data to vb.net?..


im a noobie in arduino can someone help me.. T__T
Logged

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

Serial.println (2);

That's about it.

On the Arduino
Code:
for (int i = 0; i < 100; i++) {
Serial.print (i, BYTE);
delay(100);
}


Then write some code on the PC to read the value and do something with it.

The following should give you a start.

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
  
        Do
            i = SerialPort1.ReadByte()
            TextBox1.Text = TextBox1.Text + "<" + Str(i) + ">"
            TextBox1.Refresh()
        Loop While i <> 100

    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 = "Com37"
        SerialPort1.Open()
    End Sub
End Class

As i is never = 100 this will loop forever.
______
Rob
« Last Edit: February 07, 2011, 09:21:25 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it worked..thanks to sir Graynomad..

heres what i did..

this is the code for arduino..

Code:
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {  //This will print bytes from 0-100
  for (int i = 0; i < 100; i++) {
Serial.print (i, BYTE);
delay(1000);
}     
}

while arduino is sending data to the computer..A vb.net software will be listening to what that port is sending (in my case COM5)

here are the codes for vb.net

Code:
Imports System.Threading
Imports System.IO
Imports System.Text
Imports System.IO.Ports

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Long
        Do
            i = SerialPort1.ReadByte
            RichTextBox1.Text = RichTextBox1.Text + "<" + Str(i) + ">"
            RichTextBox1.Refresh()
            TextBox1.Text = "Hellow World"
        Loop While i <> 100
         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 = "Com5"
        SerialPort1.Open()
    End Sub
   End Class

a serialport object is added to the form,with variable name "SerialPort1"..when the button1 is clicked..it will fetch the data that com5 is sending..(0-100 bytes based on the code above)..i dont really know how to convert that byte to string..

heres another problem i want to resolve..for example..i want to send "5" to com5(Serial.print (5, BIN)smiley-wink , when you open the serial monitor from arduino..it will output "101" which is the equivalent of 5 in Binary..but when fetch to the vb.net software..VB's output are 49,48,49....(49 is the ASCII code for 1 and 48 for 0)..how do i change that output from the software to BIN also..not 49,48,49 but instead 101..

pls correct me if im wrong..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The reason your form "Freeze" is because the UI (User Interface) is in the same thread as your code.
When your software is busy reading the serialport, the UI will "freeze", and because this is a loop in the code, it will just hang there untill you close it.

The solution for that is to put the loop code inside a new thread.

That being said, thank you for the code. Playing around with Ping))) Ultrasonic and VB now. smiley
Logged

Pages: [1]   Go Up
Jump to: