hi all,

i'm very - very newbie here, i need a help, i try to connect arduino with vb6, i want to display data from arduino to vb6, lets say the data is : "the distance : XX m", the problem is when i want to put data via mscomm.input to temp, the value of temp isn't equal with mscomm.input (the script is : temp = mscomm.input), i know this when i try to debugging, like in this pic...why is this happen and...any solution, please?

thanx

vb-facebook

Those pics don't show enough. Copy and paste your code in here, and we'll have a better idea.

thanx for the reply,

like i wrote in post before, when i try to run (F5) there's an error 13, data missmatch, then i'm trying to debug, the error occurs on the " jarak = Mid(strFilter(I), 6, 3) ", when i move my mouse over ".Input" the data was similar with the data from arduino, but when i move to "strInput", the data isn't complete...
this is my code :

With MSComm1
    Select Case .CommEvent
    Case comEvReceive
        strInput = .Input
        temp = temp + strInput
    End Select
        Text1.Text = Text1.Text & temp
        Text1.SelStart = Len(Text1.Text)
        jarak = Mid((Text1.Text), 6, 3)

        strHsl = Split((Text1.Text), vbCrLf)
           For I = 0 To UBound(strHsl)
              hsl = strHsl(I)
         Next

        strFilter = Filter(strHsl, "cm", True, vbTextCompare)
          For I = 0 To UBound(strFilter)
              angka = Now & " - " & strFilter(I) & vbCrLf
             jarak = Mid(strFilter(I), 6, 3)
        Next


        txtView = txtView & angka
        nilai.Caption = ((jarak / 13) * 100)
        ProgressBar1.Value = ((jarak / 13) * 100)

End With
End Sub

thanx before and sorry for my bad english...

The only thing I can think of is, if jakar is a number, it will automatically cast a string if the string is numbers only. anything else except for a period and it will thorw an exception

sorry for my noob...

actually, i just want to take the number from arduino's data trough serial port, arduino sent like this : "Tinggi : xx cm", it sends every 0,5 second, my code just like this :

strInput = .Input

when i debug and move my cursor the data in strInput doesn't same with .Input, so, for the next code it cause error like i said, .Input contain data "Tinggi : xx cm Tinggi : xx cm Tinggi : xx cm... ", but strInputonly contain "Ting", not completely, any suggestion please?

Data mismatch error refers to a mismatch of a value returned i.e. if you told the code to return an integer (numbers) but the data returned is a string (letters etc.), the error will be fired.

What did you declare "jarak" as, an integer or a string?

It looks to me that strInput should be concatenated not just assigned. Try this : strInput = strInput & .Input. See if that helps.

thanx for reply...

@AndreRet : i declared "jarak" as integer, .Input and strInput as string, is it true? i don't know exactly, i'm very very noob here, i think it caused by mismatch given-received data in strInput = .Input (please see my pic in 1st post), may be you have suggestion for me then...

@tinstaafl : i've tried your suggestion, but still not working, strInput = strInput & .Input, strInput has different contain data with strInput & .Input, just like before.

What is the value of the string to be returned - "the distance : 35 m"?

This will raise an error because jarak should be a number. Remove the "Dim jarak As Integer" and just do "Dim jarak" for testing purposes. If this works, we know where your error is raised.

Also, make sure that you have the Mid sections correctly, including spaces to return just the distance number... "jarak = Mid((Text1.Text), 6, 3)"

thanx for reply...

@AndreRet : i've just tried your suggestion, remove the "Dim jarak As Integer" and just do "Dim jarak", and my unlucky may be, it still don't work.
for the mid function, i'm sure it's correct, it just little bit modified in arduino data that want to sent to vb, if you've seen the pict, the data in the code strInput = .Input is different,

.Input ---> "Tinggi : xx cm Tinggi : xx cm Tinggi : xx cm..."
strInput---> "Ting"

why it can happen even there is "=" sign

One thing to remember with the Mid method the index is 1 based.

I think I caught your problem. If I look at your photo, you are setting mid correctly to return from 6. You do however ask to read 3 spaces and not 2 to cover for the 14 of 14 cm. When returning 3 spaces, it returns 14 AND an empty space which will result in an empty data returned value, hence the error. change the 3 to 2 and let me know if that solved the error.

Looking at that string it looks to me that the index for the mid function should be 10 and the length 2

thanx for reply,

@AndreRet and @tinstaafl : i have a little bit enlightment, i remove Text1.SelStart = Len(Text1.Text) and jarak = Mid((Text1.Text), 6, 3) in row 8 and 9.

if i follow your suggestion to change in the mid function, replace the length, how if i want to take data if the data in hundred, lets say the data is 1-150, it cause a badly reading right? may be both of you have a good methode to read data correctly...

If your string follows this format,

    | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14|
    | T | i | n | g | g | i |   | : |   | x | x |   | c | m |

, then you could try this,jarak = Val(Mid(Text1.Text,10,3)). The Val function is a little more forgiving in that it will ignore leading and trailing spaces(i.e. it will process 2 or 3 digit numbers without a problem). Also if it can't parse a number from the string it will return 0 instead of throwing an exception.

You can use the Replace function...

jarak = Mid((Text1.Text), 6, 3)
jarak = Replace$(jarak, " ", "")

This will replace all empty spaces IF they exist, else it will remain the same.

@AndreRet and @tinstaafl :sorry for long time to tell you the result and thanx for reply.... :)

i've tried both of your methode script, tinstaafl's code is perfect for me, the result is correct when data is 1 digit till 3 digit (eg 1-123) but with replace 10 with 9 , for AndreRet's code, when data only 1 digit, it's cause error 13, mismatch data type (maybe "cm" in to the mid function, CMIIW), or may be i was wrong to copy your code, is it replace/completing the jarak = Mid(strFilter(I), 6, 3)? so the code become on your post?

Does strFilter(I) follow the same pattern as Text1.Text? If so the Val function will work for it too. If not it's just a matter of adjusting the parameters of the mid function inside the vall function.

@tinstaafl : strFilter (I) follow the same pattern as Text1.Text? dunno what you mean, the complete code just like my earlier post, with a little bit modification, removing one line code... but as i wrote before, your code works for me, just need to replace 10 with 9 in mid function.

Have a look at THIS link. You'd probably make use of InStr function. Check if an empty value forms part of jarak. If so, just replace it.

Ok good. Glad it works for you.

@AndreRet : thanks for tutorial links and great help so far.

@tinstaafl : thanks a lot for the help.

Only a pleasure. Happy coding...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.