Pass an array to procedure

General discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
jmusselman64
Posts: 31
Joined: 15 May 2018 21:05

Pass an array to procedure

#1 Post by jmusselman64 » 08 Nov 2018 06:28

Quick question...how do I pass and use a Constant array to a procedure?

my array:

Code: Select all

Const PACKET AS BYTE[15]= (0x0E,0x00,0x03,0x11,0x0A,0x00,0x01,0x00,0x01,0x00,0x00,0x63,0xC7,0x00,0x00)
Thanks,
Jerry

User avatar
petar.suknjaja
mikroElektronika team
Posts: 683
Joined: 05 Mar 2018 09:44
Location: Belgrade

Re: Pass an array to procedure

#2 Post by petar.suknjaja » 08 Nov 2018 14:49

Hi Jerry,
You can use them like any regular arrays.
There's a little chapter on arrays and const arrays in the Help Section of the compiler.

If you had something more specified on your mind, post the code here.

Kind regards,
Petar

jmusselman64
Posts: 31
Joined: 15 May 2018 21:05

Re: Pass an array to procedure

#3 Post by jmusselman64 » 08 Nov 2018 15:34

Hi Petar,
This is what I'm trying to accomplish...copying one of many constant array packets to a destination packet...

Code: Select all

Const PKT1 AS BYTE[14]= (0x00,0x03,0x11,0x0A,0x00,0x02,0x00,0x02,0x00,0x00,0x63,0x93,0x00,0x00)
Const PKT2 AS BYTE[8]= (0x00,0x02,0x01,0x04,0x00,0x04,0x00,0x00)
Const PKT3 as byte[10] = (0x00,0x02,0x05,0x06,0x08,0x00,0x00,0x02,0x00,0x00)
'..etc, maybe 20-30 different packets, all less than 15 words

dim Dest_Packet[15] as word

'Copy one of many source packets to the destination packet
sub procedure CopyPacket(dim source_packet as word)
    dim x as byte
    
    for x = 0 to sizeof(pktname)-1      'length of array
        Dest_Packet[x] = pktname[x]    'copy array
    next x
end sub

main:
     CopyPacket(PKT1)
end.

Thanks,
Jerry

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Pass an array to procedure

#4 Post by filip » 09 Nov 2018 16:05

Hi,

Try this :

Code: Select all

program Led_Blinking

Const PKT1 as byte[14]= (0x00,0x03,0x11,0x0A,0x00,0x02,0x00,0x02,0x00,0x00,0x63,0x93,0x00,0x00)

dim Dest_Packet as byte[14]

sub procedure CopyPacket(dim source_packet as ^ const byte)
  dim x as byte
  for x = 0 to (sizeof(PKT1) - 1)
    Dest_Packet[x] = source_packet^
    Inc(source_packet)
   next x
end sub

main:
  CopyPacket(@PKT1)
  nop
end.
Regards.
Filip.

jmusselman64
Posts: 31
Joined: 15 May 2018 21:05

Re: Pass an array to procedure

#5 Post by jmusselman64 » 10 Nov 2018 06:38

Thanks Petar, but I'm still having trouble. I've expanded the test program a bit so you can get a better idea of what I'm trying to do.
It's not finding the size of the passed CONST array...it thinks it's always 2 bytes.
Any suggestions? I tried several variations of pointers; nothing worked.
Thanks, Jerry

Code: Select all

program constarraytest

Const PKT1 as byte[14]= (0x00,0x03,0x11,0x0A,0x00,0x02,0x00,0x02,0x00,0x00,0x63,0x93,0x00,0x00)
Const PKT2 AS BYTE[8]= (0x00,0x02,0x01,0x04,0x00,0x04,0x00,0x00)
Const PKT3 as byte[10] = (0x00,0x02,0x05,0x06,0x08,0x00,0x00,0x02,0x00,0x00)
'...etc...maybe 20 different packet 'templates'


dim Dest_Packet as byte[14]
dim pktlen as byte

sub procedure CopyPacket(dim source_packet as ^ const byte)
  dim x as byte
  
  pktlen = sizeof(source_packet) 'this doesn't work, but needs to be something like 
                                 'this instead of 'sizeof(PKT1)', because the length of the source
                                 'packet can vary.   Also, I need to know this length in order to use it in the SEND routine later.
  
  for x = 0 to (pktlen - 1)
    Dest_Packet[x] = source_packet^
    Inc(source_packet)
   next x
end sub

sub procedure SendPacket(dim sendlength as byte)
dim x as byte

 for x= 0 to sendlength-1
   '   send the packet out over a serial line (after replacing a few 'placeholder' bytes in Dest_Packet)
 next x
 
end sub

main:
  CopyPacket(@PKT1) 'copy packet template to Dest_Packet
  SendPacket(pktlen)      'format and send
  nop
  CopyPacket(@PKT2)
  SendPacket(pktlen)
  nop
  CopyPacket(@PKT3)
  SendPacket(pktlen)
  ' etc...

end.

jmusselman64
Posts: 31
Joined: 15 May 2018 21:05

Re: Pass an array to procedure

#6 Post by jmusselman64 » 07 Dec 2018 22:33

Thanks Philip, but I'm still having trouble. I've expanded the test program a bit so you can get a better idea of what I'm trying to do.
It's not finding the size of the passed CONST array...it thinks it's always 2 bytes.
Any suggestions? I tried several variations of pointers; nothing worked.
Thanks, Jerry

Code: Select all
program constarraytest

Const PKT1 as byte[14]= (0x00,0x03,0x11,0x0A,0x00,0x02,0x00,0x02,0x00,0x00,0x63,0x93,0x00,0x00)
Const PKT2 AS BYTE[8]= (0x00,0x02,0x01,0x04,0x00,0x04,0x00,0x00)
Const PKT3 as byte[10] = (0x00,0x02,0x05,0x06,0x08,0x00,0x00,0x02,0x00,0x00)
'...etc...maybe 20 different packet 'templates'


dim Dest_Packet as byte[14]
dim pktlen as byte

sub procedure CopyPacket(dim source_packet as ^ const byte)
dim x as byte

pktlen = sizeof(source_packet) 'this doesn't work, but needs to be something like
'this instead of 'sizeof(PKT1)', because the length of the source
'packet can vary. Also, I need to know this length in order to use it in the SEND routine later.

for x = 0 to (pktlen - 1)
Dest_Packet[x] = source_packet^
Inc(source_packet)
next x
end sub

sub procedure SendPacket(dim sendlength as byte)
dim x as byte

for x= 0 to sendlength-1
' send the packet out over a serial line (after replacing a few 'placeholder' bytes in Dest_Packet)
next x

end sub

main:
CopyPacket(@PKT1) 'copy packet template to Dest_Packet
SendPacket(pktlen) 'format and send
nop
CopyPacket(@PKT2)
SendPacket(pktlen)
nop
CopyPacket(@PKT3)
SendPacket(pktlen)
' etc...

end.

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 General”