Pages: [1] 2   Go Down
Author Topic: Arduino LM335 and visual basic 6  (Read 2378 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!

I am trying to send a temperature (LM335) from the arduino to my visual basic 6.0 program and sofar i get the correct temp inside the "serial monitor" but when i try to receive it in VB6 all i get is a large number.

ex
Serial monitor  24.11
VB6 11870

here´s the vb program

Private Sub Form_Load()

' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 2

' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 2

' 2400 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "9600,N,8,1"

' Make sure DTR line is low to prevent Stamp reset
MSComm1.DTREnable = False
' Open COM3
MSComm1.CommPort = 3
MSComm1.PortOpen = True

End Sub

Private Sub MSComm1_OnComm()
Dim sData As String
Dim lHighByte As Long
Dim lLowByte As Long
Dim lByte As Long

' If Rx Event then get data and process
If MSComm1.CommEvent = comEvReceive Then
    sData = MSComm1.Input ' Get data
    lHighByte = Asc(Mid$(sData, 1, 1)) ' get 1st byte
    lLowByte = Asc(Mid$(sData, 2, 1))  ' Get 2nd byte
    lByte = JoinHighLow(lHighByte, lLowByte)
    Label2.Caption = CStr(lByte)
    DrawScale lByte
End If
End Sub

what do i do wrong?
i want to get 24 in the vb6 instead

robban
Logged

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

Code:
sData = MSComm1.Input ' Get data
Gets the data as a string.

Code:
    lHighByte = Asc(Mid$(sData, 1, 1)) ' get 1st byte
    lLowByte = Asc(Mid$(sData, 2, 1))  ' Get 2nd byte
    lByte = JoinHighLow(lHighByte, lLowByte)
Pretends that you got binary data. You didn't.

Why are you doing that?
Logged

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

i tried the

mscomm1.input=sdata
label2.caption=sdata

but all i get is the last decimals of the temp.
here is my arduino program

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

  //Print the temperature in Fahrenheit to the serial port
  //Serial.print("Fahrenheit: ");
  //Serial.println(temp_in_fahrenheit);
 // Serial.println();


  delay(1000);
}

what is wrong?
Logged

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

Quote
but all i get is the last decimals of the temp. ... what is wrong?
You are sending strings that have no delimiters between packets, and relying solely on data arriving in a timely fashion.

For certain baud rates, on lightly loaded PCs, that will work for a short period of time. For robust communications, you need to send start and end of packet markers between the strings.

Then, read data on the PC, appending it to a buffer until the buffer contains a start and end of packet marker. When it does, extract the value between the markers.

The Arduino should send something like "<19.5><19.6><19.5><19.4>".
Logged

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

How would the code look like?

(pretty new with arduino)
Logged

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

Quote
How would the code look like?

(pretty new with arduino)
Very similar to what you have, except you need two more Serial.print() statements:
Code:
  Serial.print("<");                 
  Serial.print(temp_in_celsius);                 
  Serial.print(">");     
           
Logged

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

ok,thanks, tried it in vb6 but i cant seem to get it right..
ideas?
Logged

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

Quote
tried it in vb6 but i cant seem to get it right
Tried what in VB6?

Quote
ideas?
Oh, I don't know. Maybe post your code.
Logged

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

' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 5

' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 5

' 2400 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "9600,N,8,1"

' Make sure DTR line is low to prevent Stamp reset
MSComm1.DTREnable = False
' Open COM3
MSComm1.CommPort = 3
MSComm1.PortOpen = True

End Sub

Private Sub MSComm1_OnComm()
Dim sData As String
Dim lHighByte As Long
Dim lLowByte As Long
Dim lByte As Long


' If Rx Event then get data and process

If MSComm1.CommEvent = comEvReceive Then
If MSComm1.Input = "<" Then

Do

sData = MSComm1.Input ' Get data
Label2.Caption = sData
Loop Until sData =">"
Label2.Caption = sData
End If

End If
End Sub
Logged

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

Code:
' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 5
So, why is the length 5?

Quote
Private Sub MSComm1_OnComm()
This event fires when there is some serial data to read - not just when there is a complete packet. It is up to you to read and store data until a complete packet (or more) has arrived.

Quote
If MSComm1.Input = "<" Then
The Input field contains all the data that has arrived so far, NOT just one character.

You need to define a String object that you append any serial data to. When all the available data has been read, you need to see if that String object contains a < and a >. If it does, extract the portion up to the >, then strip off the first character. What will be left will be the packet contents.
Logged

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

ok, i changed it to the following


' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 2

' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 2
' 2400 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "9600,N,8,1"

' Make sure DTR line is low to prevent Stamp reset
MSComm1.DTREnable = False
' Open COM1
MSComm1.CommPort = 3
MSComm1.PortOpen = True

End Sub


Private Sub Timer1_Timer()
Dim sData As String

' If Rx Event then get data and process
If MSComm1.CommEvent = comEvReceive Then
    sData = MSComm1.Input ' Get data
    If sData = "<" Then
    Do
    Label2.Caption = sData
    Loop Until (sData = ">")
End If
End If
End Sub


but still nothing,just 0 :-(
Logged

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

This works for me. You should be able to modify it to get what you want.
Code:
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

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

ok, i copied your program and pasted it into mine, deleted my "oncomm..." etc and added at the end of your program label2.caption=Tempnow
i changed my < and > to your Tmp and @ in the arduino
but still nothing

here is the vb program


Private Sub Form_Load()

' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 5

' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 5

' 2400 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "9600,N,8,1"

' Make sure DTR line is low to prevent Stamp reset
MSComm1.DTREnable = False
' Open COM1
MSComm1.CommPort = 3
MSComm1.PortOpen = True

End Sub




 
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
    Label2.Caption = TempNow
 End If
End Function




and here is the arduino program
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("Tmp");                 
  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);
}

getting a headache...:-(
Logged

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

the Arduino code is ok. I ran it and it showed the correct values in my VB program, so it's coming across the USB ok
Here is the code I use to find the correct com port. It cycles through until it finds a com port sending out the correct string
you will have to declare some of the variables to get it running, but it
stick some debug.print statements in the code and step through it. You should be getting values comming over if the baud rate is set correctly
also, you need to put the VB side read insome kind of loop also so it will keep reading the input

Code:
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:
      Sleep 50
     Next i
  End If
Next n
jump:

Logged

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

Code:
' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 5

' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 5

' 2400 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "9600,N,8,1"
If you are not going to maintain the comments, delete them. Otherwise, they make you look like an idiot.
Logged

Pages: [1] 2   Go Up
Jump to: