Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

A Byte copy command similar to Mid?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Sat Oct 04, 2014 1:38 am    Post subject: A Byte copy command similar to Mid? Reply with quote

Hello all,

I need to selectively copy some bytes from one variable to another. I tried MEMCOPY but that did not work.
Code:

        'bts = MEMCOPY(source, target , bytes[ , option])
'        w = MEMCOPY(Rxbuffer(NextStartPosition), Rx_Address(Rx_Addr_Cntr) , 2)
'        w = MEMCOPY(Rxbuffer(NextStartPosition), Rx_Zone(Rx_Addr_Cntr) , 1)
'        w = MEMCOPY(Rxbuffer(NextStartPosition), Rx_DevType(Rx_Addr_Cntr) , 1)
'        w = MEMCOPY(Rxbuffer(NextStartPosition), Rx_DevType(Rx_Addr_Cntr) , 1)
 


I also tried Mid, but the variables are not strings:
Code:

       Rx_Address(Rx_Addr_Cntr) =  Mid(Rxbuffer(NextStartPosition),2,2)
        Rx_Zone(Rx_Addr_Cntr)  =  Mid(Rxbuffer(NextStartPosition),4,1)
        Rx_DevType(Rx_Addr_Cntr)  =  Mid(Rxbuffer(NextStartPosition),6,1)
 


Can anyone suggest the correct or best way to do this?

Thanks,
Tim

(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1207
Location: SYDNEY

australia.gif
PostPosted: Sat Oct 04, 2014 2:12 am    Post subject: Reply with quote

Hello Tim

I would overlay a byte array onto the variables then select the elements in the arrays to swap/move

Regards paul
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Sat Oct 04, 2014 3:08 am    Post subject: Reply with quote

Hi Paul,
Thanks for the tip. But it already is a bytes array:

Code:

Dim RxBuffer(253) As Byte  
 


Do you mean overlay a string like this:
Code:

 Dim Rxstring As String * 253 At RxBuffer(1) Overlay
 



Thanks again,
Tim
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1207
Location: SYDNEY

australia.gif
PostPosted: Sat Oct 04, 2014 4:16 am    Post subject: Reply with quote

Hello Tim

then just copy the elements you want from buffer array to the other
I thought with your question that they were not byte arrays just variables.
Yes if you have bytes that are ASCII characters as I have in the web servers you can overlay a byte array on a string variable
then use string functions on the string to search for characters this is what I do in the web servers
I take bytes from the wiznet/enc28j60 chips put them into the overlay-ed byte array then they are also in the string.
eg
dim thestring as string * 100
dim bytesof(101) as byte at thestring overlay
Note there is an extra byte at the end of the string as a terminator you need to set this byte see web servers.
Also note you can put the ASCII value as a number into a byte then in the string it is a character.


regards Paul
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1915

blank.gif
PostPosted: Sat Oct 04, 2014 10:15 am    Post subject: Re: A Byte copy command similar to Mid? Reply with quote

TSEYFARTH wrote:
I need to selectively copy some bytes from one variable to another. I tried MEMCOPY but that did not work.
Code:

w = MEMCOPY(Rxbuffer(NextStartPosition), Rx_Address(Rx_Addr_Cntr) , 2)

Tim, I do not want to sound too harsh here, but I believe, that you do hard with programming, as you don't pay heed to advices, instead you make the same mistake over and over again.
In this case it's the "I just post a chunk of code, which can't be simulated and lacks necessary info" mistake.

And I think it's not only me, who did ask for minimized samples usable for simulation.

The memcopy-command exactly matches your requirement, and there's no need to mess around with strings or overlays.
However, if there would be actually a bug in memcopy, how do you think, it can be found?
Well, yes, by simulating the code.

But for proper simulation, all variable dimensioning has to be known, to rule out a "bug made by user".
So this minimum sample has to be created first, and there I wonder, why you, as the someone seeking for help, expect from the someone giving help to do your very work?

Ok, preached enough...

Here is a sample code:
Code:
' Bascom Ver 2.0.7.7
$Regfile = "m328def.dat"
$Crystal=4000000
$hwstack=40
$swstack=16
$framesize=32

Dim BArr_A(16) As Byte
Dim BArr_B(16) As Byte
Dim Addr_A As Word
Dim Addr_B As Word
Dim rslt As Word

BArr_A(1) = 10
BArr_A(2) = 66
BArr_A(3) = 5
BArr_A(4) = 128
BArr_A(5) = 13

Addr_A = 2
Addr_B = 5

rslt = Memcopy(BArr_A(Addr_A) , BArr_B(Addr_B) , 2)

end


If you simulate it (open the MEMORY window, select SRAM tab in simulator) by stepping the code with F8, you will notice array A's memory cells (in this sample at memory hex address 0100) are set and after, stepping over the memcopy command, you'll find the content of array A's cells 2 and 3 at position 5 & 6 of array B .

That means, memcopy is working as expected and the reason it doesn't work for you, has to be found somewhere in your code.
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Sat Oct 04, 2014 8:54 pm    Post subject: Reply with quote

Hi MWS.
Yes, I am guilty as charged..... The medium gentility is sometimes a good idea - not preachy put downs, but a reminder of what can make a difference to the reader and the poster. The poster will get more responses with more complete information, as you have pointed out.

I was actually going to make post today (just got up after a long night) that I was able to solve the problem. I was not getting the answer that I was expecting but was blind by the use of the MEMCOPY command. I had reviewed the help which was only a little help. Had the results been shown in the help for each of the lines of code that would have been more clarifying. And of course having not made a simple trial, as you suggested too, may have helped.

This is the code that works properly

Code:


    Dim RxBuffer(253) As Byte
    Dim Rxstring As String * 253 At RxBuffer(1) Overlay
    Dim RxBuff_Payload_Cntr As Byte
    Dim RxBuff_StrtPointer(30) As Byte

    Dim RxBuff_ByteCount(30) As Byte
    RxBuff_Payload_Cntr = 0

    Dim RxBuffer_Full As Byte    '1 = Yes, 0 = No
    RxBuffer_Full = 0

    Dim Rx_Address(10) As word
    Dim Rx_strAddr As String * 40 At Rx_Address(1) Overlay

    Dim Rx_Zone(10) As Byte
    Dim Rx_strZone As String * 10 At Rx_Zone(1) Overlay

    Dim Rx_DevType(10) As Byte
    Dim Rx_strDevType As String * 10 At Rx_DevType(1) Overlay

    Dim Rx_Addr_Cntr As Byte
    Rx_Addr_Cntr = 0


..... lots of other code.....

        If Rx_Addr_Cntr = 10 Then Rx_Addr_Cntr = 0
        Incr Rx_Addr_Cntr

        'bts = MEMCOPY(source, target , bytes[ , option])
        w = MEMCOPY(Rxbuffer(NextStartPosition), b , 1)
        b = b *256
        w = MEMCOPY(Rxbuffer(NextStartPosition + 1), Rx_Address(Rx_Addr_Cntr) , 1)
        Rx_Address(Rx_Addr_Cntr) = b + Rx_Address(Rx_Addr_Cntr)


        w = MEMCOPY(Rxbuffer(NextStartPosition + 2), Rx_Zone(Rx_Addr_Cntr) , 1)
        w = MEMCOPY(Rxbuffer(NextStartPosition + 4), Rx_DevType(Rx_Addr_Cntr) , 1)

        Print "Rx_Address(Rx_Addr_Cntr) ";Rx_Address(Rx_Addr_Cntr)
        Print "Rx_Zone(Rx_Addr_Cntr) "; Rx_Zone(Rx_Addr_Cntr)
        Print "Rx_DevType(Rx_Addr_Cntr) "; Rx_DevType(Rx_Addr_Cntr)


 


Expected and received results are:
Print "Rx_Address(Rx_Addr_Cntr) ";Rx_Address(Rx_Addr_Cntr) = 52719
Print "Rx_Zone(Rx_Addr_Cntr) "; Rx_Zone(Rx_Addr_Cntr) = 1
Print "Rx_DevType(Rx_Addr_Cntr) "; Rx_DevType(Rx_Addr_Cntr) = 1


In any case MWS, thanks for your response and reminder!
Tim
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5199
Location: Holland

blank.gif
PostPosted: Sat Oct 04, 2014 9:12 pm    Post subject: Reply with quote

Quote:
I had reviewed the help which was only a little help.

Sure, the problem was the help. It was of no help. Shocked
I just reviewed the help for memcopy but i do not understand how this can be confusing or be of little help?
So something in the help did you think it was working different than you expected. What I wonder?

_________________
Mark
Back to top
View user's profile Visit poster's website
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Sun Oct 05, 2014 12:10 am    Post subject: Reply with quote

HI Mark,

Please understand I am/was not throwing rocks at you or the tools you created - they ARE really GREAT! I cannot wait for the next tool set to become available - I will be one of the first to buy it! The problem was not with the help, and it was *of* help; but it would have been of even more help had it had just a tiny bit more.

I will try to explain specifically what I mean. This is from the MEMCOPY in help.

"///
option
An optional numeric constant with one of the following values :
1 - only the source address will be increased after each copied byte
2 - only the target address will be increased after each copied byte
3 - both the source and target address will be increased after each copied byte
"///
What is meant by "increased"?? Does this mean "changed to"? Or "incremented"?



"///
Dim Ars(10) As Byte 'source bytes
Dim Art(10) As Byte 'target bytes
Dim J As Byte 'index

For J = 1 To 10 'fill array
Ars(j) = J
Next
J = Memcopy(ars(1) , Art(1) , 4) 'copy 4 bytes
Print J ; " bytes copied"
"///
what the new value of Art(1) Art(2) Art(3) Art(4) ? Is it the value 1, 2, 3, 4 or all 1's - I am 99% sure it is 1234.


"///
Dim W As Word , L As Long
W = 65511
J = Memcopy(w , L , 2) 'copy 2 bytes from word to long
End
"///
What is the new value of L?

If the result was shown, ie what the new value of Art(1) Art(2) Art(3) Art(4) and L becomes, it would be clearer - at least to me.


"///
For J = 1 To 10
Print Art(j)
Next
J = Memcopy(ars(1) , Art(1) , 10 , 2) 'assign them all with element 1
"///
In this case, when using option 2 how is the second array Art(1) set to the value of element 1? option 2 says that the target address (Art()) is "incremented", so how it is set to a 1 in each element of its array?


If this is obvious to everyone else, please excuse my ignorance. And, perhaps this is deemed redundant, obvious or something else; but when fighting with a problem and one referrs to the help, which is what it is there for, the expected result is absolutely spelled out, leaving nothing to ambiguity. Having that absolute, would have said to me, that to look elsewhere in my code to find the problem.

In the above cases, the two things that are confusing are the words "increased" in the options and in the last example understanding how they call get to a 1. Finally in this same help example, it says "MEMCOPY can also be used to clear an array quickly". How? After now having used this, I suppose I can assume that both the target and/or the source would be set to some new value like 255, or 0 etc. But spelling it out would complete the effort you made when you created and updated the Help.


As a final example, from the help for MID:
Dim S As String * 15 , Z As String * 15
S ="ABCDEFG"
Z = Left(s , 5)
Print Z 'ABCDE

This spells out that the value of Z will be "ABCDE"! Leaving nothing to guess.


Again Mark, I hope you do not take offense to the comments. In the above, you asked for a response and that is what I have tried to provide.

Thank you,
Tim
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5199
Location: Holland

blank.gif
PostPosted: Sun Oct 05, 2014 12:16 pm    Post subject: Reply with quote

- first of all, when having doubts of what is meant in the help : ask. it is the only way to clear it up and to improve it.
Quote:
What is meant by "increased"?? Does this mean "changed to"? Or "incremented"?

To my knowledge when i have something, and it increases, i will end up with more. So it will both change since it is not the same as before and it will be higher than before. incremented is indeed a better word.
And incremented by what? by 1 since we use bytes.

- sample : you leave out part of the help : "By default, option 3 is used as this will copy a block of memory from one memory location to another location."
So this will copy by increasing both source and target address. you will end up with 2 duplicate arrays.

Quote:
What is the new value of L?

why dont you simply test that? you can see it in the simulator. but it should not be a surprise. You should do some reading on data types and how much memory they use. off course L will always be the same as W !
The memcopy topic would be the wrong one to explain the result. But since a word is 2 bytes, and a long 4 bytes, the long is big enough to hold any value of W.

When we get a lot of questions about a statement/topic we change the help. This will result in less support questions and a better help. But there is no intention to go to a level of lego block help where i spell out how 3 blocks would look like when i stack them together. I know the help is never finished and each version we add more info, but there is a limit to which level of explaining we go.

when memcopy is still not clear, then let me know.

_________________
Mark
Back to top
View user's profile Visit poster's website
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Sun Oct 05, 2014 6:44 pm    Post subject: Reply with quote

Thank you Mark,
Tim
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive 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