Shop OBEX P1 Docs P2 Docs Learn Events
how do i pass a byte array to a method and return another byte array — Parallax Forums

how do i pass a byte array to a method and return another byte array

BeagleBeagle Posts: 7
edited 2011-01-05 12:47 in Propeller 1
In a nutshell: I’m trying to pass a reference to a byte array to a method and have it return a byte array…

I’m using the Propeller wired to a WIZ105SV UART to Ethernet module. It acts much like a modem with command/communication modes. I can set the Wiznet into command mode by sending it “+++”. In command mode I can check settings like the IP Address by sending it a command. The Wiznet responds with the response wrapped in “<S … >” brackets. For instance the IP Address response would be “<S192.168.1.4”>. I’m able to strip off the brackets and the S by using a code like the following
VAR
MySize BYTE
IPAddress[20] BYTE
TEMPString[20] BYTE

PUB MAIN

MySize := STRSIZE(@IPAddress) ‘find out how many bytes
BYTEMOVE (@TEMPString, @IPAddress + 2, MySize – 2) ‘copy from the third byte to the end
MySize := 0
MySize := STRSIZE(@TEMPString) ‘find out how many bytes
BYTEMOVE(@IPAddress, @TEMPString, MySize – 1) ‘copy all but the last byte
IPAddress[MySize - 1] := 0 ‘terminate the string with a 0

I’m using the same code several times to get the IP address, Subnet mask etc. I’d like to wrap the code above into a method and then pass it a byte array. I’d like the method to return a byte array… so I tried the following:

PUB MAIN

IPAddress := STRIPWIZ(@WizResponce)


PUB STRIPWIZ (Wizcode) | MMySize, TTEMPString
MMySize := STRSIZE(@Wizcode)
BYTEMOVE (@TTEMPString, @Wizcode + 2, MMySize – 2)
MMySize := 0
MMySize := STRSIZE(@TTEMPString)
BYTEMOVE(@Wizcode, @TTEMPString, MMySize – 1)
Wizcode[MMySize - 1] := 0
RETURN Wizcode

But this does not work. If I comment everything out and just try to debug the string array inside the STRIPWIZ method, I get a single character instead of the string array.

Any ideas on how to get the method to work? I currently have the code working by reusing the same block of code several times, but I think using a method would be more elegant.

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-01-05 12:23
    First of all, all you can return is a long! Nothing else - no way. So, if you need a result wich does not fit into a long you have to pass back addresses. Or ... which is possibly better in your case, you give the method an additional parameter which tells the method where to store the result.

    PUB STRIPWIZ( Wizcode, RetBuffer )

    When calling STRIPWIZ you'd say

    STRIPWIZ( @WizResponse, @IPAddress )

    Of couse both parts - the caller and the called need to be aware of what the size of the buffer is.

    What you do wrong in your STRIPWIZ is that you use functions which expect the address of a string. But you call em by giving the address of a variable that contains the address of a string.
    You call STRIPWIZ with @WizResponse - this is the address of the string. You will find this address in Wizcode. But then you call STRSIZE( @Wizcode ). This means you call strsize by giving it the address of Wizcode and not the address of the string which is stored in Wizcode.

    Right is : MMySize := STRSIZE( Wizcode )
    which equals STRSIZE( @WizResponce )

    Using @TTEMPString is ok, because it's not been passed as a function parameter.

    By the way BYTE has to be in front of the label:
    VAR
    BYTE MySize
    ...
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-05 12:47
    What about something a little more generic that allows you to pass the address of the input and output string locations -- like this:
    pub stripit(pntr1, pntr2) | c
    
    '' Strips prefix and suffix chars from string with IP
    '' -- pntr1 is address of input
    '' -- pntr2 is address of output (array big enough to hold all chars + 0)
    
      repeat
        c := byte[pntr1++]                                          ' get lead characer
      until ((c => "0") and (c =< "9"))                             ' until IP string found
    
      repeat
        byte[pntr2++] := c                                          ' write char to output
        c := byte[pntr1++]                                          ' get next
      until ((c < "0") or (c > "9")) and (c <> ".")                 ' until non-IP character
    
      byte[pntr2] := 0                                              ' terminate string
    

    The attached demo shows it in action. I put "[" and "]" around the input and output strings so that you can see the removal of leading and trailing spaces.
Sign In or Register to comment.