arduino+vb6

hi!

can you help me please in right coding the arduino and the vb6...i find hard in communicating the two devices which is the arduino and the vb6.

here is my 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 vb code:

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
  1. What about first having the same communication speed on both sides? (Arduino uses 9600, VB uses 19200)

  2. What about getting something easier to run first?

Arduino sends something like
"Red is the color of Led/ The pressures value is 20psi"

Your VB code seems to draw colored circles controlled by characters found in that string

Is that what you intend to get ? (If I got that wrong, please tell what the code is supposed to do, and what it does instead)

  1. What do you see while debugging your VB code ?

                Select Case UCase(strWords(1))

Case "a"
                       Call LEDOn(vbRed)

IMO UCase() will never return a lower case character.

thanks for replying me, actually i'm just a newbie about arduino and vb..i have ideas but it's only a sort of idea..

  1. yah, i got mistaken on setting my baud rates..thanks for that :slight_smile:

  2. yes something like that; the flow would be like this..
    when the input is hit it prompts the characters found in serial.print then the indicator which led will turned on.(this will be the flow in arduino)
    now i want this to simulate by vb, a shape of circle which filled by color when the above is happen in the arduino or else any representation that will simulate the above..

  3. when i run the vb code the only thing that appears is the circle then when i hit the button which came from the arduino nothings happen..

  4. so it should be that in capital letter..thanks for that

by the way, as i run the arduino and vb it should be that one of them is not in running mode..it seems, if i run the vb code i should close the serial monitor of the arduino same as the other one.

if i run the vb code i should close the serial monitor of the arduino

Sure. If one application has opened the serial connection, the other cannot connect and will show an error.

when the input is hit it prompts the characters found in Serial.print

It's nice to have a verbose text in Serial Monitor, but for your VB code it would be easier just to receive few (single) characters.

Anyway, VB is easy to debug, and once you get the same message in your VB code as you saw in Serial Monitor, you're in the wrong forum here :wink: (I don't have VB6 any more)

is it possible that the arduino can control the vb6?

in communicating the arduino to vb is it necessary to use the Rx Tx? or the program that Ive been posted will do.?.

THANKS!!!

Glennmars:
is it possible that the arduino can control the vb6?

Not sure if I understand your question.
Of course your VB exe has to be started somehow and has to open the right COM port ( MSComm in VB6, I assume) properly.
Then it can listen and thus be controlled by your arduino.

communicating the arduino to vb is it necessary to use the Rx Tx

I prefer to call it Serial on the Arduino side.
necessary? That's by far the easiest way to communicate, if a USB cable is available.

in communicating the arduino to vb is it necessary to use the Rx Tx?

No, of course not. Your PC is equipped to read smoke signals, right? No? Morse code? Maybe it can see the Arduino waving semaphore flags? No? Damn, then I guess you ARE stuck using the serial port.

thanks for your reply..hehe, anyways i was doubt about the rx/tx that's why i'm asking coz i am pretty sure that you know better the arduino..
if that so, is there any suggestions that you may give to me on how to communicate or interface the arduino to vb6.?. i've done so many research but still it doesn't work..the code i posted can you check if what are the lacking codes.?.

thanks!

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

Ah, I see from your other thread you're obviously a step ahead already. Let's abandon this one. One last hint:

If you want to send both verbose texts and "Commands" , perhaps you distinguish them by a special first character of a new line :

Serial.println("Anything to be put into a text box for debugging, logging, etc. ");
Serial.println("#R"); // A command meaning  " Paint Circle in Red" ...

thanks for replying michael_x :slight_smile:

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

awesome, it's helpful.. ;), but still my vb code does not receive any data... :disappointed_relieved:

Serial.println("#R"); // A command meaning " Paint Circle in Red" ...

should i have to change my code in arduino into this?..hehe, i don't know the < # > means..

perhaps you can link me to my other thread..

Shouldn't these threads be merged?

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

yeah, i feel sorry for this..then, let's merged them..
sorry for this inconvenience becausei don't know how to link the two.. :wink:

try this ...

 ' set up for the input....

 str = MSComm1.Input  ' get the string
 str = Left(str,1)  ' chops off the first char
 Debug.Print ">"  str  "<"  ' now you can see what is comming through in the immediate window of VB
 Select Case (str) ' set a break point here so you can step through the case statement
   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

thanks RPCoyle for your suggestions :%

i tried the code but still nothing sends to the vb.. :roll_eyes:

You may be finding the wrong com port. You have COM1 hardwired into the VB code.

You need to look at what the Arduino IDE is using for your board and use that instead

The code below is what I use to make sure that I am seeing output from my board. It look for a specific string input and then selects that COM port

You can tweak the code to give you what you want

