{"id":50,"date":"2020-05-10T16:51:39","date_gmt":"2020-05-10T08:51:39","guid":{"rendered":"https:\/\/jiangshibo.cn\/?p=50"},"modified":"2021-06-14T23:13:45","modified_gmt":"2021-06-14T15:13:45","slug":"python-%e8%b0%83%e7%94%a8%e5%8a%a8%e6%80%81%e9%93%be%e6%8e%a5%e5%ba%93%e6%95%99%e7%a8%8b%ef%bc%88return-%e6%8c%87%e9%92%88%ef%bc%89","status":"publish","type":"post","link":"https:\/\/jiangshibo.cn\/index.php\/2020\/05\/10\/python-%e8%b0%83%e7%94%a8%e5%8a%a8%e6%80%81%e9%93%be%e6%8e%a5%e5%ba%93%e6%95%99%e7%a8%8b%ef%bc%88return-%e6%8c%87%e9%92%88%ef%bc%89\/","title":{"rendered":"Python \u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u6559\u7a0b\uff08return\/\u6307\u9488\/\u5168\u5c40\u53d8\u91cf\uff09"},"content":{"rendered":"<h1>Python \u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u6559\u7a0b\uff08return\/\u6307\u9488\/\u5168\u5c40\u53d8\u91cf\uff09<\/h1>\n<p>[TOC]<br \/>\n<!-- TOC --><\/p>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#1-\u524d\u8a00\">1. \u524d\u8a00<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#2-c\u8bed\u8a00\u5b9e\u73b0\u90e8\u5206\">2. C\u8bed\u8a00\u5b9e\u73b0\u90e8\u5206<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#3-python-\u6d4b\u8bd5\u90e8\u5206\u4ee3\u7801\">3. Python \u6d4b\u8bd5\u90e8\u5206\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p><!-- \/TOC --><\/p>\n<p><!--more--><\/p>\n<h1>1. \u524d\u8a00<\/h1>\n<p>\u5f53\u4e0bPython\u5728\u6c7d\u8f66\u7535\u5b50\u884c\u4e1a\u8d8a\u6765\u8d8a\u706b\uff0c\u5c24\u5176\u5728\u5404\u5bb6\u4e0a\u4e86AUTOSAR\u4e4b\u540e\uff0c\u7531\u4e8e\u5176\u7e41\u7410\u7684\u5f00\u53d1\u6d41\u7a0b\u4ee5\u53ca\u76f8\u5173\u5de5\u5177\u5c1a\u672a\u5b8c\u5584\u7684\u73b0\u72b6\uff0c\u5bfc\u81f4\u9700\u8981\u8fdb\u884c\u4e00\u7cfb\u5217\u7684\u5de5\u5177\u5316\uff0c\u81ea\u52a8\u5316\uff0c\u5de5\u5177\u94fe\u5316\u7684\u5de5\u4f5c\uff0cPython\u4f5c\u4e3a\u8fd1\u51e0\u5e74\u706b\u6c14\u6765\u7684\u8bed\u8a00\uff0c\u52a0\u4e4b\u7f51\u7edc\u4e0a\u5df2\u7ecf\u5b58\u5728\u5f88\u591a\u6c7d\u8f66\u7535\u5b50\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9700\u8981\u7684\u8f6e\u5b50\uff08\u4f8b\u5982 canmatrix \u53ef\u4ee5\u4e00\u952e\u8f6c\u6362dbc\u4e3aexcel\u7b49\uff09\uff0c\u5bfc\u81f4\u8eab\u8fb9\u7684\u540c\u4e8b\uff0c\u4ee5\u53ca\u4e0d\u540c\u516c\u53f8\u7684\u540c\u884c\uff0c\u4e0d\u7ea6\u800c\u540c\u7684\u91c7\u7528python\u4f5c\u4e3a\u4e3b\u529b\u5de5\u5177\u5f00\u53d1\u8bed\u8a00\u3002<\/p>\n<p>\u5728\u9879\u76ee\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u514d\u5b58\u5728\u4e0e\u4f9b\u5e94\u5546\u8fdb\u884c\u4ea7\u54c1\u7684\u5408\u4f5c\u5f00\u53d1\u60c5\u51b5\uff0c\u672c\u6559\u7a0b\u4ecb\u7ecd\u7684\u65b9\u6848\u662f\u57fa\u4e8e\u6b64\u573a\u666f\u4e0b\uff0cC\u8bed\u8a00\u7f16\u5199\u4e86\u9700\u8981\u4fdd\u5bc6\u7684\u6838\u5fc3\u7b97\u6cd5\uff0cPython\u5f00\u53d1\u76f8\u5e94\u7684\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u8fdb\u884c\u4e24\u90e8\u5206\u7684\u8054\u5408\u8c03\u7528\u3002<\/p>\n<h1>2. C\u8bed\u8a00\u5b9e\u73b0\u90e8\u5206<\/h1>\n<ul>\n<li>testDll.c<\/li>\n<\/ul>\n<pre><code class=\"language-c \">\/*-----------------------------------------------------------------------------\n\/*   Testing file for dll and python combine\n\/*  \n\/*   File         : testDll.c\n\/*   Complier     : gcc  Ver. &gt; 4.9\n\/*   Author       : Tomato \n\/*   Time         : 2019\/8\/23\n\/*   Instructions : New file                                           - 0.1    \n\/*                  Add global parameter  gTestData                    - 0.2\n\/*                 \n\/*---------------------------------------------------------------------------*\/\n\n\/* Incluce files *\/\n#include \"testDll.h\"\n\n\/* Parameters *\/\nuint8 gTestData = 66u;\n\n\/* Function Declation *\/\nvoid __TestFunc(uint8 * arrayHead, uint8 arraySize);\nuint8 __TestFuncRtn(uint8 argv1, uint8 argv2);\n\nvoid main()\n{\n    \/* *\/\n}\n\n\/* Function start----------------------------------------------------------- *\/\nvoid __TestFuncAry(uint8 * arrayHead, uint8 arraySize)\n{\n    for (uint8 i = 0; i &lt; arraySize; i++)\n    {\n        arrayHead[i] ++;\n    }\n}\n\n\/* End of function---------------------------------------------------------- *\/\n\n\/* Function start----------------------------------------------------------- *\/\nuint8 __TestFuncRtn(uint8 argv1, uint8 argv2)\n{\n    uint8 temp_vle = 0u;\n    temp_vle = argv1 + argv2;\n    return temp_vle;\n}\n\n\/* End of function---------------------------------------------------------- *\/\n\n\/* Function start----------------------------------------------------------- *\/\nuint8 GetGblData(void)\n{\n    return gTestData;\n}\n\n\/* End of function---------------------------------------------------------- *\/\n\n\n<\/code><\/pre>\n<ul>\n<li>testDll.h<\/li>\n<\/ul>\n<pre><code class=\"language-c \">\/*-----------------------------------------------------------------------------\n\/*   Testing file for dll and python combine\n\/*  \n\/*   File         : testDll.h\n\/*   Complier     : gcc  Ver. &gt; 4.9\n\/*   Author       : Tomato\n\/*   Time         : 2019\/8\/23\n\/*   Instructions : New file                                           - 0.1    \n\/*                  Add global parameter                               - 0.2        \n\/*                 \n\/*---------------------------------------------------------------------------*\/\n\n#ifndef _TESTDLL_H\n#define _TESTDLL_H\n\n\/* Alies type define *\/\ntypedef signed char int8_T;\ntypedef unsigned char uint8_T;\ntypedef short int16_T;\ntypedef unsigned short uint16_T;\ntypedef int int32_T;\ntypedef unsigned int uint32_T;\ntypedef float real32_T;\ntypedef double real64_T;\ntypedef double real_T;\ntypedef double time_T;\ntypedef unsigned char boolean_T;\n\n\/* AUTOSAR Base to Platform types mapping *\/\ntypedef boolean_T boolean;\ntypedef int16_T sint16;\ntypedef int32_T sint32;\ntypedef int8_T sint8;\ntypedef uint16_T uint16;\ntypedef uint32_T uint32;\ntypedef uint8_T uint8;\ntypedef real32_T float32;\ntypedef real_T float64;\n\n\/* Function Declation *\/\nextern void __TestFuncAry(uint8 * arrayHead, uint8 arraySize);\nextern uint8 __TestFuncRtn(uint8 argv1, uint8 argv2);\nextern uint8 GetGblData(void);\n\n\/* Variable Declation*\/\nextern uint8 gTestData;\n\n#endif\n<\/code><\/pre>\n<p>\u672c\u793a\u4f8b\u4ec5\u5b58\u5728\u4e00\u4e2a.c\u6587\u4ef6\uff0c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u4f1a\u5b58\u5728\u5f88\u591a\u6e90\u4ee3\u7801\u6587\u4ef6\uff0c\u6240\u4ee5\u6700\u597d\u7684\u65b9\u5f0f\u662f\u628ac\u7f16\u8bd1\u6210\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u8fdb\u884c\u7edf\u4e00\u8c03\u7528\u3002<\/p>\n<p>\u9488\u5bf9\u672c\u793a\u4f8b\u91c7\u7528\u5982\u4e0b\u547d\u4ee4\u8fdb\u884c\u52a8\u6001\u94fe\u63a5\u5e93\u7684\u7f16\u8bd1(\u524d\u63d0\u662f\u7535\u8111\u8981\u5b89\u88c5gcc\u7f16\u8bd1\u5668)<br \/>\n- windows<\/p>\n<pre><code class=\"\">gcc -fPIC -shared testDll.c -o testDll.dll\n<\/code><\/pre>\n<ul>\n<li>linux<\/li>\n<\/ul>\n<pre><code class=\"\">gcc -fPIC -shared testDll.c -o testDll.so\n<\/code><\/pre>\n<p>\u82e5\u4e3a\u591a\u4e2a\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4ee5\u7f16\u5199makefile\u3002<\/p>\n<h1>3. Python \u6d4b\u8bd5\u90e8\u5206\u4ee3\u7801<\/h1>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8ec\u548cPython\u4e2d\u6570\u636e\u7c7b\u578b\u5e76\u4e0d\u901a\u7528\uff0c\u5982\u679c\u4f7f\u7528\u6570\u7ec4\uff08\u5730\u5740\u8fde\u7eed\uff0c\u4e14\u5404\u5143\u7d20\u6570\u636e\u7c7b\u578b\u7edf\u4e00\uff09\u7b49\u590d\u6742\u6570\u636e\u7c7b\u578b\u9700\u8981\u5148\u8fdb\u884c\u8f6c\u6362\u7b49\u524d\u5904\u7406\u3002<\/p>\n<p>\u672c\u6559\u7a0b\u4f7f\u7528\u7684\u662fnumpy\u6a21\u5757\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff0c\u7a0b\u5e8f\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>Python<\/li>\n<\/ul>\n<pre><code class=\"language-python \"> #!\/usr\/bin\/env python3\n# -*- coding: utf-8 -*-\n# -----------------------------------------------------------------------------\n#    Testing file for dll\/so and python combine\n#   \n#    File         : testPyWithC.py\n#    Author       : Tomato\n#    Time         : 2019\/8\/23\n#    Instructions : New file                                           - 0.1   \n#                   Add global parameter access and setting            \n#                                    2021\/6\/13   Tomato                - 0.2         \n#                  \n# -----------------------------------------------------------------------------\n\n# Import files\nfrom ctypes import *\nimport numpy as np\n\n# load the shared object file ,*.so(linux) or *.dll(windows)\ntestLib = CDLL('.\/testDll.dll')\n\n# test normal input, datatype implict truned\ntestRtn = testLib.__TestFuncRtn(15,20)\nprint(testRtn)\n\n# test array ,should convert datatype first------------------------------------\nstrInput = input(\"Please input testing array eg.50 60: \")\n# Turn string input to number\ninput_list = strInput.split(' ')\nnum_list = list()\nfor data in input_list:\n    num_list.append(int(data))\n\n# Judge generate array's memmroy whether is continues\n# \u5224\u65ad\u4f7f\u7528numpy\u751f\u6210\u7684uint8\u7c7b\u578b\u6570\u7ec4\uff0c\u5728\u5185\u5b58\u4e2d\u5730\u5740\u662f\u5426\u8fde\u7eed\uff0c\u5982\u679c\u4e0d\u8fde\u7eed\u5219\u9700\u8981\u8fdb\u884c\u5f3a\u5236\u8fde\u7eed-\u91cd\u8981\uff01\ntestArray = np.asarray(num_list, dtype=np.uint8).reshape(-1)\nif not testArray.flags['C_CONTIGUOUS']:\n    testArray = testArray.ascontiguous(testArray, dtype=testArray.dtype)\n\n# Turn to ctypes pointer\n# \u8f6c\u6362\u4e3actypes\u6307\u9488\uff0c\u5bf9\u5e94\u4e8ec\u8bed\u8a00\u4e2d\u7684uint8 *\ntestArrayPtr = cast(testArray.ctypes.data, POINTER(c_uint8))\n\n# Get array length\naryLen = len(num_list)\n\n# Testing c function with pointer used\ntestLib.__TestFuncAry(testArrayPtr,aryLen)\n\nfor i in range(aryLen):\n    print(testArrayPtr[i])\n\n# Testing global parameter between c\/python\ngTestData = c_uint8.in_dll(testLib, 'gTestData')\nprint(gTestData)\ngTestData = 77\nc_uint8.in_dll(testLib, 'gTestData').value = gTestData\ntp_rtn = testLib.GetGblData()\nprint(tp_rtn)\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python \u8c03\u7528\u52a8\u6001\u94fe\u63a5\u5e93\u6559\u7a0b\uff08return\/\u6307\u9488\/\u5168\u5c40\u53d8\u91cf\uff09 [TOC] 1. \u524d\u8a00 2. C\u8bed\u8a00\u5b9e\u73b0\u90e8\u5206 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/50"}],"collection":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":5,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions\/96"}],"wp:attachment":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}