Arduino LM335 and visual basic 6

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

sData = MSComm1.Input ' Get data

Gets the data as a string.

    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?

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?

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

How would the code look like?

(pretty new with arduino)

How would the code look like?

(pretty new with arduino)

Very similar to what you have, except you need two more Serial.print() statements:

  Serial.print("<");                 
  Serial.print(temp_in_celsius);                 
  Serial.print(">");

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

tried it in vb6 but i cant seem to get it right

Tried what in VB6?

ideas?

Oh, I don't know. Maybe post your 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"

' 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

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

So, why is the length 5?

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.

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.

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

This works for me. You should be able to modify it to get what you want.

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

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

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

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

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

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

Re: Arduino LM335 and visual basic 6

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

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

call TempNow
label5.caption=TempNow

You are setting the caption to a function. Why? What is that supposed to accomplish?

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.