arduino+vb6

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

hi Glennmars ... just like michael_x says... It's time you slowed down and started to learn to use the VB and Arduino IDE's

If you don't know even how to set a break point or view a debug window, then you must learn.

If what you are doing is just a one time project, then I think you might be able to work your way through it by just using examples off the internet. If you want to continue you just have to learn your code and that takes time and a hell of a lot of patience.

You already have what you need for the communication between VB and Arduino. That is kind of the hard part. The rest is learning to code in VB6

Good luck

Thanks guys..i appreciate your helpfulness :slight_smile:

I can’t imagine how I passed by that code without noticing that a double end sub appears, :cold_sweat: well thanks again..

RPCoyle thanks, I understand what you mean..hehehe, I just can’t fully understand those other terms in vb that’s why I keep on asking..sorry for that.!. :relaxed:

Anyways, I’m still confuse on the code you posted..just feel free to make this clear to me but if not I understand..
Hmmp, about this code:

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.

do you mean that, that code is inserted one by one or they belong to a separate places of the code then i should only determine if where this code fits.?. :zipper_mouth_face:

Finally, the code of my arduino goes like this:

Serial.println ("<Y>");
Serial.print(ColorStr);

is this right.?.or it should be like this:

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

i was confused for that..

Here is some VB6 code I put together from your code...
I added a text box as Text1 so that you would know what the pressure is

Option Explicit
' this is added to allow the sleep call
Private Declare Sub Sleep Lib "kernel32" ( _
    ByVal dwMilliseconds As Long)


Private Sub Form_Load()
  With MSComm1
        If .PortOpen Then .PortOpen = False
        .CommPort = 7 ' *************** YOU MAY NEED TO CHANGE THIS OR ADD MY CODE TO FIND THE COM PORT************************
        .Settings = "9600,N,8,1"
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True
  End With
 
End Sub

Private Sub LEDOn(col As Long)

 Shape1.BackColor = col
End Sub

Private Sub LEDOff()

 Shape1.BackColor = &HFFFFFF
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
Dim mystr As String

        Sleep 1000 ' this waits one second between readings from the Arduino
        mystr = "" ' set the string to nothing
        mystr = MSComm1.Input
      
   
            intPos = InStr(mystr, "<")
            If intPos > 0 Then
           
                Select Case Mid(mystr, 1, 3) ' strip off the CR/LF
                    Case "<Y>"
                        Text1.Text = ("The pressures value is 80 psi")
                        Call LEDOn(&HFFFF&) ' this is in hex
                    Case "<B>"
                        Text1.Text = ("The pressures value is 60 psi")
                        Call LEDOn(&HFF0000) ' this is in hex
                    Case "<R>"
                        Text1.Text = ("The pressures value is 20 psi")
                        Call LEDOn(&HFF&) ' this is in hex
                    Case "<G>"
                        Text1.Text = ("The pressures value is 40 psi")
                        Call LEDOn(&HFF00&) ' this is in hex
                    Case Else
                       Call LEDOff
                End Select
         End If

End Sub

here is the Arduino code I used to send the info to the VB code, based on your code.
I eliminated some of the logic in your rev because I did not know what it did, and I wonder if you did. It seemed to have njot much to do with what you are looking for. You can put it back in is you need it.

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 ("<R>");
      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 ("<G>");
      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 ("<B>");
      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 ("<Y>");
      delay (200);
      digitalWrite (7, HIGH);

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

  }
}

It works for me... it should work for you... give it a try!

thanks for your reply RPCoyle..how great you are :wink:

what you posted on the code on my arduino is same as mine too, but i was so confused about this code that's why i clarify it to you..

Serial.print(ColorStr);
Private Declare Sub Sleep Lib "kernel32" ( _
    ByVal dwMilliseconds As Long)

yes, i know this declaration, well thanks for specifying.. :slight_smile:

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

i just want to clarify if the code above should be still inserted or this code below will do it's function:

intPos = InStr(mystr, "<")
            If intPos > 0 Then
           
                Select Case Mid(mystr, 1, 3)

thanks :wink:

just use the code that I posted last... the other code snippets were examples based on some of the things I did to get a temperature reading to control a kiln

okay thanks RPcoyle :%

i'm so excited to try the code..heheheh, thanks again :smiley:

hi again.!.

RPCoyle, your right that i should add the code you posted in the code below because when i use to debug this it never finds the comport1, (i already change the commport 7 to commport 1)..let us say that i should close the arduino first before it runs..now, i edit the code and add your code but when i debug the vb application an error prompts me that the < FoundIt does not define.> i search through the net some samples on how it was going to be declared but still i find nothing..Perhaps, you know this much will you please help me again for this.. :disappointed_relieved:

 With MSComm1
        If .PortOpen Then .PortOpen = False
        .CommPort = 7 ' *************** YOU MAY NEED TO CHANGE THIS OR ADD MY CODE TO FIND THE COM PORT************************
        .Settings = "9600,N,8,1"
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True

Thanks!

Which is your COM port then ?

You see/set it in the Arduino IDE, Serial Monitor, and see it when you locate your Arduino among "Printers, Devices" in your Windows Control Panel.

For e.g. COM3 you should modify that line to

.CommPort = 3 ' *************** YOU MAY NEED TO CHANGE THIS OR ADD MY CODE TO FIND THE COM PORT************************

Alternatively you might try to understand the hint

ADD MY CODE TO FIND THE COM PORT

this refers to

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

in Entry 16. Can you guess what these lines are doing ?

hi again!..i feel sorry for the delay..

