Shop OBEX P1 Docs P2 Docs Learn Events
Question on converting passing LONG pointer arrays, then copying to BYTE arrays — Parallax Forums

Question on converting passing LONG pointer arrays, then copying to BYTE arrays

Chuck RiceChuck Rice Posts: 210
edited 2008-04-16 03:41 in Propeller 1
.

Is the gotcha comment below correct, given that packetArg and packetBuf are defined elsewhere (in another object) as

long packetArg[noparse][[/noparse]g#maxPacketArgs]
  byte  packetBuf[noparse][[/noparse]g#maxData]



and are called like:

longfill(@packetArg,0,g#maxPacketArgs)
  packetArg[noparse][[/noparse]g#pSource]:= cMyAddress
  packetArg[noparse][[/noparse]g#pDest]  := dest
  rs[noparse][[/noparse]dest].sendPacket(command,g#query,@packetArg)         




I am away from my propeller right now, but I think that this is what has been causing
my problem with the dest field being set to $00 instead of the passed value of $01.
I was using byte instead of long in the pfSource and PFDest header bytes.

Is this correct or am I missing something basic?

[b]pub[/b] sendPacket(packetFunc,packetType,packetArgPtr)   | pBufPtr,pDataPtr,argPtr,n

'   Build Packet Header 
'     gotcha here. The destination is a byte, but the
'     argument is passed in a long array. So the index
'     into the passed argument array must be long bassed,
'     or multiplied by 4 to be byte based.  
 
    [b]byte[/b][noparse][[/noparse]pBufBase][noparse][[/noparse]g#pfStart ] := g#stx                 'Set Start-Of-Transmission
    [b]byte[/b][noparse][[/noparse]pBufBase][noparse][[/noparse]g#pfSource] := [b]long[/b][noparse][[/noparse]packetArgPtr][noparse][[/noparse]g#pSource]
    [b]byte[/b][noparse][[/noparse]pBufBase][noparse][[/noparse]g#pfDest  ] := [b]long[/b][noparse][[/noparse]packetArgPtr][noparse][[/noparse]g#pDest]
    [b]byte[/b][noparse][[/noparse]pBufBase][noparse][[/noparse]g#pfFunc  ] := packetFunc
    [b]byte[/b][noparse][[/noparse]pBufBase][noparse][[/noparse]g#pfType  ] := packetType

'   Build Packet Data Section
    argPtr  := g#pArg1                                   'Set to first argument                       
    pBufPtr := pBufBase + g#pfData


    [b]repeat[/b]
      [b]if[/b] [b]long[/b][noparse][[/noparse]packetArgPtr][noparse][[/noparse]argPtr] <> 0                'Does this arg point to any data?
        pDataPtr :=  [b]long[/b][noparse][[/noparse]packetArgPtr][noparse][[/noparse]argPtr]         'Get the address of the data to send from the argument
        n := [b]strsize[/b]([b]long[/b][noparse][[/noparse]packetArgPtr][noparse][[/noparse]argPtr])        'Calc its length
        [b]bytemove[/b](pBufPtr , pDataPtr , n)                'move the arg data into the buffer
        pBufPtr += n                                    'add the length of the data to the buffer pointer
    [b]while[/b] [b]long[/b][noparse][[/noparse]packetArgPtr][noparse][[/noparse]++argPtr] <> 0

    
'   Build Packet Trailer
    [b]byte[/b][noparse][[/noparse]pBufPtr] := g#ETX                              'Add End-Of-Transmission byte
    
'   Send the Packet
    pBufPtr := pBufBase
    [b]repeat[/b]
      tx([b]byte[/b][noparse][[/noparse]pBufBase][noparse][[/noparse]pBufPtr++])  
    [b]while[/b] [b]byte[/b][noparse][[/noparse]pBufBase][noparse][[/noparse]pBufPtr] <> g#ETX





The Enumerations defined in my global object are:
  #1                            'Packet Function Enumeration                           
  GetGPSinfo
  GetGPSinfo2
  stopMotors
  GetCogs
  sendPosition
  getScanMap
  setTargetLatLon
  getDistanceAndBearing
  doTest
  steer
  identify
  
  
  #1                            ' Source and Destination Enumerations
  NavBoard
  MotBoard
  ComBoard
  xxxBoard

  #$F0                          ' Enumeration
  STX
  ETX
     
  #0                            ' Packet Field Enumeration
  pfStart                       '    Packet Start
  pfSource                      '    Packet Source Address
  pfDest                        '    Packet Destination Address
  pfFunc                        '    Packet Function
  pfType                        '    Packet Type
  pfData                        '    Packet Payload
  pfEOT                         '    Packet End of Transmission
                          
  #0
  pSource                       'Packet Argument Enumeration
  pDest                         
  pArg1
  pArg2
  pArg3
  pArg4
  pArg5                


Post Edited (Chuck Rice) : 4/16/2008 3:40:28 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-16 03:29
    If I understand your question, the answer is yes. If you're passing the address of a long, you would need to use long[noparse][[/noparse]...][noparse][[/noparse]...] to reference it because the second subscript is multiplied by 4 and added to the first "subscript" to get the full address.
  • Chuck RiceChuck Rice Posts: 210
    edited 2008-04-16 03:41
    .
    Thanks. I will sleep better tonight! [noparse]:)[/noparse]
    .
Sign In or Register to comment.