Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Can't compare strings!

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Zvoc47

Bascom Member



Joined: 02 Apr 2015
Posts: 21

PostPosted: Sun Apr 12, 2015 1:51 am    Post subject: Can't compare strings! Reply with quote

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
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Sun Apr 12, 2015 2:08 am    Post subject: typo Reply with quote

There is a extra ) in your code. After Target [I])

have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Zvoc47

Bascom Member



Joined: 02 Apr 2015
Posts: 21

PostPosted: Sun Apr 12, 2015 2:10 am    Post subject: Reply with quote

Doesn't fix. Still the same error
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 749

poland.gif
PostPosted: Sun Apr 12, 2015 8:11 am    Post subject: Reply with quote

I test if this compile without errors for sure so here You have whole code.
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



This is very simple. Letters and control keys from keyboard are located in ASCII table. Values from 32 to 127 are letters that You can see. In lower values, from 0 to 31 You have "control keys" For example 13 is an Enter Key - You can`t see it but it works Smile
Have a nice day Very Happy


Last edited by EDC on Sun Apr 12, 2015 8:21 am; edited 3 times in total
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1944

blank.gif
PostPosted: Sun Apr 12, 2015 8:14 am    Post subject: Reply with quote

"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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1944

blank.gif
PostPosted: Sun Apr 12, 2015 8:25 am    Post subject: Reply with quote

"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
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 749

poland.gif
PostPosted: Sun Apr 12, 2015 8:36 am    Post subject: Reply with quote

Yes, I don`t analyze the code. Only this "If Target[I])<>" " Then WhiteSpace=0 " I want fix...
Back to top
View user's profile Visit poster's website
Zvoc47

Bascom Member



Joined: 02 Apr 2015
Posts: 21

PostPosted: Sun Apr 12, 2015 7:29 pm    Post subject: Reply with quote

I solved it by using mid()
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5272
Location: Holland

blank.gif
PostPosted: Sun Apr 12, 2015 9:01 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
Zvoc47

Bascom Member



Joined: 02 Apr 2015
Posts: 21

PostPosted: Tue Apr 14, 2015 11:09 pm    Post subject: Reply with quote

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
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5272
Location: Holland

blank.gif
PostPosted: Tue Apr 14, 2015 11:36 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
Zvoc47

Bascom Member



Joined: 02 Apr 2015
Posts: 21

PostPosted: Fri Apr 17, 2015 3:44 pm    Post subject: Reply with quote

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
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5272
Location: Holland

blank.gif
PostPosted: Tue Apr 21, 2015 3:28 pm    Post subject: Reply with quote

why not using INP() function?
_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum