﻿{"id":300,"date":"2020-12-19T16:32:17","date_gmt":"2020-12-19T08:32:17","guid":{"rendered":"https:\/\/dt9025a.xyz\/?p=300"},"modified":"2022-04-24T21:01:32","modified_gmt":"2022-04-24T13:01:32","slug":"keil-c51%e5%b7%a5%e7%a8%8b%e9%80%82%e9%85%8dvscode%e7%8e%af%e5%a2%83","status":"publish","type":"post","link":"https:\/\/dt9025a.top\/?p=300","title":{"rendered":"Keil C51\u5de5\u7a0b\u9002\u914dVSCode\u73af\u5883"},"content":{"rendered":"\n<p>Keil\uff0c\u61c2\u5f97\u90fd\u61c2\uff0c\u4ee3\u7801\u8865\u5168\u5c31\u6ca1\u6709\u3002<\/p>\n\n\n\n<p>\u539f\u7406\u5f88\u7b80\u5355\uff0c\u914d\u7f6e\u597d.vscode\u6587\u4ef6\u5939\u4e0b\u7684\u4e24\u4e2ajson\u5c31\u884c\u3002<\/p>\n\n\n\n<p>\u4f46\u6211\u61d2\uff0c\u4e00\u4e2a\u5de5\u7a0b\u914d\u4e00\u904d\u70e6\u6b7b\u4e86\u3002<\/p>\n\n\n\n<p>\u6b63\u597d\u5b66\u4e60\u4e00\u4e0bpython\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<p>\u5355\u7a0b\u5e8f\u6587\u4ef6\uff0c\u653e\u5230\u76ee\u6807\u6587\u4ef6\u5939\u4e0b\u6267\u884c\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># coding=utf-8\n\"\"\"\n    Keil\u5de5\u7a0b\u6587\u4ef6\u5939\u9002\u914dVSCode\n    Author: DT9025A\n\"\"\"\nimport xml.dom.minidom as xml\nimport re as regex\nimport os.path as path\nimport os\n\n\nclass Target:\n    target_name = ''\n    toolset_name = ''\n    target_device = ''\n    target_vendor = ''\n    target_cpu = ''\n    create_batch_file = '0'\n\n    def __init__(self, xml_result_elements):\n        Target.target_name = xml_result_elements.getElementsByTagName('TargetName')[0].firstChild.data\n        Target.toolset_name = xml_result_elements.getElementsByTagName('ToolsetName')[0].firstChild.data\n        xml_result_elements = xml_result_elements.getElementsByTagName('TargetOption')[0]. \\\n            getElementsByTagName('TargetCommonOption')[0]\n        Target.target_device = xml_result_elements.getElementsByTagName('Device')[0].firstChild.data\n        Target.target_vendor = xml_result_elements.getElementsByTagName('Vendor')[0].firstChild.data\n        Target.target_cpu = xml_result_elements.getElementsByTagName('Cpu')[0].firstChild.data\n        Target.create_batch_file = xml_result_elements.getElementsByTagName('CreateBatchFile')[0].firstChild.data\n\n\ndef read_uvproj(file_path):\n    file_result = None\n    file_found = False\n    file_list = os.listdir(file_path)\n\n    for filename in file_list:\n        match_result = regex.match(\".*?uvproj$\", filename)\n        if match_result is not None:\n            file_result = match_result.string\n            file_found = True\n            break\n    if file_found is True:\n        file_result = path.join(file_path, file_result)\n        xml_result_tree = xml.parse(file_result)\n        xml_result_elements = xml_result_tree.getElementsByTagName('Project')\n        if xml_result_elements.length != 0:\n            xml_result_elements = xml_result_elements[0].getElementsByTagName('Targets')\n            if xml_result_elements.length > 0:\n                if xml_result_elements.length != 1:\n                    print(f'\u6587\u4ef6 {file_result} \u4e2d\u6709\u591a\u4e2aTarget, \u8f6c\u6362\u7b2c\u4e00\u4e2a')\n                result = Target(xml_result_elements[0])\n                print(\"------\\n\u5c06\u8981\u8fdb\u884c\u8f6c\u6362\u7684Target:\\nTarget Name : %s\\nToolset Name : %s\\nTarget Device : %s\\n\"\n                      \"Target Vendor : %s\\nTarget CPU : %s\\nCreate Batch File : %s\\n------\" %\n                      (result.target_name, result.toolset_name, result.target_device,\n                       result.target_vendor, result.target_cpu, result.create_batch_file))\n                if result.create_batch_file != '1':\n                    print(\"\u672a\u5728Output\u52fe\u9009Create Batch File, \u70b9\u51fb\u8c03\u8bd5\u7f16\u8bd1\u529f\u80fd\u5c06\u4e0d\u53ef\u7528.\\n\"\n                          \"\u82e5\u60f3\u8981\u5728VSCode\u8fdb\u884c\u7f16\u8bd1, \u8bf7\u52fe\u9009\u4e0a\u8ff0\u9009\u9879\u540e\u5728Keil\u4e2d\u6267\u884c\u4e00\u6b21Build\")\n                return result\n            else:\n                print(f'\u6587\u4ef6 {file_result} \u4e2d\u6ca1\u6709Target')\n        else:\n            print(f'\u6587\u4ef6 {file_result} \u4e0d\u662f\u6709\u6548\u7684uvproj\u6587\u4ef6')\n    else:\n        print(f'\u76ee\u5f55 {file_path} \u4e0b\u6ca1\u6709uvproj\u6587\u4ef6')\n\n\nif __name__ == '__main__':\n    work_path = os.path.abspath(os.curdir)\n    vscode_conf_path = path.join(work_path, '.vscode')\n\n    print(\"\u8bfb\u53d6\u76ee\u5f55 %s \u4e0b\u7684uvproj\u6587\u4ef6...\" % work_path)\n    target = read_uvproj(work_path)\n    if target is None:\n        exit(0)\n\n    if path.exists(vscode_conf_path):\n        user_choice = input(\"\u8be5\u76ee\u5f55\u4e0b\u5df2\u7ecf\u6709VSCode\u914d\u7f6e\u6587\u4ef6\u5939, \u662f\u5426\u7ee7\u7eed? [y\/N] \")\n        if regex.fullmatch(\"[Nn]\", user_choice) or len(user_choice) == 0:\n            print(\"\u53d6\u6d88\u914d\u7f6e\")\n            exit(0)\n    else:\n        print(\"\u521b\u5efa\u6587\u4ef6\u5939 .vscode\")\n        os.makedirs(vscode_conf_path)\n\n    keil_dir = input(\"\u8f93\u5165Keil INC\u8def\u5f84 (\u4f8b\u5982C:\/Keil4\/C51\/INC) \")\n    if keil_dir.index('\\\\') != -1:\n        keil_dir = keil_dir.replace('\\\\', '\/')\n    if keil_dir[-1] == '\/':\n        keil_dir = keil_dir[0:-1]\n\n    file = open(path.join(vscode_conf_path, \"c_cpp_properties.json\"), \"w\", encoding='utf-8')\n    file.write(\"\"\"{\n    \"configurations\": [\n        {\n            \"name\": \"Keil\",\n            \"includePath\": [\n                \"${workspaceFolder}\/**\",\n                \"%s\/**\"\n            ],\n            \"defines\": [\n                \"_DEBUG\",\n                \"UNICODE\",\n                \"_UNICODE\",\n                \"__CC_ARM\",\n                \"sbit=unsigned char\",\n                \"sfr=unsigned char\",\n                \"pin=unsigned char\",\n                \"sfr16=unsigned char\",\n                \"bit=unsigned char\",\n                \"idata=\/**\/\",\n                \"bdata=\/**\/\",\n                \"data=\/**\/\",\n                \"xdata=\/**\/\",\n                \"code=\/**\/\",\n                \"at=\/**\/\",\n                \"interrupt=\/**\/\"\n            ],\n            \"cStandard\": \"c99\"\n        }\n    ],\n    \"version\": 4\n}\"\"\" % keil_dir)\n    file.close()\n    print(\"\u5199\u5165\u6587\u4ef6 c_cpp_properties.json\")\n\n    file = open(path.join(vscode_conf_path, \"launch.json\"), \"w\", encoding='utf-8')\n    file.write(\"\"\"{\n    \"version\": \"0.2.0\",\n    \"configurations\": [\n        {\n            \"name\": \"\u7f16\u8bd1\",\n            \"type\": \"cppvsdbg\",\n            \"request\": \"launch\",\n            \"program\": \"${workspaceFolder}\/%s.BAT\",\n            \"args\": [],\n            \"stopAtEntry\": false,\n            \"cwd\": \"${workspaceFolder}\",\n            \"environment\": [],\n            \"externalConsole\": false\n        }\n    ]\n}\"\"\" % target.target_name)\n    file.close()\n    print(\"\u5199\u5165\u6587\u4ef6 launch.json\")\n    input(\"\u5b8c\u6210\")\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Keil\uff0c\u61c2\u5f97\u90fd\u61c2\uff0c\u4ee3\u7801\u8865\u5168\u5c31\u6ca1\u6709\u3002 \u539f\u7406\u5f88\u7b80\u5355\uff0c\u914d\u7f6e\u597d.vscode\u6587\u4ef6\u5939\u4e0b\u7684\u4e24\u4e2ajson\u5c31\u884c\u3002 \u4f46\u6211\u61d2\uff0c\u4e00&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/dt9025a.top\/?p=300\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-5","category-9"],"_links":{"self":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300"}],"version-history":[{"count":0,"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions"}],"wp:attachment":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}