Pages: [1] 2 3 4   Go Down
Author Topic: arduino+vb6  (Read 9255 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 ("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:
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

Logged

Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)

3) What do you see while debugging your VB code ?

4)
Quote
Code:
               Select Case UCase(strWords(1))
                    Case "a"
                        Call LEDOn(vbRed)
IMO  UCase() will never return a lower case character.
« Last Edit: May 03, 2012, 03:48:57 am by michael_x » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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 smiley-wink (I don't have VB6 any more)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :

Code:
Serial.println("Anything to be put into a text box for debugging, logging, etc. ");
Serial.println("#R"); // A command meaning  " Paint Circle in Red" ...
« Last Edit: May 05, 2012, 07:05:01 am by michael_x » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for replying michael_x smiley

Quote
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.. smiley-wink, but still my vb code does not receive any data... smiley-sad-blue

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try this ...
Code:
' 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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks RPCoyle  for your suggestions smiley-draw

i tried the code but still nothing sends to the vb.. smiley-roll-blue

Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

Pages: [1] 2 3 4   Go Up
Jump to: