View previous topic :: View next topic |
Author |
Message |
Zvoc47
Joined: 02 Apr 2015 Posts: 21
|
Posted: Sun Apr 12, 2015 1:51 am Post subject: Can't compare strings! |
|
|
Code: | $regfile = "xm128a1def.dat"
$Crystal=32000000
$hwstack=4160
$swstack=16
$framesize=32
$baud = 115200
'Config Serialin = Buffered , Size = 80
'Config Serialout = Buffered , Size = 254
Declare Sub UsartInput(byref Target As String, byval MaxChars As Integer)
'Enable interrupts
Dim ime As String * 8
Do
Call UsartInput(ime,8)
Print ime
Loop until lcase(ime)="over"
do
print "Over!"
Waitms 100
loop
End
Sub UsartInput(byref Target As String, byval MaxChars As Integer)
Local A As Byte
Local Index As Integer
Local WhiteSpace As Byte
Local I As Byte
InputAgain:
Target=""
Index=0
WhiteSpace=0
Do
A=WaitKey()
If A=8 Then
If Index>0 Then
Print Chr(8);" ";Chr(8);
Decr Index
Target=left(Target,Index)
End if
Else
If A>31 And A<127 Then
If Index<MaxChars Then
Incr Index
Target=Target+Chr(A)
Print Chr(A);
End If
End If
End If
Loop Until A=13
WhiteSpace=1
For I=1 To Index
If Target[I])<>" " Then WhiteSpace=0
Next I
If WhiteSpace=1 Then Goto InputAgain
Print ""
End Sub |
The error is in "If Target[I])<>" " Then WhiteSpace=0". It says Code: | Error : 31 Line : 58 Invalid data type [[0011] 0[TARGET[I]]] , in File : (censored)\noname2.bas
|
I'm trying to iterate through the array of chars if there is a non-space character and say that WhiteSpace is 0 and go to input again
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Sun Apr 12, 2015 2:08 am Post subject: typo |
|
|
There is a extra ) in your code. After Target [I])
have fun
Ben Zijlstra |
|
Back to top |
|
|
Zvoc47
Joined: 02 Apr 2015 Posts: 21
|
Posted: Sun Apr 12, 2015 2:10 am Post subject: |
|
|
Doesn't fix. Still the same error |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Apr 12, 2015 8:14 am Post subject: |
|
|
"Target" is by definition data type "string" and not "array".
It's inefficient anyway, to test the string and set a flag for white spaces in a separate loop, if this can be done easily in the input code before.
If out of any reason, a separate loop is desired, then string function mid() needs to be used for accessing single chars in a string. It will also work to overlay the global variable ime with a char array and forward this array as argument to the sub routine. However, the code in the sub needs to be rewritten then, to handle an array instead of a string. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Apr 12, 2015 8:25 am Post subject: |
|
|
"Target" is by definition data type "string" and not "array".
It's inefficient anyway, to test the string and set a flag for white spaces in a separate loop, if this can be done easily in the input code before.
If out of any reason, a separate loop is desired, then string function mid() needs to be used for accessing single chars in a string. It will also work to overlay the global variable ime with a char array and forward this array as argument to the sub routine. However, the code in the sub needs to be rewritten then, to handle an array instead of a string. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Apr 12, 2015 8:36 am Post subject: |
|
|
Yes, I don`t analyze the code. Only this "If Target[I])<>" " Then WhiteSpace=0 " I want fix... |
|
Back to top |
|
|
Zvoc47
Joined: 02 Apr 2015 Posts: 21
|
Posted: Sun Apr 12, 2015 7:29 pm Post subject: |
|
|
I solved it by using mid() |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sun Apr 12, 2015 9:01 pm Post subject: |
|
|
when the assigned variable is a byte you can also use ASC() like : bSomeByte=asc(sSomestring, bIndex). The index is optional. _________________ Mark |
|
Back to top |
|
|
Zvoc47
Joined: 02 Apr 2015 Posts: 21
|
Posted: Tue Apr 14, 2015 11:09 pm Post subject: |
|
|
The problem is solved. However, I'd sometimes have problems with ASC().
Sometimes ASC() would give me incorrect data type error. Can ASC convert 1 character (character as in byte, not byte and zero byte) from a string into a number that I can later convert into hex? I need this for my debugging program that reads from the program and data memory and prints out hex display like this
Code: | 0x002000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ |
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Apr 14, 2015 11:36 pm Post subject: |
|
|
asc() expects a string. And it will return a byte so it should be used on a numeric data type:
dim b as byte, s as string * 10, idx as byte
s="abcd"
idx=1
b=asc(s,idx)
but there is a catch : when you use the optional index, there is a protection so that you do not index past the end of the string. This means that when the string contains an end of string marker (0) it will end.
If you want direct access you can also consider overlay :
dim s as string * 80
dim b(80) as byte AT S OVERLAY
b() is not a byte array at the same location as String S. Thus b(1) would return the first character.
Using hex() you can print a numeric value. _________________ Mark |
|
Back to top |
|
|
Zvoc47
Joined: 02 Apr 2015 Posts: 21
|
Posted: Fri Apr 17, 2015 3:44 pm Post subject: |
|
|
That means I would need to have an overlay over the whole memory. Is it possible to use C-style pointers instead? I need to be able to load any byte from anywhere in any memory and display it in hex format. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Apr 21, 2015 3:28 pm Post subject: |
|
|
why not using INP() function? _________________ Mark |
|
Back to top |
|
|
|