« Return to Thread: [Q] Howto Send/Return "unsigned char" arrays between "C" and "Perl5" Swig?

[Q] Howto Send/Return "unsigned char" arrays between "C" and "Perl5" Swig?

by Ricky Marek-2 :: Rate this Message:

Reply to Author | View in Thread

Hello,

I'm new to SWIG.

The Scenario:  I have some code that needs to interface with a low level driver that will receive or
send buffers through some hardware device (I2C in my case), and be able to run it from a Perl script.
Running native "C" code allows me to do all the required operations, but I'm required to run the same
from Perl.

The 'C' functions I want to be wrapped (and be visible to Perl) have the following signature:

        int i2c_write(I2C_HANDLE *handle, int count, unsigned char *input_buffer);
        int i2c_read(I2C_HANDLE *handle,  int count, unsigned char *output_buffer);

Both the input and output buffers will contain binary data (not strings) and are pointers to an array,
previously allocated with enough room to work.

As far I have seen with SWIG is that "char *" (or/and unsigned "char *") is dealt in a special way
that can be used as an array...

The interface from Perl that I expect is that I will prepare an Perl array containing the characters
to be sent, or receive an array with the characters received. Is this possible?

        @buffer = ( 10, 255, 23, 12, 70, 1 .. 255);
        $count = I2C::write($handle, @buffer);
        ...
        $count = I2C::read($handle, @buffer);
               
        foreach $i (@buffer(
        {
                print "Got $i\n";
        }

Also the use of "pack" or "unpack" is acceptable.

Currently I'm using a "carrays.i" for this task, which adds a lot of overhead on the perl scripts..
and also a small warning on the manual tells me not to use "char *" with "%array_functions"
(end of section 8.2.2)

[Q] Is it safe to use "%array_functions" with "unsigned char *"?

Thanks in advance

--- Ricky Marek.





     

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

 « Return to Thread: [Q] Howto Send/Return "unsigned char" arrays between "C" and "Perl5" Swig?