how do i pass a byte array to a method and return another byte array
Beagle
Posts: 7
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.
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
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
...
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.