I receive a string from a comport from an arduino
Serial.println("12345"+","+"567"+","+"1.2895");
Where a=12345 ,b=567 ,c=1.2895
How can I get 2 integers and a double derivation of a string,the numbers are separated by a comma.
for example "12345,567,1.2895"
The lenght of de string can vary
Is there an easy way ? I suppose something with mid(string$, m,n),but there are maybe better solutions ?
a=
b=
c=
how to retrieve 2 integers from a string?
Re: how to retrieve 2 integers from a string?
Try this one (only a rough shot):
Code: Select all
Define.s Test$ = "1234,567,89"
Define.i Num, i
Repeat
i + 1
Num = Val(StringField(Test$, i, ","))
If Num = #Null : Break : EndIf
Debug Num
ForEver
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
"Happiness is a pet." | "Never run a changing system!"
Re: how to retrieve 2 integers from a string?
Yes, use StringField() and ValD() for the double entry.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: how to retrieve 2 integers from a string?
thanks, I change num to double ,it works very good
Code: Select all
Procedure string_test()
Define.s Test$ = "1234,567,1.89"
Define.i Num.d, i
For i=1 To Len(test$)
Num = ValD(StringField(Test$, i, ","))
If Num = #Null : Break : EndIf
Debug Num
Next
EndProcedure
Re: how to retrieve 2 integers from a string?
@ludoke:
The for loop isn't correct, because you need only three cycles for splitting Test$ = "1234,567,1.89".
Your For loop will run "Len(Test$)" cycles, which is way to much. But because of the "break" command you doesn't notice this. You could determine the number of cycles by searching the number of ',' (comma) in Test$ before you initialize the loop.
Or you choose the Repeat - ForEver loop instead like in my first post. then you don't have to determine the number of cycles for splitting the text.
Code: Select all
For i=1 To Len(test$)
Your For loop will run "Len(Test$)" cycles, which is way to much. But because of the "break" command you doesn't notice this. You could determine the number of cycles by searching the number of ',' (comma) in Test$ before you initialize the loop.
Or you choose the Repeat - ForEver loop instead like in my first post. then you don't have to determine the number of cycles for splitting the text.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
"Happiness is a pet." | "Never run a changing system!"
Re: how to retrieve 2 integers from a string?
thanks for the help ,I have now this code,I am still learning
Code: Select all
Enumeration
#main_win
#F1 ;functietoets
#A:#B:#C:#D ;textgadget numbers
#test ;button
EndEnumeration
;-------------------------------------------------------
Global Dim x.d(4) ;array for 4 numbers
Global A.d,B.d,C.d,D.d ;double numbers
;-------------------------------------------------------
Declare string_test()
Declare show_values()
;-------------------------------------------------------
flags=#PB_Window_SystemMenu;| #PB_Window_Maximize;#PB_Window3D_Borderless;|#PB_Window_ScreenCentered
If OpenWindow (#main_win,0,0,400,400, "STRING TEST" ,flags )
ButtonGadget(#test,50,50,80,30,"TEST")
TextGadget(#A,50,100,80,30,"A")
TextGadget(#B,50,150,80,30,"B")
TextGadget(#C,50,200,80,30,"C")
TextGadget(#D,50,250,80,30,"D")
AddKeyboardShortcut(#main_win, #PB_Shortcut_F1,#F1) ;define F1 key
Repeat
Ev = WaitWindowEvent()
Select ev ;
Case #PB_Event_Menu
Select EventMenu()
Case #F1 ;F1 key
string_test()
show_values()
EndSelect
Case #PB_Event_Gadget ;dit werkt verschillende case scheiden met komma
Select EventGadget()
Case #test ;test button
string_test()
show_values()
EndSelect
EndSelect
Until ev=#PB_Event_CloseWindow
EndIf
End
;--------------------------------------------------------
Procedure show_values()
a=x(1):b=x(2):c=x(3) :d=x(4) ;numeric values
TextGadget(#A,50,100,80,30,"A="+Str(a))
TextGadget(#B,50,150,80,30,"B="+Str(b))
TextGadget(#C,50,200,80,30,"C="+StrD(c))
TextGadget(#D,50,250,80,30,"D="+StrD(d))
EndProcedure
;--------------------------------------------------------------
Procedure string_test()
Define.s Test$ = "1234,567,1.89,0.0012"
Define i
Define Num.d
Repeat
i + 1
Num = ValD(StringField(Test$, i, ","))
x(i)=num
If i = 4: Break : EndIf
ForEver
EndProcedure
;--------------------------------------------------------------