In GCbasic , is there any way to extract a numerical value from a String Variable.
eg if String$="Data 25", can the 25 be extracted to use in calculations.
The data is coming in from a weather station via the Serial port of a 16F88, and consists of lines of text
with numbers at the end.
ie "textual Info xxx" cr,lf where the xxx is a number between 0 and 255, and the textual info is alpha characters.
The string length varies slightly, but the number is always at the end.
Tnx
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is a code snippet for you to study. In this case I knew exactly how many bytes was being sent, but I don't think that matters, as a variable could just as easily been used in the "hardware Usart" receive loop. You may have to play with loop some, instituting a counter or other method to keep track of the number of bytes coming in.
So dimension an array, assign array values as they are read from the Usart buffer, pick off the numerical values starting from the end of variable length, then decode. The example worked alright at 19200 baud on intermittent transmissions. The data coming in was hex, so a small case select was used for the alpha characters. Good luck =).
Kent
Main:
'There are 22 bytes sent by selection "d) Transmit Packet Defined by User"
'from the other radio receiver module.
Do
For frame = 1 to 22
'wait for uart receive interrupt flag
Do Until RCIF On
nop
nop
nop
nop
Loop
UartIN(frame) = ReceiveSerial
Next
for count = 11 to 16
Led = UartIn(count)-48
If Led < 10 Then 'Hex is decimal
Led = Not Led
Else If
Led = UartIn(count) - 65 'Hex is capital alpha
Select Case Led
Case 0
Led = 10 'Hex 'A'
Case 1
Led = 11 'Hex 'B'
Case 2
Led = 12 'Hex 'C'
Case 3
Led = 13 'Hex 'D'
Case 4
Led = 14 'Hex 'E'
Case 5
Led = 15 'Hex 'F'
End Select
…
…
…
Loop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Main:
'There are 22 bytes sent by selection "d) Transmit Packet Defined by User"
'from the other radio receiver module.
Do
For frame = 1 to 22
'wait for uart receive interrupt flag
Do Until RCIF On
nop
nop
nop
nop
Loop
UartIN(frame) = ReceiveSerial
Next
for count = 11 to 16
Led = UartIn(count)-48
If Led < 10 Then 'Hex is decimal
Led = Not Led
Else If
Led = UartIn(count) - 65 'Hex is capital alpha
Select Case Led
Case 0
Led = 10 'Hex 'A'
Case 1
Led = 11 'Hex 'B'
Case 2
Led = 12 'Hex 'C'
Case 3
Led = 13 'Hex 'D'
Case 4
Led = 14 'Hex 'E'
Case 5
Led = 15 'Hex 'F'
End Select
Led = Not Led
End if
...
...
...
Loop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In GCbasic , is there any way to extract a numerical value from a String Variable.
eg if String$="Data 25", can the 25 be extracted to use in calculations.
The data is coming in from a weather station via the Serial port of a 16F88, and consists of lines of text
with numbers at the end.
ie "textual Info xxx" cr,lf where the xxx is a number between 0 and 255, and the textual info is alpha characters.
The string length varies slightly, but the number is always at the end.
Tnx
Here is a code snippet for you to study. In this case I knew exactly how many bytes was being sent, but I don't think that matters, as a variable could just as easily been used in the "hardware Usart" receive loop. You may have to play with loop some, instituting a counter or other method to keep track of the number of bytes coming in.
So dimension an array, assign array values as they are read from the Usart buffer, pick off the numerical values starting from the end of variable length, then decode. The example worked alright at 19200 baud on intermittent transmissions. The data coming in was hex, so a small case select was used for the alpha characters. Good luck =).
Kent
Main:
'There are 22 bytes sent by selection "d) Transmit Packet Defined by User"
'from the other radio receiver module.
Do
For frame = 1 to 22
'wait for uart receive interrupt flag
Do Until RCIF On
nop
nop
nop
nop
Loop
UartIN(frame) = ReceiveSerial
Next
for count = 11 to 16
Led = UartIn(count)-48
If Led < 10 Then 'Hex is decimal
Led = Not Led
Else If
Led = UartIn(count) - 65 'Hex is capital alpha
Select Case Led
Case 0
Led = 10 'Hex 'A'
Case 1
Led = 11 'Hex 'B'
Case 2
Led = 12 'Hex 'C'
Case 3
Led = 13 'Hex 'D'
Case 4
Led = 14 'Hex 'E'
Case 5
Led = 15 'Hex 'F'
End Select
…
…
…
Loop
Checking back in to see if code tags work.
Kent
Thank you.
Exactly the sort of code I need.