Go Down

Topic: arduino+vb6 (Read 10 times) previous topic - next topic

Glennmars

hello..
Code: [Select]

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:


Code: [Select]

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

Glennmars

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

Code: [Select]

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!  ;)

Glennmars

Code: [Select]

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

2. 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
Quote

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


Code: [Select]

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!

RPCoyle

Gosh Glen, we seem to be going around in circles.

The code I gave you in reply #22 works... I tried it, it works with your Arduino script.

You still seem to be thrashing around trying to get the VB app to read COM 1.

The code should work if you are sending data from the Arduino to the VB on COM 1.

Even if the data doesn't match what VB is looking for, it should not give you a error message. it should just do nothing.

Are you sure that the Arduino board is sending data out on COM 1? When you open Tools/Serial Port from the menu does it say COM 1 and can you then look at the serial monitor sending out data on this port?

I guess the other question is... is your Arduino board a real Arduino board made by the folks in Italy or is it some clone made by some other company. I bought a couple of clones from China that had different  chips for serial communication and I had trouble with them using the  Arduino 1.0 rev and had to go back to rev .021 to even get a download.

This is probably not your problem, but I can see nothing much else to try.

Oh buy the way, it seems like way back some time you actually did get the VB to connect with the Arduino. Is that true or just my imagination?





Glennmars

hi RPCoyle am sorry if i keep on inquiring 'coz until now still i find nothing..

base on what you said, i had two installer of arduino the latest version which is the 1.0 and also the .0021....i used the old version which is the .0021 and yet it works on the arduino..i can see in the serial monitor the <R> and so on when i press the switch button but when i run the vb6 application and press the button switch nothing happens..seems it goes back to the problem before that when i run the arduino then next to that is the vb app then as i pressed the button switch the LED will not flash or nothing happens to it... :smiley-sad-blue:

hope you understand me.. :(

thanks

Glennmars

hello again...

i just want to show you my edited code..this code will be able to access the switch button which is connected in arduino when i run the vb application..i just inserted your code RPCoyle in finding the commport...just take a look if what is lacking on this because i can only access the button switch but nothing sends to vb application or we may say the circle doesn't fill any color.... :smiley-sweat:
Code: [Select]

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

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




thanks  ;)

RPCoyle

Sorry Glen, we are still going in circles. I asked you what COM port showed up in the Tools/Serial Port menu in the Arduino IDE when you are seeing the correct data coming out on the Serial Monitor window.
Answer this question, and then maybe we can go further.

Glennmars

actually, you can set the comport on any vacant or not in used port...right.?. now base on what you've ask RPCoyle it's Commport 1..whenever i open the arduino then go to tools/serial port it's comport 1..then when i open the serial monitor it's still com1... but i can change this if ever i want by just right clicking the computer/properties/device manager/port(COM & LPT)/port settings/advance/comport number..

RPCoyle

You may see more than one com port listed in the tool/serial port menu, but only one com port will work to give you the output from the board. you can not set the comport for the Arduino to any vacant or not in used port. If you do not select the correct port you will not see anything on the serial monitor.

The only valid port for you is the com port that shows the correct values on the serial monitor for your board... Which com port selected from the tool/serial port menu gives you the correct output on the serial monitor?

Glennmars

it's comport 1 RpCoyle...i can see on the serial monitor the "<R>" and the rest of this when i hit the button with the commport value which is commport1...

how about correcting those components i put on my vb.?.just try to correct this:

as i open the vb application first thing i did was inserting the component, i open the Project/components/controls/(i check the) micsrosoft comm control 6.0..after that i put the component that appears on the toolbox picture like a telephone on the form, then i put a textbox and a shape on a form also..
my regard is, is this all right.?. :smiley-roll-blue:

RPCoyle

Use this Arduino sketch to upload to your board
DO NOT CHANGE ANYTHING!!!

Code: [Select]

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)
    {
      Serial.println("<a>");
      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)
    {
        Serial.println("<b>");
      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)
    {
      Serial.println("<c>");
      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)
    {
        Serial.println("<d>");
      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);

    }

  }
}


Create a new project and use the code below you need a put a label called (label1)and caption it "Com Port Number. add a communication object named (MSComm1). Put in a text box called
(ComPort). Add another longer text box called (text1). add a command button called (Command1)change the caption to "find com port". now add another text box called (text1).
Next add a shape object called (Shape1)
compile and run the code. Put the number of the com port into the text box called ComPort... just type in "1". click the command button and the other text box should show the pressure and the shape object should change colors.
If it is the wrong com port you will see the error 8002 comport not found ( or whatever it says)
Then re start and try another port till it doesn't give error messages.
DO NOT CHANGE THE CODE!!!
Code: [Select]

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


Private Sub LEDOn(col As Long)

Shape1.BackColor = col
End Sub

Private Sub LEDOff()

Shape1.BackColor = &HFFFFFF
End Sub


Private Sub Command1_Click()
If Val(ComPort) < "1" Then Exit Sub
With MSComm1
        If .PortOpen Then .PortOpen = False
        .CommPort = Val(ComPort)
        .Settings = "9600,N,8,1"
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True
  End With
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 "<a>"
                        Text1.Text = ("The pressures value is 20 psi")
                        Call LEDOn(vbRed) ' this is in hex
                    Case "<b>"
                        Text1.Text = ("The pressures value is 40 psi")
                        Call LEDOn(vbGreen) ' this is in hex
                    Case "<c>"
                        Text1.Text = ("The pressures value is 60 psi")
                        Call LEDOn(vbBlue) ' this is in hex
                    Case "<d>"
                        Text1.Text = ("The pressures value is 80 psi")
                        Call LEDOn(vbYellow) ' this is in hex
                    Case Else
                       Call LEDOff
                End Select
         End If

End Sub


Glennmars

thanks for replying RPCoyle i'll be online in this consecutive days it's a big opportunity for me to keep in touch with this  forum.

now, regarding on what you've posted..i just need to clarify this
Quote

Create a new project and use the code below you need a put a label called (label1)and caption it "Com Port Number.

> base on what i understood...i change the caption on the label as 1

Quote

add a communication object named (MSComm1). Put in a text box called (ComPort).

> do i have to put a textbox on my form then if it is..i change the text into ComPort

Quote

Add another longer text box called (text1). add a command button called (Command1)change the caption to "find com port". now add another text box called (text1).

>i add a longer textbox and change the text as text1, then i add a command button and change the caption into find com port, then i add another textbox and change the text text1


kindly check RPCoyle if all of this are right or did we understand each other..thanks!


RPCoyle

Glenn... you are on your own. You need to learn enough about VB to correctly label and initialize the form objects.

If you do as I instructed, the form will work.

I have actually done your project for you. The code on both sides of the com port work. If there is anything left to do, or if for some reason the communications still do not happen then you must work it out yourself.

I wish you Good luck, but this will be my last post to you except perhaps say congratulations, if you send a post that says you completed the project and made it work.


Glennmars

thanks RPCOyle.. ;)i know i'm so makulit..but please am asking you to stay on touch on resolving this just for today and tomorrow.. :smiley-sad-blue:


..regarding on what you've posted i made it already and yet it works but what i wanted to happen is, the input should be done from the arduino going to the vb application..it seems, when i press the switch button from my hardware which is connected to the arduino i can see on the serial monitor the "<G>" then the "<G>" should be sent to the vb application..inshort, there is no more pressing the button that may happen to the vb and no more command button should be added, all the vb should do is receiving the input from arduino..


anybody please help me..

majdi_mohammad

#44
Jun 23, 2012, 01:07 pm Last Edit: Jun 23, 2012, 01:13 pm by majdi_mohammad Reason: 1
First build visual basic 6 project :

Added to the form1 :
1. Label1
2. Shape1 then copy it to 8 Piece. *It will be array sequence so the name will be = shape1(0) to shape1(7) 
3. MSComm1





Open on source code interface copy and paste this source code :

Dim angka(8) As Integer  //--> this smile will be your enemy 8 )

Private Sub Form_Load()
For i = 0 To 7
Shape1(i).FillStyle = 0
Shape1(i).FillColor = vbRed
angka(i) = "0"
Next
MSComm1.RThreshold = 8
MSComm1.InputLen = 8
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 2
MSComm1.PortOpen = True
MSComm1.DTREnable = False
End Sub


Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub


Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = comEvReceive Then
    sData = MSComm1.Input
    Label1.Caption = ""
    For i = 0 To 7
    angka(i) = Asc(Mid$(sData, i + 1, 1))
    Label1.Caption = angka(i) & Label1.Caption
    If angka(i) = "1" Then
    Shape1(i).FillColor = vbYellow
    Else
    Shape1(i).FillColor = vbRed
    End If
    Next
End If
End Sub


After done make sure you setup the comport number on the visual basic same with your arduino used.

Go Up