Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Is this a sane/safe way to pass an aref into a C function?

by Anonymous Monk
on Jan 21, 2017 at 19:55 UTC ( [id://1180090]=note: print w/replies, xml ) Need Help??


in reply to Is this a sane/safe way to pass an aref into a C function?

Hm. A sequence of bytes could be an array in perl but in C it is generally known as a string. :^)

So you could just pass a string and read it with SvPV. Although I suppose it depends on your concept of the channel. If the bytes are going to have an individual meaning, like a value sampling, then perhaps the array model is appropriate.

Replies are listed 'Best First'.
Re^2: Is this a sane/safe way to pass an aref into a C function?
by stevieb (Canon) on Jan 21, 2017 at 21:23 UTC

    Thanks Anonymonk, the bytes most definitely have individual meaning... MSByte is a control register (which actually consists of two 4-bit nibbles, but I dissect that later in the chain), and LSByte is the data register, both of a larger 16-bit command register, which gets sent to a piece of hardware over the Serial Peripheral Interface.

    Example of what I was originally toying with:

    # perl testing(65535, 4); ... // C #include <stdio.h> int testing(int n, int len){ unsigned char bytes[4]; bytes[3] = (n >> 24) & 0xFF; bytes[2] = (n >> 16) & 0xFF; bytes[1] = (n >> 8) & 0xFF; bytes[0] = n & 0xFF; printf("output should be 255, 255, 0, 0\n\n"); printf("%d, %d, %d, %d\n", bytes[0], bytes[1], bytes[2], bytes[3]) +; return 0; }

    Works fine. The problem though is that the buf array is eventually sent to another C function that puts it on the SPI bus. The way I've done it in the above example limits me to accept/send only a single 4-byte array, so if a device requires say 6 bytes, I'd have to add in another param (at least that's how I saw it). I wanted my Perl code to be as compatible with the API I'm using as possible, and heck, I've always wanted to learn how to pass in an array/aref to a C function anyway, so it was a fun learning experience :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1180090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-03-28 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found