interfacing the arduino to vb6

hi!

i need some help on resolving my project which is all about interfacing the arduino to vb6..my project is somewhat like a push button..
we have five terminals the one terminal is connected to the source and the other four terminal is connected to four inputs..
when the first button hits the led will turn on same to other three buttons..the led is just an indicator that indicates that the first button or terminal is being hit and so on..
each terminal/button has a led for representation..and now, i want to simulate this to vb6 where the simulation could be done by a shape that will fill color whenever the terminal is being hit or whenever the leds are on..

could you help me in doing this..?. :frowning:

yours,
glenmars

it seems that i want that the vb6 will receives the data from arduino and let the vb6 to simulate this.

am hoping that anybody could help me in resolving this..

your's,
glenmars

it seems that i want that the vb6 will receives the data from arduino

So, what code is running on the Arduino, sending serial data?

and let the vb6 to simulate this.

So, what code is running on the PC to read the serial data?

See http://arduino.cc/forum/index.php/topic,104164.0.html

code for arduino:

void setup()
{                
  pinMode(9, OUTPUT); 
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(7, OUTPUT);
  Serial.begin (9600);
}
void loop()
{

  int a = digitalRead (2);
  int b = digitalRead (3);
  int c = digitalRead (4);
  int d = digitalRead (5); 
  //digitalWrite (7,LOW);
  {
    if (a == HIGH)
    {
      digitalWrite (9, HIGH);
      Serial.println ("Red is the color of Led/ The pressures value is 20psi");
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);

    }
    else if (a == LOW)
    {
      digitalWrite (9, LOW);

    }

    if (b == HIGH)
    {
      digitalWrite (10, HIGH);
      Serial.println ("Green is the color of Led/The pressures value is 40psi");
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
      
    }
    else if(b==LOW)
    {
      digitalWrite (10, LOW);
     
    }

    if (c == HIGH)
    {
      digitalWrite (11, HIGH);
      Serial.println ("Blue is the color of Led/The pressures value is 60psi");
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
      
    }
    else if (c==LOW)
    {
      digitalWrite (11, LOW);
     
    }

    if (d == HIGH)
    {
      digitalWrite (12, HIGH);
      Serial.println ("Yellow is the color of Led/The pressures value is 80psi");
      delay (200);
      digitalWrite (7, HIGH);

    }
    else if (d==LOW)
    {
      digitalWrite (12, LOW);
 
    }

  }
}

here is my code in vb6:

Option Explicit

Private Sub Form_Load()
  With MSComm1
        If .PortOpen Then .PortOpen = False
        .CommPort = 1
        .Settings = "19200,N,8,1"
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True
  End With
  With Shape1
    Shape1.Shape = 3    'Circle
    Shape1.Height = 555
    Shape1.Width = 555
    Shape1.FillColor = vbWhite
    Shape1.FillStyle = 0 'Solid
  End With
End Sub

Private Sub LEDOn(col As Long)
End Sub
Shape1.FillColor = col
End Sub

Private Sub LEDOff()
End Sub
Shape1.FillColor = vbWhite
End Sub


Private Sub MSComm1_OnComm()
Dim strData As String
Static strBuffer As String
Dim strWords() As String
Dim intPos As Integer
Dim boComplete As Boolean
Select Case MSComm1.CommEvent
    Case comEvReceive
        strData = MSComm1.Input
        strBuffer = strBuffer & strData
        Do
            intPos = InStr(strBuffer, vbCrLf)
            If intPos > 0 Then
                strWords = Split(strBuffer, " ")
                Select Case UCase(strWords(1))
                    Case "a"
                        Call LEDOn(vbRed)
                    Case "b"
                        Call LEDOn(vbGreen)
                    Case "c"
                        Call LEDOn(vbBlue)
                    Case "d"
                        Call LEDOn(vbYellow)
                    Case Else
                       Call LEDOff
                End Select
                If intPos + 2 < Len(strBuffer) Then
                    strBuffer = Mid(strBuffer, intPos + 2)
                Else
                    strBuffer = ""
                    boComplete = True
                End If
            Else
                boComplete = True
            End If
        Loop Until boComplete = True
End Select
End Sub

i thought the serial.print will automatically sends data to vb...
fortunately, I'm just a newbie in arduino and vb..

please help me, THANKS!!

yours truly:
Glennmars

if you could write it in vb10 then there are a lot of examples on this forums, but you'll have to switch for that ofcourse...

i can't help you in vb6, but first of all, set your baudrate the same in vb as on the arduino to have to working (both 9600 or 19200)

                Select Case UCase(strWords(1))
                    Case "a"
                        Call LEDOn(vbRed)
                    Case "b"
                        Call LEDOn(vbGreen)
                    Case "c"
                        Call LEDOn(vbBlue)
                    Case "d"
                        Call LEDOn(vbYellow)
                    Case Else
                       Call LEDOff
                End Select

I can't see how any case will ever be selected.
The Arduino sends data:

      Serial.println ("Red is the color of Led/ The pressures value is 20psi");

