vb arduino please help

all i needed to do was send "OK" from vb to arduino and then arduino would start sending data and vb would receive it and display it

but not working...
here is the code i used

int ledPin = 13;
String usbnumber ;
String start = "OK";
void setup() 
{
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
    
}

void loop() 
{
     
  if (Serial.available() >0) 
  {
        
    usbnumber = Serial.read(); /*reading data*/
   
  }
  
   if (usbnumber == start  ) /* cheching if vb sent "OK" start = "OK" */
    {
      Serial.println("1234567"); /*send 1234567 to vb*/
       delay(10);       
   digitalWrite (ledPin,HIGH ); /* turn on led as indicator */
   
    }
   
   in vb side 
[code]
Private Sub Command1_Click()
' Buffer to hold input string
Dim Instring As String
' Use COM3.
MSComm1.CommPort = 3
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
Dim data As String
data = "OK"
MSComm1.Output = data


Dim readdata As String


readdata = MSComm1.Input


MsgBox readdata

MSComm1.PortOpen = False

End Sub

anybody please help

}
[/code]

String usbnumber ;
String start = "OK";

These are important.

    usbnumber = Serial.read(); /*reading data*/

Look up the Serial::read function. What does it return? Hint: It does NOT return a string.

   if (usbnumber == start  ) /* cheching if vb sent "OK" start = "OK" */

Look at the == operator in the String class. What does it do? Not what you are trying to do here.

Even if it did compare two strings, usbnumber never contains more than one character, so it would never equal "OK".

i had been messing around with the code for all day long and finally got one up and running

but still one problem

i transmit SEND in arduino

and receive say SEN one time S onetime SEND one time in vb

(pops up randomly)

the code is

Private Sub Form_Load()
MSComm1.CommPort = 3
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.EOFEnable = True

MSComm1.InputLen = 0
MSComm1.RThreshold = 1
' Open the port.
MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()
Dim inbuffer() As Byte 'Declare an array of bytes
Dim b() As Byte

Dim i As Long
Dim s As String

Select Case Me.MSComm1.CommEvent
Case comEvReceive
ReDim inbuffer(Me.MSComm1.InBufferCount) 'Specify the size of the array. InBuffercount gives the number of characters in the InputBuffer

inbuffer = Me.MSComm1.Input 'Read the InputBuffer

For i = 1 To 20000
i = i + 1
Next i
 
For i = 0 To UBound(inbuffer) 'Ubound(inbuffer) gives the upper bound of the array, which is equal to the number of characters in the InputBuffer

Me.txtreceive.Text = Me.txtreceive.Text & Chr$(inbuffer(i)) 'TxtReceive is a text box

Next i

s = StrConv(inbuffer, vbUnicode)
End Select

MsgBox s

MSComm1.PortOpen = False

End Sub

thanks a lot thanks a lot

i figured it out

thanks

it lay in a single line

MSComm1.RThreshold = 4

changing that to 4 did the trick

mscomm event fires only after 4 characters (SEND)

works great and feels great

thanks

okay then started phase 2

i would like to send "y" in vb so that arduino would turn on led

but i dont know how arduino interprets the received "y" from vb

{note: i had confirmed that vb is sending string only using MSComm1.output="y" }

but my arduino is not interpreting it correctly please help

this is the code i used

int i;
char rec[7];
char str3 = "Y";
void establishContact();
void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial.flush();
  pinMode(13, OUTPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until receiver responds 
           
 }


void loop()
{
  // if we get a valid byte, read analog ins:
   for (i=0;i<Serial.available();i++)
    {
    rec[i]=Serial.read();
     }
  delay(10);
        if (rec==str3)
     {
        Serial.print("firs");
        digitalWrite(13,HIGH);
     }
    
}

void establishContact()
{
  //while (Serial.available() <= 0) 
  //{
    Serial.print("SEND"); 
    //Serial.println(byte(26));
    // send an initial string
    delay(300);
    
  //}
}
char str3 = "Y";

str3 is a character, not a string. You should be using single quotes, not double quotes.

   for (i=0;i<Serial.available();i++)
    {
    rec[i]=Serial.read();
     }

Suppose that initially there are 2 characters to read. On the first pass through the loop, i is 0, which is less than 2, so a character is read, and stored. Then, i is incremented to 1.

On the next pass through loop, Serial.available() returns 1, because one of the characters was read, and no more have arrived. 1 is not less than 1, so the loop ends.

  delay(10);

Why?

        if (rec==str3)

An array will never equal a character, no matter what is stored in the array or the character.

thanks a lot for all your help i finally got eberything lined up and working
i understood that serial.read returns bytes not integer and that to compare strings we need strcmp()
thanks

the code which FINALLY worked is

Private Sub Form_Load()
MSComm1.CommPort = 3
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.EOFEnable = True

MSComm1.InputLen = 0
MSComm1.RThreshold = 4  ' IMPORTANT change this to change the number of characters to be received
MSComm1.SThreshold = 3

' Open the port.
MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()
Dim inbuffer() As Byte 'Declare an array of bytes
Dim b() As Byte

Dim i As Long
Dim s As String

Select Case Me.MSComm1.CommEvent
Case comEvReceive
ReDim inbuffer(Me.MSComm1.InBufferCount) 'Specify the size of the array. InBuffercount gives the number of characters in the InputBuffer

inbuffer = Me.MSComm1.Input 'Read the InputBuffer


 
For i = 0 To UBound(inbuffer) 'Ubound(inbuffer) gives the upper bound of the array, which is equal to the number of characters in the InputBuffer

Me.txtreceive.Text = Me.txtreceive.Text & Chr$(inbuffer(i)) 'TxtReceive is a text box

Next i

s = StrConv(inbuffer, vbUnicode)
End Select

MsgBox s
Dim string2 As String
string2 = "1234"
If StrComp(s, string2, vbTextCompare) = 0 Then
MsgBox s
MSComm1.Output = "YES"
End
Else
MSComm1.PortOpen = False
End If

End Sub

and in the arduino side

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int ledpin =13;
void establishContact();
void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial.flush();
  pinMode(ledpin, OUTPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until receiver responds 
           
 }


void loop()
{
  // if we get a valid byte, read analog ins:
     while(Serial.available() > 0) // Don't read unless
                                                  // there you know there is data
   {
        //digitalWrite(ledpin,HIGH);
            if(index < 19) // One less than the size of the array
       {
           
             inChar = Serial.read(); // Read a character
           inData[index] = inChar; // Store it
           index++; // Increment where to write next
           inData[index] = '\0'; // Null terminate the string
       }
   }
    if (strcmp(inData,"YES")==0)
     {
        digitalWrite(ledpin,HIGH);
        Serial.print(inData);
     }
         
}

void establishContact()
{
//  while (Serial.available() <= 0) 
  //{
    Serial.print("1234"); 
        // send an initial string
    delay(300);
    
 // }
}

thank you

I am trying to do a similar thing with a robot i got from robot shop with the Arduino micro-controller and i want toy use VB 2010 to control it and give read outs from it's sensors to VB to be displayed on the screen. Do i need any special objects on the form or any special code in the arduino ,like firmata, or can i just write the code as you have it and accomplish the same thing?

Add a SerialPort object to your form then use functions like SerialPort1.Write() etc.

A simple VB.2008 example

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

Note that this is simple test code I used for a particular problem, your while loop will vary.


Rob

problem is i'm using VB 2010 some of code is probably different.

problem is i'm using VB 2010 some of code is probably different.

That's possible but I wouldn't expect huge changes, give it a go.


Rob

i used visual studio 2010 ( it had vb6) i think that you too are takling about the same .... but don't know for sure

I am documenting all the stages of my project in www.frankey.webs.com . thank you all . please continue to guide me. thanks all.

Hi cinnamon,

You've gotta reduce the size of that tiny graphic, it's over 3meg bytes, it took so long to load I was about to kill the page because I thought there was no content.

Not everyone has unlimited plans, even these days :slight_smile:


Rob

k i never thought that. will change that now