{"id":23,"date":"2024-12-25T20:12:40","date_gmt":"2024-12-25T12:12:40","guid":{"rendered":"http:\/\/www.bqmz001.top\/?p=23"},"modified":"2024-12-25T20:12:40","modified_gmt":"2024-12-25T12:12:40","slug":"ubuntu24-04-python-can%e5%ae%9e%e8%b7%b5%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.bqmz001.top\/index.php\/2024\/12\/25\/ubuntu24-04-python-can%e5%ae%9e%e8%b7%b5%e8%ae%b0%e5%bd%95\/","title":{"rendered":"Ubuntu24.04 + python-can\u5b9e\u8df5\u8bb0\u5f55"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u786c\u4ef6:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">canable&nbsp;v2.0(slcan<a href=\"https:\/\/so.csdn.net\/so\/search?q=%E5%9B%BA%E4%BB%B6&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">\u56fa\u4ef6<\/a>\uff0c\u652f\u6301canfd)<br>\u4e00\u4e2aj1900\u5c0f\u4e3b\u673a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a><a><\/a>\u7cfb\u7edf\uff1a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Ubuntu24.04\uff08\u5728Ubuntu\u548cxubuntu\u4e0a\u90fd\u53ef\uff09<br>python3.12<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a><a><\/a>\u7528\u5230\u7684Python\u5e93\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cantools==39.3.0\nmqtt==0.0.1\npaho-mqtt==1.6.1\npyserial==3.5\npython-can==4.2.0\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a><\/a><a><\/a>\u98df\u7528\u65b9\u6cd5\uff1a<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><a><\/a><a><\/a>1\u3001\u6302\u8f7d\u5185\u6838\u6a21\u5757\u548capt\u4e0b\u8f7d\u5305<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6302\u8f7d\u5185\u6838\u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo modprobe can\nsudo modprobe can_raw\nsudo modprobe slcan\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5can-utils<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install can-utils\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\uff08\u7384\u5b66\u4f46\u6709\u7528\uff0c\u4e0d\u8fdb\u884c\u4ee5\u4e0a\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6ca1\u6709\u6570\u636e\u8f93\u51fa\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5pip\u548cpython-venv<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install python3-pip\nsudo apt install python3-venv\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a><\/a><a><\/a>2\u3001\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f4\u63a5pip install \u5728python3.12\u4e2d\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>error: externally-managed-environment\n \n\u00d7 This environment is externally managed\n\u2570\u2500&gt; To install Python packages system-wide, try apt install\n    python3-xyz, where xyz is the package you are trying to\n    install.\n    \n    If you wish to install a non-Debian-packaged Python package,\n    create a virtual environment using python3 -m venv path\/to\/venv.\n    Then use path\/to\/venv\/bin\/python and path\/to\/venv\/bin\/pip. Make\n    sure you have python3-full installed.\n    \n    If you wish to install a non-Debian packaged Python application,\n    it may be easiest to use pipx install xyz, which will manage a\n    virtual environment for you. Make sure you have pipx installed.\n    \n    See \/usr\/share\/doc\/python3.11\/README.venv for more information.\n \nnote: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.\nhint: See PEP 668 for the detailed specification.\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u5c31\u6574\u4e2avenv\u5e72\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p $HOME\/.env &amp;&amp; python3 -m venv $HOME\/.env\/default  #\u4ee5default\u4e3a\u4f8b\n$HOME\/.env\/default  \/bin\/python -m pip install -r .\/requirements.txt\n$HOME\/.env\/default  \/bin\/python .\/test.py\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837python\u811a\u672c\u5c31\u597d\u8dd1\u4e86\u3002<br>\u5728\u6b64\u4e4b\u524d\uff0c\u8bb0\u5f97\u7ed9\u4f60\u7528\u7684canable\u7ed9\u4e86777\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chmod 777 \/dev\/ttyACM0\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a><\/a><a><\/a>\u9644\uff1apython\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code><em># This is a sample Python script.<\/em>\nimport asyncio\nimport time\nfrom typing import List\nimport can\nfrom can.notifier import MessageRecipient\nimport cantools\nimport serial.tools.list_ports\n\nimport paho.mqtt.client as mqtt\nimport json\n\nclient = mqtt.Client()\ndb = cantools.database.load_file('.\\test.dbc')\n\n\ndef on_connect(client, userdata, flags, rc):\n    print(f\"Connected with result code {rc}\")\n\n\nasync def main() -&gt; None:\n\n    client.on_connect = on_connect\n    client.username_pw_set(username=\"publisher\", password=\"123456\")\n    client.connect(\"127.0.0.1\", 1883, 60)\n    \"\"\"The main function that runs in the loop.\"\"\"\n    \n    with can.interface.Bus(bustype='slcan', channel='\/dev\/ttyACM0', bitrate=500000, ) as bus:\n        reader = can.AsyncBufferedReader()\n        listeners: List&#91;MessageRecipient] = &#91;\n            reader,\n        ]\n        loop = asyncio.get_running_loop()\n        notifier = can.Notifier(bus, listeners, loop=loop)\n        while True:\n            msg = await reader.get_message()\n            try:\n                print(msg)\n                encodeMsg = db.decode_message(msg.arbitration_id, msg.data)\n                print(encodeMsg)\n                sendData={'id':msg.arbitration_id,'data':encodeMsg}\n                client.publish(\"\/payload\",json.dump(encodeMsg))\n            except Exception as ex:\n                print(ex)\n                pass\n            pass\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u786c\u4ef6: canable&nbsp;v2.0(slcan\u56fa\u4ef6\uff0c\u652f\u6301canfd)\u4e00\u4e2aj1900\u5c0f\u4e3b\u673a \u7cfb\u7edf\uff1a Ub [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":1,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}