Pages: 1 [2]   Go Down
Author Topic: Arduino LM335 and visual basic 6  (Read 2942 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi again

i tried RPcoyls program, and it seems that it finds the right comport (3),however i cant get the temp to display
here´s the program, all that happends is that the label1 disapears...:-(

please help

 
Private Sub Timer1_Timer()
Dim n As Long
Dim str As String
Dim i As Long
For n = 1 To 16 ' com ports

  ' close any open ports
  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 ' keep looking
      str = ""
      str = MSComm1.Input
      ' finds the port sending out the correct string.
      If InStr(str, "Tmp") > 0 And InStr(str, "@") > 0 Then GoTo jump:
     
      Label2.Caption = n
     Next i
  End If
Next n
jump:
Label3.Caption = str
End Sub


« Last Edit: August 21, 2012, 03:06:43 pm by fenderrobban » Logged

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

i wonder if the code below is written for vb6 or some newer versions
The command " SLEEP 10" shows " Sub or function not defined"
i am running vb6 and all i get is 0
the string is ex "TMP22.15@"

in timer1 i have
call TempNow
label5.caption=TempNow



Function TempNow() As Single
Dim str As String
Dim strt, fin, ct As Integer
Dim tempstr As String
Dim u As Single
Dim i, B  As Integer
u = 0
str = ""
For i = 1 To 5
Sleep 10
 str = MSComm1.Input
' I use "Tmp and "@" instead of "<" and ">"
 If InStr(str, "Tmp") > 0 And InStr(str, "@") > 0 Then
   GoTo J1
 End If
Next i

J1:
ct = 0
u = 0
 
  strt = InStr(str, "@-") + 2
  fin = InStr(str, "-Tmp")
  If (fin - strt) <= 0 Then
   Exit Function
  Else
   ct = ct + 1
   u = u + Mid(str, strt, (fin - strt))
 End If
 TempNow = u / ct
 If Option1.Value = True Then
    TempNow = TempNow * (9 / 5) + 32
 End If
End Function

Logged

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

Quote
Re: Arduino LM335 and visual basic 6

Yes it is written for VB6... isn't that what you are using?
Logged

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

yes. i use vb6, then i don´t understand why i can´t get the temperature to show..
Logged

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

Code:
call TempNow
label5.caption=TempNow
You are setting the caption to a function. Why? What is that supposed to accomplish?

Code:
For i = 1 To 5
Sleep 10
 str = MSComm1.Input
' I use "Tmp and "@" instead of "<" and ">"
 If InStr(str, "Tmp") > 0 And InStr(str, "@") > 0 Then
   GoTo J1
 End If
Next i
Try 5 times to read all data that has arrived so far. Hope like hell that one of those 5 times will contain a whole packet.

It does NOT work that way. Once you have read the data that has arrived so far, that data gets thrown away if it is not a complete packet. It is likely that it won't ever be.

You need to read each partial packet, and APPEND it to some other string. Then, test that other string to see if it contains a complete packet. Do NOT erase the other string. Ever. Just remove the complete packet from the front, and append data to the end.
Logged

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

the label5.caption= TempNow is just for displaying the temperature, is there another way when using Functions,please tell me..


so i basically could remove the whole "For i=1 to 5"  line?
or decrease it to 1...3?


Do you mean something like
a=left(str,3,1)
b=right(str,9,1)
c=mid(str,b-a)

if not....please write an example, i think i understand it better that way.
Logged

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

Quote
the label5.caption= TempNow is just for displaying the temperature
The first call to the function will collect information from the serial port, and throw it away. Then, you assign the result of the 2nd call to the caption. By the time the 2nd call is made, there is no serial data to read.

Quote
so i basically could remove the whole "For i=1 to 5"  line?
or decrease it to 1...3?
You're not listening. Perhaps I could illustrate the point this way:

What you need to do is...

(I'll put some more of the answer in the next post, but you can't read it until tomorrow, by which time you'll have forgotten the partial answer you got today.)

Logged

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

Try this... start a new project in the VB IDE. Create a form and put a VB com control component on it. open the code window and paste the following code into it. be sure and change the com port to the one the Arduino is using. Make sure you have the Arduino IDE closed or the VB app will not connect.

open the immediate window in the VB IDE

This is the simplest possible case. If should work if you are sending a string with a @ in it .


Code:
Private Sub Form_Load()
With MSComm1
        If .PortOpen Then .PortOpen = False
        ' change this to the com port shown by the arduino
        .CommPort = 3
        .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


 

        mystr = "" ' set the string to nothing
        mystr = MSComm1.Input
     
 
            intPos = InStr(mystr, "@")
            If intPos > 0 Then
            Debug.Print mystr
           End If
             
End Sub


Logged

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

Thanks for the hint....beginning to understand it...But
after doing what you said this is the output:

@24.
15@
@24.
15@
@24.
15@
@24.
15@
67@
@24.
64@
@24.
15@
@24.
15@
@23.
67@


is it my Arduino program? here it is
float temp_in_celsius = 0, temp_in_kelvin=0, temp_in_fahrenheit=0;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  //Reads the input and converts it to Kelvin degrees
  temp_in_kelvin = analogRead(0) * 0.004882812 * 100;

  //Converts Kelvin to Celsius minus 2.5 degrees error
  temp_in_celsius = temp_in_kelvin - 2.5 - 273.15;

  temp_in_fahrenheit = ((temp_in_kelvin - 2.5) * 9 / 5) - 459.67;

  //Print the temperature in Celsius to the serial port
  // Serial.print("Celsius: ");
  Serial.print(temp_in_celsius);                 
  Serial.print("@");
  //Print the temperature in Fahrenheit to the serial port
  //Serial.print("Fahrenheit: ");
  //Serial.println(temp_in_fahrenheit);
  // Serial.println();
 
  delay(1000);
}


tried to change the
RThreshold = 4
SThreshold = 3

to
.RThreshold = 5
.SThreshold = 3

and then the outcome is:
23.67@
24.15@
23.67@
23.67@
24.15@
23.67@
24.15@
24.15@
24.15@
24.15@
24.15@

CLOSER!!!:-) but how to i get rid of the @

second question
can i use this in a label or ex mschart?

Logged

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

you are pretty much there fenderrobban as far as I/O from the Arduino to the VB app.  You need to learn to use some of the string functions in VB. Look at my previous post with instr and mid and figure out what I am doing to pull the info out of the total string coming over from the Arduino.

The last Arduino code you sent had pretty much all commented out. you need a Serial.println() to terminate the string. look at what is coming out the Arduino side and compare it with what you have coded it to expect on the VB side

You can use VB to display the results in the form as a text box or label, or even drop them into a list box. You just need to take one step at a time and build your code on that.

I used the debug.print just because it was the easiest way to see if you were getting anything over the line.  Now you can use any of the widgets or functions in VB to manipulate it and get the output you want

Charting the output will require you to really know what you are doing in VB. Don't even think of it till you have a much better understanding of how to manipulate string and numeric values in VB. The chart widgets included in VB are not that good, and they are kind of hard to learn, unless you have a pretty good feel for what you are coding.



Logged

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

ok, i understand,reason i ask about "getting rid of "@" is because i want to use that as a startbyte..


Story is....i am planning to build a computerized greenhouse and there will be about 10 to 15 diffrent sensors  attached to the arduino.


so from what i have understood i want something like.

s122.11@s266@s323.11......

where s1 s2 and s3 etc is the diffrent sensors

hope you understand
Logged

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

Yes you can send a long string over from the Arduino like  @a13.98b200c0.079d346e122.7 and use the VB string functions to select out each sensor value. You might write a function to simplify this

function GetSensorVal(BeginingLetTer,EndingLetter)  returns float or whatever

then to get the first value you could just call Val1 = GetSensorVal("c","d") and it would give you 0.079

You really don't need the @. I just used it as the start of the string since I am looking at only one temperature value and it made a good character to use when I was trying to identify the correct com port.
Logged

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

will try that.

now i have been able to get 2 sensors inplace however, if the second sensor shows a value less then 10 och more than 100 the strings get confused.
i beleive it is the threshold for mscomm1....cause if i incresase it by 1 and have a sensorvalue of ex 20 then the program program works fine but if it is less then 10 i have to decrease it by 1 and increase if sensorvalue is more than 100

how can i make the thresholds automaticlly sens how long the mystr is?

Logged

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

Quote
if the second sensor shows a value less then 10 or more than 100 the strings get confused.
on which end the VB or Arduino?  

Quote
i beleive it is the threshold for mscomm1

The com port only sees characters coming through.You need to use the string functions to find the start and end of the value you are looking for. Then if it is a 10 or a 1000 will make no difference.

so if your input string is a10b or a1000b it makes no difference to the code below

Code:
strt = InStr(str, "a") +1  ' one char past the a
  fin = InStr(str, "b")
  If (fin - strt) <= 0 Then
   Exit Function
  Else
   SensorVal =  Mid(str, strt, (fin - strt))
Logged

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

Worked GREAT! thanks

Logged

Pages: 1 [2]   Go Up
Jump to: