Python——调用C数组

1、运行collect工程中的Main.c,得到collect.dll;
2、运行collect.py,调用Main.c中的数组;

Main.c代码如下:

#include <stdio.h>
#include <time.h>
#define DLLEXPORT extern _declspec(dllexport)

DLLEXPORT int main(unsigned short* buffer, unsigned short row, unsigned short col)
{
	unsigned short n, i, row_, col_;

	for (n=0;n<12;n++)
	{
		for (i=0;i<row*col;i++)
		{
			row_ =  i / col;
			col_ = i % col;
			if (row_ == 1 && col_ > 0 && col_ < 3)
			{
				buffer[i] = n;
			}
			else
			{
				buffer[i] = 0;
			}
		}
		Sleep(1000);
	}
	return 0;
}

collect.py代码如下:

import time
import numpy as np
from threading import Thread

from ctypes import *

row = 3
col = 4
buffer = np.ones(row*col, dtype="uint16")

dll = CDLL('x64\\Debug\\collect.dll')
main = dll.main
main.argtype = [POINTER(c_ushort), c_ushort, c_ushort]

long_run_thread = Thread(target = main, args = (buffer.ctypes.data_as(POINTER(c_ushort)), row, col))
long_run_thread.start()

time.sleep(0.1)
for i in range(12):
	print(buffer)
	time.sleep(1)

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值