Shop OBEX P1 Docs P2 Docs Learn Events
bytefill and bytemove — Parallax Forums

bytefill and bytemove

nicolad76nicolad76 Posts: 164
edited 2010-11-02 20:19 in Propeller 1
Hi,
readingfrom Prop Manual:
[...]
Using BYTEFILL
BYTEFILL is a great way to clear large blocks of byte-sized memory. For example:

VAR
byte Buff[100]
PUB Main
bytefill(@Buff, 0, 100) 'Clear Buff to 0
[...]

and

[...]
BYTEMOVE is a great way to copy large blocks of byte-sized memory. For example:
VAR
byte Buff1[100]
byte Buff2[100]
PUB Main
bytemove(@Buff2, @Buff1, 100) 'Copy Buff1 to Buff2
[...]

So, in order to extract a substring I wrote the following code:
var


 byte command[56]
 byte cmd[4]
 byte params[51]
  
PUB Main 

 pst.Start(115_200) 

 repeat
       pst.StrIn(command)      
       pst.Str(String(pst#NL, pst#NL, "COMMAND 1: "))
       pst.Str(command)

       bytemove(@cmd,@command,3) ' copy 3 bytes from command to cmd   
       bytefill(@cmd+3,0,1)      ' add zero-termination string

       pst.Str(String(pst#NL, pst#NL, "COMMAND 2: "))
       pst.Str(command)

       pst.Str(String(pst#NL, pst#NL, "CMD: "))
       pst.Str(cmd)


output is

COMMAND 1: 1234567890

COMMAND 2: 1234567890

CMD: 1234567890

if i try (bcs I was desperate and I tried all permutations....)
       bytemove(cmd,command,3) ' copy 3 bytes from command to cmd   
       bytefill(cmd+3,0,1)      ' add zero-termination string

the output will be:
COMMAND 1: 1234567890

COMMAND 2: 123

CMD: 123

which looks fine but the original value of the variable "command" seems to be corrupted.

Why all this happens?
Thanks
Nicola

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2010-11-02 20:15
    You need to pass the address of your byte arrays. Try this:
    var
     byte command[56]
     byte cmd[4]
     byte params[51]
      
    PUB Main 
    
     pst.Start(115_200) 
    
     repeat
           pst.StrIn([COLOR="Red"]@[/COLOR]command)      
           pst.Str(String(pst#NL, pst#NL, "COMMAND 1: "))
           pst.Str([COLOR="Red"]@[/COLOR]command)
    
           bytemove(@cmd,@command,3) ' copy 3 bytes from command to cmd   
           [COLOR="Blue"]cmd[3] := 0[/COLOR]               ' add zero-termination string [COLOR="Blue"](simpler)[/COLOR]
    
           pst.Str(String(pst#NL, pst#NL, "COMMAND 2: "))
           pst.Str([COLOR="Red"]@[/COLOR]command)
    
           pst.Str(String(pst#NL, pst#NL, "CMD: "))
           pst.Str([COLOR="Red"]@[/COLOR]cmd)
    
    
  • nicolad76nicolad76 Posts: 164
    edited 2010-11-02 20:19
    All right!!!! I was looking for the problem in the wrong place!!!
    thanks, it worked!!!!
    Nicola
Sign In or Register to comment.