The stream of data is split into a collection of Strings ("Red", "is", etc.). The first letter of each word is converted to upper case ('R', 'I', etc.). There are then cases for some lower case letters. After UCase() does its thing, there are no lower case letters in existence. So, why are all the cases lower case letters?

wow PaulS..thanks for the correction!...your such a smart :wink:

hehe, i'm just a newbie in arduino and specifically on vb6..i'm so excited to try it..thanks a lot! :slight_smile:

by the way my code goes like this:

Select Case UCase(strWords(1))
                    Case "a"
                        Call LEDOn(vbRed)
                    Case "b"
                        Call LEDOn(vbGreen)
                    Case "c"
                        Call LEDOn(vbBlue)
                    Case "d"
                        Call LEDOn(vbYellow)
                    Case Else
                       Call LEDOff
                End Select

while in arduino the characters that i used to sends to serial is: ("Red is the color of Led/ The pressures value is 20psi")
in viewpoint of this, the < Case "a"> i my vb code should be change into < Case "Red"> is this what you mean?..if not, will you please site some samples in doing this..

thanks!

i my vb code should be change into < Case "Red"> is this what you mean?

"Red" is not a letter, so no. You are switching on the first letter in each word, after converting that letter to upper case. So, the case values should be 'R', 'G', 'B'. 'Y'. etc.

Even better would be to have the Arduino send something easier to parse.

ah okay so my case would be like this <Case "R"> right..?.

so in my arduino code, it would be better if i only sends a less character..is this what you mean?..
what about if i only sends "1" not in form of character.?,or else if i'm going to change it instead of < ("Red is the color of Led/ The pressures value is 20psi"); > i will change this into one letter..?..what do you think is the easiest way..?

ah okay so my case would be like this <Case "R"> right..?.

No. There is a world of difference between 'R' and "R". The sooner you learn the difference, the better.

what do you think is the easiest way.

I'd be sending something like if the pressure value was important/going to be used. The < and > define the start and end of a packet. The letter defines the LED color, and the numeric digits are the pressure reading.

The < and > define the start and end of a packet

  1. where of the code this < and > should be inserted Pauls..?.i figure what you mean but i was doubt if where this must be place in my code..

  2. another thing, i find hard in running the arduino and vb because it seems both of them are using the same commport..this is the process when i use to run them.. i run the arduino first then afterwards the vb.?.is this right?..because if i'm going to run the vb first before the arduino it prompts < Serial port "COM1" is already in use. Try quitting any programs that may be using it >

  1. another thing, i find hard in running the arduino and vb because it seems both of them are using the same commport..this is the process when i use to run them.. i run the arduino first then afterwards the vb.?.is this right?..because if i'm going to run the vb first before the arduino it prompts < Serial port "COM1" is already in use. Try quitting any programs that may be using it >

michael_x suggestions:
You are able to get Serial Monitor tool in the Arduino IDE to run and see output coming from Arduino, Right?
Then close it and start/debug a VB6 application which opens the same COM port and has an event handler on incoming data.
Once you see something arriving, you're done (from the arduino point of view) ...

:slight_smile: i apply what michael_x said..but still nothing happens..my vb doesn't receive any data or filling the circle a color.. :disappointed_relieved:

below are the edited codes:

arduino code:

void setup()
{                
  pinMode(9, OUTPUT); 
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(7, OUTPUT);
  Serial.begin (9600);
}
void loop()
{

  int a = digitalRead (2);
  int b = digitalRead (3);
  int c = digitalRead (4);
  int d = digitalRead (5); 
  //digitalWrite (7,LOW);
  {
    if (a == HIGH)
    {
      digitalWrite (9, HIGH);
      Serial.println ("R5");
      delay (200);
      digitalWrite (7, HIGH);
      delay (200);
      digitalWrite (7, LOW);

    }
    else if (a == LOW)
    {
      digitalWrite (9, LOW);
      //Serial.println("Waay Contact");

    }

    if (b == HIGH)
    {
      digitalWrite (10, HIGH);
      Serial.println ("G10");
      delay (200);
      digitalWrite (7, HIGH);
      delay (200);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (200);
      digitalWrite (7, LOW);
      
    }
    else if(b == LOW)
    {
      digitalWrite (10, LOW);
      //Serial.println("Waay Contact");
    }

    if (c == HIGH)
    {
      digitalWrite (11, HIGH);
      Serial.println ("B15");
      delay (200);
      digitalWrite (7, HIGH);
      delay (200);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (200);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (200);
      digitalWrite (7, LOW);
      
    }
    else if (c == LOW)
    {
      digitalWrite (11, LOW);
      //Serial.println("Waay Contact");
    }

    if (d == HIGH)
    {
      digitalWrite (12, HIGH);
      Serial.println ("Y20");
      delay (200);
      digitalWrite (7, HIGH);
      
    }
    else if (d == LOW)
    {
      digitalWrite (12, LOW);
    }

  }
}

VB6 code:

Option Explicit

Private Sub Form_Load()
  With MSComm1
        If .PortOpen Then .PortOpen = False
        .CommPort = 1
        .Settings = "9600,N,8,1"
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True
  End With
  With Shape1
    Shape1.Shape = 3    'Circle
    Shape1.Height = 555
    Shape1.Width = 555
    Shape1.FillColor = vbWhite
    Shape1.FillStyle = 0 'Solid
  End With
End Sub

Private Sub LEDOn(col As Long)
End Sub
Shape1.FillColor = col
End Sub

Private Sub LEDOff()
End Sub
Shape1.FillColor = vbWhite
End Sub
Private Sub MSComm1_OnComm()
Dim strData As String
Static strBuffer As String
Dim strWords() As String
Dim intPos As Integer
Dim boComplete As Boolean
Select Case MSComm1.CommEvent
    Case comEvReceive
        strData = MSComm1.Input
        strBuffer = strBuffer & strData
        Do
            intPos = InStr(strBuffer, vbCrLf)
            If intPos > 0 Then
                strWords = Split(strBuffer, " ")
                Select Case UCase(strWords(1))
                    Case "#R5"
                        Call LEDOn(vbRed)
                    Case "G10"
                        Call LEDOn(vbGreen)
                    Case "#B15"
                        Call LEDOn(vbBlue)
                    Case "#Y20"
                        Call LEDOn(vbYellow)
                    Case Else
                       Call LEDOff
                End Select
                If intPos + 2 < Len(strBuffer) Then
                    strBuffer = Mid(strBuffer, intPos + 2)
                Else
                    strBuffer = ""
                    boComplete = True
                End If
            Else
                boComplete = True
            End If
        Loop Until boComplete = True
End Select
End Sub

< you are free to edit my code >
thanks..

Lets walk through one case. You press the switch connected to pin 2 on the Arduino. The Arduino sends "R5" to the serial port, along with a carriage return and line feed, and flashes the LED on pin 7.

While not running the VB app, open the Serial Monitor. You should see R5 in the serial monitor window, on a new line, and the LED on pin 7 should flash every time you press the switch connected to pin 2. Do both of those things happen?

Presuming that they do, close the Serial Monitor, and run the VB application.

When the Arduino sends R5, the MSComm1_OnComm() method should be called. The method detects that a receive event is what woke it up, so it reads the data that has arrived. storing it in strData. It then appends that data to whatever is in strBuffer.

It then executes a loop until some condition is true. In the loop, it tests for a carriage return and line feed in the input data. If none is present, you have an infinite loop, from which you never can exit. The do/loop Until stuff is useless. Get rid of it.

Presuming that there IS a carriage return and line feed in the buffer, you proceed to split strBuffer (which contains "R5" at each space, to create an array of words. Oops, there are no spaces, so the array is only going to contain one word. More useless code to get rid of.

Then, you convert the first (and only) word to upper case. More silliness, since the string sent was all uppercase to begin with.

Finally, you have a set of cases that you hope "R5" might match. These include "#R5", "G10", etc.

None of the cases include , so none of the cases will ever match.

You tested that the serial string contained . When you determined that the string did, the proper next step was to remove them. Trim(), Left(), etc.

Nowhere in the Arduino code do you send a '#', so none of the cases with # in them will ever be true.

try this...

 ' set up the code for the input through MSComm1....

 str = MSComm1.Input  ' get the string
 str = Left(str,1)  ' chops off the first char and puts it back in str
 Debug.Print ">"  str  "<"  ' now you can see what is coming through in the immediate window of VB
 Select Case (str) ' set a "beak point " here in VB to stop the processing so you can step through the case
   Case "A"
     Call LEDOn(vbRed)
   Case "B"
     Call LEDOn(vbGreen)
   Case "C"
     Call LEDOn(vbBlue)
   Case "D"
     Call LEDOn(vbYellow)
   Case Else
      Call LEDOff
 End Select
 
' on the Arduino side just send out    Serial.println("A")

'why send out a whole line of text if you don't need it

[/quote]
Lets walk through one case. You press the switch connected to pin 2 on the Arduino. The Arduino sends "R5" to the serial port, along with a carriage return and line feed, and flashes the LED on pin 7.

.great PaulS :wink: you got the idea of mine, this is what i wanted to be..but i just want to clarify that pin 7 is our buzzer, whenever something is pressed it will buzz accordingly to it's precise times...so, our output pin will start on pin 9..therefore the switch connected to pin 2 has an output LED on pin 9.
thanks you so much guys for helping me. :wink:

RPCoyle, i try the code you posted but still nothing sends to the vb.. :disappointed_relieved:

to PaulS:

While not running the VB app, open the Serial Monitor. You should see R5 in the serial monitor window, on a new line, and the LED on pin 7 should flash every time you press the switch connected to pin 2. Do both of those things happen?

.yes precisely both of them happens.. :slight_smile:

Presuming that they do, close the Serial Monitor, and run the VB application.

i was thinking why everytime i used to run the vb application then afterwards when i am going to press the switch connected to pin 2 the LED 9 didn't flash anymore..maybe that was the problem why there is nothing sends to vb...what do you think..?.

another thing, some of my friends told me that maybe i should use < dll > or any software for this..is it true.?.if so, i don't know about dll or those particular software for this..