View previous topic :: View next topic |
Author |
Message |
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Sat Sep 20, 2014 5:53 am Post subject: Set all bytes in an array to a value - is there a way? |
|
|
Hello all,
I want to set all of the bytes in array RxBuff_StrtPointer and RxBuff_ByteCount to zero. Is there a way to set all bytes in an array to the same value, or is a loop required?
Code: |
Dim RxBuffer(254) As Byte
Dim RxBuff_StrtPointer(30) As Byte
Dim RxBuff_ByteCount(30) As Byte
|
Thanks,
Tim
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
Visovian
Joined: 31 Oct 2007 Posts: 584 Location: Czech
|
Posted: Sat Sep 20, 2014 6:31 am Post subject: |
|
|
After being declared, the global array is filled with zeroes.
To do it in the code, you have to use a loop. |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Sat Sep 20, 2014 7:01 am Post subject: |
|
|
Hi,
Have a look in the help text for memcopy
bts = MEMCOPY(source, target , bytes[ , option])
using Option=2 - only the target address will be increased after each copied Byte
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Sat Sep 20, 2014 8:30 am Post subject: |
|
|
Have a look at my wiznet web server I do exactly that for a much larger array to zero them
All I did was declare a string variable then overlay the bytes on it so
Code: |
dim rxbuffers as string * 253 'one more byte will be at the end making it 254
dim rxbuffer(254) as byte at rxbuffers overlay
rxbuffers = "" 'clear buffer
|
Regards Paul |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sat Sep 20, 2014 10:28 am Post subject: |
|
|
Paulvk wrote: | Have a look at my wiznet web server I do exactly that for a much larger array to zero them
Code: | dim rxbuffers as string * 253 'one more byte will be at the end making it 254
dim rxbuffer(254) as byte at rxbuffers overlay
rxbuffers = "" 'clear buffer |
|
That's nonsense, only the first byte of the string/array is zeroed, which is enough to declare the string empty.
If all of the bytes of the array have to be reset to a certain state, Ian's suggestion is the way to go. |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Sat Sep 20, 2014 10:37 am Post subject: |
|
|
If that is so why does is work for the web server when I send the bytes
Note I am not saying you are wrong I was not aware that only the first byte was zeroed
Always willing to learn!
Regards Paul |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Sat Sep 20, 2014 10:51 am Post subject: |
|
|
Hi,
A string has a 0 as trailing char so Setting the first char to 0 sets the string to "" even if theres "junk" in Memory after it.
Using memcopy to set all Bytes to a value is the same as using a Loop, but as all the code is in the lib (is machine code) it'll be quicker than writing all Bytes in Basic using a Loop.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Sat Sep 20, 2014 11:11 am Post subject: |
|
|
This is the problem when you have only yourself to teach you
some of what you learn is only by making an error
Any now I have learnt something to day
but I will now have to look at the send sub and see why it works and learn some more!
Regards Paul |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Sat Sep 20, 2014 11:26 am Post subject: |
|
|
Hi,
Try making a small "simple" program and run it in the Simulator looking at the Memory.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Sat Sep 20, 2014 11:32 am Post subject: |
|
|
Hi Ian
Have done just that and also found var = STRING(m ,n)
This seems to work
Code: |
$regfile = "m2560def.dat" ' specify the used micro
$crystal = 16000000 ' used crystal frequency
$baud = 56700 ' use baud rate
$hwstack = 180
$swstack = 250
$framesize = 200
Dim Tests As String * 10
Dim Bytes(11) As Byte At Tests Overlay
Dim X As Byte
Tests = "1234567890"
For X = 1 To 10
Cls
Locate 1 , 1
Lcd Bytes(x)
Wait 2
Next
Cls
Locate 1 , 1
Lcd "next"
Tests = String(10 , 0)
Wait 5
For X = 1 To 10
Cls
Locate 1 , 1
Lcd Bytes(x)
Locate 2 , 1
Lcd X
Wait 2
Next
End
|
Regards Paul |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Sat Sep 20, 2014 11:44 am Post subject: |
|
|
Hi,
There are many ways to Skin a cat.
Looks as if String command does the same thing as memcopy with Option 2.
But looking in the help text I see:
Since a string is terminated by a 0 byte, you can't use 0 for n. where n is the ASCII char to use.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Sun Sep 21, 2014 4:26 am Post subject: |
|
|
Hi and thank you for all your replies everyone!
I did not expect such activity on this thread but am thankful for your responses and help.
Tim |
|
Back to top |
|
|
|