michael_x..i can see already the comport number when i use to plug the arduino it's comport 1...yet, i set the comport as a .CommPort = 1 and as what you said that i should change or add your code for me to find the comport but when i add your code it prompts an error on:

FoundIt = False          // in this code an error occur  stating that it was not being defined...
' find USB port
ComFail = False
FailErr = False
For n = 1 To 16
' 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
  ...

base on my understanding, this line of codes set for finding the comport...

It is hard to believe that your board shows up as COM 1. The computer usually has something at COM 1.

but if the Arduino IDE says it is on COM 1 and you are able to see your output coming through over the serial monitor then it is on COM 1 and the code I gave you should work as long as the output is unclosed in "<" and ">"

If you are mixing the code I gave you with the code Glen gave you, then you might have problems, even though his code is OK. He just uses a "With" statement to streamline the code and it might be a little harder for you to figure out, even though you started out that way. I don't think you understand the fine points.

If you are just using my code, then i really have no idea what the problem might be.

hello..

Private Sub Form_Load()
  With MSComm1
        If .PortOpen Then .PortOpen = False
        .CommPort = 2         // i change the comport into 2 as what i had seen in serial monitor of arduino
        .Settings = "9600,N,8,1"
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True
  End With
 
End Sub

RPCoyle, i used your code and yet it works but it goes back to the problem before that when i try to hit the switch button the Led connected in pin 9 will not light and so on...it seems nothing happens when i use to run the vb application..the communication between arduino to vb are not functioning or we may say that i can't access the input from arduino to vb...but if i try the code below:

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Private Sub MSComm1_OnComm()
Dim Str As String
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
    strt = InStr(Str, "<") + 1
     fin = InStr(Str, ">")
     If (fin - start) <= 0 Then
     
     Else: ColorStr = Mid(Str, start, (fin - strt))
     End If

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 Select
End If
'Exit Function
End Sub

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

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

i can access the input from arduino when i run the vb application..

below are the code i edited which come from you..i add the code you posted in previous to find the port but i guess there still lacking in this..

Option Explicit
' this is added to allow the sleep call
Private Declare Sub Sleep Lib "kernel32" ( _
    ByVal dwMilliseconds As Long)


Private Sub Form_Load()
Dim mystr As String
  With MSComm1
 FoundIt = False         // when i run this vb app. an error occurs here stating not define...
 'find USB port
ComFail = False
FailErr = False
        If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
        On Error Resume Next
       .CommPort = 2  ' *************** YOU MAY NEED TO CHANGE THIS OR ADD MY CODE TO FIND THE COM PORT************************
        .Settings = "9600,N,8,1"
        On Error Resume Next
        .InputLen = 0
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True

  End With
 
End Sub

Private Sub LEDOn(col As Long)

 Shape1.BackColor = col
End Sub

Private Sub LEDOff()

 Shape1.BackColor = &HFFFFFF
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
Dim mystr As String

        Sleep 1000 ' this waits one second between readings from the Arduino
        mystr = "" ' set the string to nothing
        mystr = MSComm1.Input
      
   
            intPos = InStr(mystr, "<")
            If intPos > 0 Then
           
                Select Case Mid(mystr, 1, 3) ' strip off the CR/LF
                    Case "<Y>"
                        Text1.Text = ("The pressures value is 80 psi")
                        Call LEDOn(&HFFFF&) ' this is in hex
                    Case "<B>"
                        Text1.Text = ("The pressures value is 60 psi")
                        Call LEDOn(&HFF0000) ' this is in hex
                    Case "<R>"
                        Text1.Text = ("The pressures value is 20 psi")
                        Call LEDOn(&HFF&) ' this is in hex
                    Case "<G>"
                        Text1.Text = ("The pressures value is 40 psi")
                        Call LEDOn(&HFF00&) ' this is in hex
                    Case Else
                       Call LEDOff
                End Select
         End If

End Sub

i appreciate most your help! :wink:

Private Sub Form_Load()
Dim mystr As String
Dim FoundIt As Integer
Dim ComFail As Integer
Dim FailErr As Integer
Dim n As Integer

MSComm1.Settings = "9600,N,8,1"
FoundIt = False
' find USB port
ComFail = False
FailErr = False
For n = 1 To 1
  If MSComm1.PortOpen Then MSComm1.PortOpen = False
   On Error Resume Next
   MSComm1.CommPort = 1
   On Error Resume Next
   MSComm1.DTREnable = True
    MSComm1.RThreshold = 4
    MSComm1.SThreshold = 3
   MSComm1.InputLen = 0
   MSComm1.PortOpen = True
Next
End Sub

the above code is what i edit using your code to find the comport...

  1. another thing is that, when i run the vb application and then hit the button it prompts an error in which i enclose in quote...then it points on the code above where i put a comment

Run-time error reading '8020':
Error reading comm device

Private Sub MSComm1_OnComm()
Dim strData As String
Static strBuffer As String
Dim strWords() As String
Dim intPos As Integer
Dim boComplete As Boolean
Dim mystr As String

        Sleep 1000 ' this waits one second between readings from the Arduino
        mystr = "" ' set the string to nothing
        mystr = MSComm1.Input  ////in this area the error points out
      
   
            intPos = InStr(mystr, "<")
            If intPos > 0 Then
           
                Select Case Mid(mystr, 1, 3) ' strip off the CR/LF
                    Case "<Y>"
                        Text1.Text = ("The pressures value is 80 psi")
                        Call LEDOn(&HFFFF&) ' this is in hex

i need some help on how to fix this..
thanks!