Python ctype help: working with C pointers unsigned char C

My first post, please take it with you. :) I am also a little new to Python, but I like what I see so far. I am trying to access the c library, which allows me to print receipts to the printer through Python. I am creating a wrapper in Python using ctypes and everything is going fine, EXCLUSIVE for two functions. Here are our prototypes:

int C56_api_printer_write(int printer, unsigned char * data, int size, unsigned long timeout_ms); int C56_api_printer_read(int printer, unsigned char * data, int size, unsigned long timeout_ms); 

My problem is writing and reading from unsigned char pointers using ctypes. I have to read in a raster file in Python and pass the array to the write function, or in the case of reading, I need to read this pointer as an integer array.

In the past few hours, I have been cheating on this, so I was hoping the expert could help by posting a simple example of how this can be done.

Thanks!

+4
source share
2 answers

Does the following help you? Please let me know if this gives you errors or I misunderstood your question:

 size = printer = timeout = data = (ctypes.c_ubyte * size)() # line 5 C56_api_printer_read(printer, data, size, timeout) # manipulate data eg data[3] = 7 C56_api_printer_write(printer, data, size, timeout) 

Edit:

About line 5: See also http://docs.python.org/library/ctypes.html sections 15.17.1.13 and 15.17.1.20.

 (ctypes.c_ubyte * size) 

gives a function that builds an array of ctypes of length size. Then, on this line, I call the function with no arguments, calling zero initialization.

+5
source

Well, after helping Kyss, I was able to finish this. Here is my test code and the result to complete this problem:

My test.c code is:

 #include <stdio.h> int test(unsigned char *test, int size){ int i; for(i=0;i<size;i++){ printf("item %d in test = %d\n",i, test[i]); } } int testout(unsigned char *test, int *size){ test[2]=237; test[3]=12; test[4]=222; *size = 5; } main () { test("hello", 5); unsigned char hello[] = "hi"; int size=0; int i; testout(hello,&size); for(i=0;i<size;i++){ printf("item %d in hello = %d\n",i, hello[i]); } } 

I created the main part for testing my c-function. Here's the output of the function test:

 item 0 in test = 104 item 1 in test = 101 item 2 in test = 108 item 3 in test = 108 item 4 in test = 111 item 0 in hello = 104 item 1 in hello = 105 item 2 in hello = 237 item 3 in hello = 12 item 4 in hello = 222 

Then I compiled for sharing, so it could be used from python:

 gcc -shared -o test.so test.c 

And here is what I used for my Python code:

 from ctypes import * lib = "test.so" dll = cdll.LoadLibrary(lib) testfunc = dll.test print "Testing pointer input" size = c_int(5) param1 = (c_byte * 5)() param1[3] = 235 dll.test(param1, size) print "Testing pointer output" dll.testout.argtypes = [POINTER(c_ubyte), POINTER(c_int)] sizeout = c_int(0) mem = (c_ubyte * 20)() dll.testout(mem, byref(sizeout)) print "Sizeout = " + str(sizeout.value) for i in range(0,sizeout.value): print "Item " + str(i) + " = " + str(mem[i]) 

And the conclusion:

 Testing pointer input item 0 in test = 0 item 1 in test = 0 item 2 in test = 0 item 3 in test = 235 item 4 in test = 0 Testing pointer output Sizeout = 5 Item 0 = 0 Item 1 = 0 Item 2 = 237 Item 3 = 12 Item 4 = 222 

Work!

My only problem now is with dynamically resizing the c_ubyte array based on the size of the output. However, I posted a separate question.

Thanks for your help Kyss!

+4
source

Source: https://habr.com/ru/post/1399433/


All Articles