MSComm1.Settings = "9600,N,8,1"
FoundIt = False
' find USB port
ComFail = False
FailErr = False
For n = 1 To 16
  If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
   On Error Resume Next
   MSComm1.CommPort = n
   On Error Resume Next
   MSComm1.InputLen = 0
   MSComm1.PortOpen = True
   If MSComm1.CommID > 0 Then
     For i = 1 To 100
      str = ""
      str = MSComm1.Input
      If InStr(str, "<") > 0 And InStr(str, ">") > 0 Then GoTo  jump: ' in this case the output from Your Arduino  would be "< R >" or  "< Y >" of < G > "....
      Sleep 50
     Next i
  End If
Next n
If MSComm1.PortOpen = False Then MsgBox "Controller Not functioning , Please check USB connection", vbCritical, "": ComFail = True: Exit Sub

jump:
     '   your case statement

Woops! got the "<" and ">" backwards on my last post. They are not consistent with my previous code... sorry. turn them around and try it.

wow very awesome RPCoyle..great :open_mouth:!. i salute you all guys :wink:

now when i run the vb application after running the arduino i can access the switch when i press it, then the LED will flash which i find hard before on how to access it..thank you so much RPCoyle :)...

one more lacking is to fill the color of the circle; i just use the code you posted RPCoyle then i inserted the cases..here is the code, perhaps you can see if what is the lacking of this.

Private Sub MSComm1_OnComm()

MSComm1.Settings = "9600,N,8,1"
FoundIt = False
' find USB port
ComFail = False
FailErr = False
For n = 1 To 16
  If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
   On Error Resume Next
   MSComm1.CommPort = n
   On Error Resume Next
   MSComm1.InputLen = 0
   MSComm1.PortOpen = True
   If MSComm1.CommID > 0 Then
     For i = 1 To 100
      Str = ""
      Str = MSComm1.Input
      If InStr(Str, "<") > 0 And InStr(Str, ">") > 0 Then GoTo jump:  ' in this case the output from Your Arduino  would be "< R >" or  "< Y >" of < G > "....
      Sleep 50
     Next i
  End If
Next n
If MSComm1.PortOpen = False Then MsgBox "Controller Not functioning , Please check USB connection", vbCritical, "": ComFail = True: Exit Sub

jump:
     '   your case statement
      Debug.Print Str;
     Select Case (Str) ' set a break point here so you can step through the case statement
   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 Sub

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

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

on my arduino code, i just replace the serial.println< "R5"> into < "A" , < "B" > and so on...as what you noted also.

too many end subs... did this compile?

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

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

On the arduino side you need to output

    Serial.print("< "); 
    Serial.print(ColorStr);       // output to computer USB port
    Serial.println(">");

On the VB side you need to get the vale back out of the string coming in from the Arduino. Then use ColorStr in your case statement

  strt = InStr(str, "<") + 1
  fin = InStr(str, ">")
  If (fin - strt) <= 0 Then
     Exit Function
  Else
      ColorStr =  Mid(str, strt, (fin - strt))
  End If

You should be able to piece this together and get it working. Get one piece working at a time. learn to set breakpoints and step through the VB code. It will help a lot also if you use lots of debug statements till you get the thing worked out

hello RPCoyle..sorry for this delay information i just found out when i use to compile the vb application an error message appear which is " Argument not optional" that points on the code

Str = ""
  1. i can't understand the what does it means..
Private Sub LEDOn(col As Long)
End Sub
Shape1.FillColor = col      //in this code the error appears
End Sub

Private Sub LEDOff()
End Sub 
Shape1.FillColor = vbWhite
End Sub
  1. also this, when i compile an error appear stating " Only comments may appear after End Sub, End Function, or End Property" which points the above code which i commented.

learn to set breakpoints and step through the VB code

as what i understand on my research it has something to do with pausing the running mode base on your cursor pointing the code..i don't think if i can really understand it..i was so confuse. :disappointed_relieved:

  1. Finally, i can't find if where on the code the shape appear.. :sweat_smile:
Private Sub LEDOn(col As Long)
End Sub                        // this is theoretically correct, but makes no sense
Shape1.FillColor = col      //in this code the error appears   , because VB was told Sub LEDOn has ended already
End Sub

I added a comment as well.
Same for Sub LEDOff below.
This was the reason for RPCoyle's comment "too many End Subs... did this compile?".

  1. i can't understand the what does it means..

It's a feature of VB6 to allow lazy programming. Is

Dim Str as String

clearer to you?
the latter declares a variable and its type, RPCoyle's code implicitly declares it by filling data (An empty sring) in, just to determine its data type.

But besides that, you might benefit a lot by learning a bit VB6 usage ( Stepping through code, etc. )