{"id":100,"date":"2021-08-07T14:42:22","date_gmt":"2021-08-07T06:42:22","guid":{"rendered":"https:\/\/jiangshibo.cn\/?p=100"},"modified":"2021-08-11T14:49:45","modified_gmt":"2021-08-11T06:49:45","slug":"%e6%a0%91%e8%8e%93%e6%b4%becan%e9%80%9a%e8%ae%af%e6%95%99%e7%a8%8b-mcp2515","status":"publish","type":"post","link":"https:\/\/jiangshibo.cn\/index.php\/2021\/08\/07\/%e6%a0%91%e8%8e%93%e6%b4%becan%e9%80%9a%e8%ae%af%e6%95%99%e7%a8%8b-mcp2515\/","title":{"rendered":"\u6811\u8393\u6d3eCAN\u901a\u8baf\u6559\u7a0b &#8211; MCP2515"},"content":{"rendered":"<h1>\u6811\u8393\u6d3eCAN\u901a\u8baf\u6559\u7a0b - MCP2515<\/h1>\n<p><!-- TOC --><\/p>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#1-\u6954\">1. \u6954<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#2-\u786c\u4ef6\u8fde\u63a5\u548c\u73af\u5883\u51c6\u5907\">2. \u786c\u4ef6\u8fde\u63a5\u548c\u73af\u5883\u51c6\u5907<\/a>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#21-\u786c\u4ef6\u8fde\u63a5\">2.1. \u786c\u4ef6\u8fde\u63a5<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#22-\u73af\u5883\u51c6\u5907\">2.2. \u73af\u5883\u51c6\u5907<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#23-\u68c0\u6d4bmcp2515\u662f\u5426\u88ab\u6b63\u786e\u6302\u8f7d\">2.3. \u68c0\u6d4bMCP2515\u662f\u5426\u88ab\u6b63\u786e\u6302\u8f7d<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#3-can-\u901a\u8baf\u8bbe\u7f6e\u53ca\u7a0b\u5e8f\u5f00\u53d1\">3. CAN \u901a\u8baf\u8bbe\u7f6e\u53ca\u7a0b\u5e8f\u5f00\u53d1<\/a>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#31-\u7cfb\u7edf\u547d\u4ee4\">3.1. \u7cfb\u7edf\u547d\u4ee4<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#32-c\u8bed\u8a00\u8c03\u7528socket\u63a5\u53e3\u8fdb\u884c\u5f00\u53d1\">3.2. c\u8bed\u8a00\u8c03\u7528socket\u63a5\u53e3\u8fdb\u884c\u5f00\u53d1<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"#33-\u5b9e\u9645\u6548\u679c\">3.3. \u5b9e\u9645\u6548\u679c<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><!-- \/TOC --><\/p>\n<p><!--more--><\/p>\n<h1>1. \u6954<\/h1>\n<p>\u5728\u4e0a\u7bc7\u6587\u7ae0<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/jiangshibo.cn\/index.php\/2020\/05\/10\/%e6%a0%91%e8%8e%93%e6%b4%begpio%e5%92%8cpwm%e6%8e%a7%e5%88%b6%e6%95%99%e7%a8%8b\/\">\u6811\u8393\u6d3eGPIO\u548cPWM\u63a7\u5236\u6559\u7a0b<\/a>\u4e2d\uff0c\u7b14\u8005\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528\u6811\u8393\u8fdb\u884c\u666e\u901aIO\u63a7\u5236\u6a21\u62df\uff0c\u4ee5\u53caPWM \u6ce2\u5f62\u53d1\u9001\u7b49\u64cd\u4f5c\uff0c\u540c\u65f6\u8fd8\u63d0\u5230\u4e86\u6c7d\u8f66\u7535\u5b50\u5e38\u89c1\u7684CAN \u901a\u8baf\u4e5f\u80fd\u591f\u4f7f\u7528\u6811\u8393\u6d3e\u5b8c\u6210\uff0c\u672c\u6587\u9488\u5bf9\u6b64\u8fdb\u884c\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n<p>\u4f7f\u7528\u786c\u4ef6\u4e3a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/item.taobao.com\/item.htm?id=597477386998\">\u6811\u8393\u6d3e3b+<\/a>, <a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/item.taobao.com\/item.htm?id=545282653835\">MCP2515<\/a> spiCAN\u6a21\u5757 ,\u603b\u6210\u672c\u63a7\u5236\u5728300 \u5143\u4ee5\u5185\u3002\u4f7f\u7528\u6811\u8393\u6d3e4b\u4e5f\u53ef\u4ee5\uff0c\u4e0d\u8fc7\u8fd1\u671f\u76844b\u4ef7\u683c\u79bb\u8c31\uff0c\u800c\u4e14\u4ec5\u4ec5can\u548cio\u7684\u6d4b\u8bd5\uff0c3b+\u6027\u80fd\u5b8c\u5168\u591f\u7528\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eCAN\u901a\u8baf\u6559\u7a0b\/MCP2515.png\" alt=\"\" \/><\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u4f7f\u7528<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.raspberrypi.org\/software\/operating-systems\/#third-party-software\">ubuntu server<\/a> \uff0c\u5982\u4f7f\u7528Raspberry Pi OS \u4e5f\u53ef\u4ee5\uff0c\u672c\u6559\u7a0b\u4e5f\u53ef\u9002\u7528\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u80fd\u9b54\u6cd5\u4e0a\u7f51\u7684\u540c\u5b66\u9700\u8981\u9996\u5148\u8fdb\u884c\u64cd\u4f5c\u7cfb\u7edf\u6362\u6e90\uff0c\u4ee5\u53capython3 \u7684\u6362\u6e90\u3002\u800c\u5176\u4e2dubuntu server arm \u7684\u7cfb\u7edf\u4e0d\u80fd\u591f\u6309\u7167\u5e38\u7528\u7684\u6e05\u534e\u6e90\u6559\u7a0b\u66ff\u6362\uff0c\u9700\u8981\u4f7f\u7528\u540e\u7f00\u4e3a-ports\u7684\u6e90\uff0c\u7b14\u8005\u4f7f\u7528\u7684\u6e90\u5982\u4e0b:<\/p>\n<ul>\n<li><code>\/etc\/apt\/sources.list<\/code>\n<pre><code class=\"\"># \u9ed8\u8ba4\u6ce8\u91ca\u4e86\u6e90\u7801\u4ed3\u5e93\uff0c\u5982\u6709\u9700\u8981\u53ef\u81ea\u884c\u53d6\u6d88\u6ce8\u91ca\ndeb https:\/\/mirrors.ustc.edu.cn\/ubuntu-ports\/ focal main restricted universe multiverse\n# deb-src https:\/\/mirrors.ustc.edu.cn\/ubuntu-ports\/ focal main main restricted universe multiverse\ndeb https:\/\/mirrors.ustc.edu.cn\/ubuntu-ports\/ focal-updates main restricted universe multiverse\n# deb-src https:\/\/mirrors.ustc.edu.cn\/ubuntu-ports\/ focal-updates main restricted universe multiverse\ndeb https:\/\/mirrors.ustc.edu.cn\/ubuntu-ports\/ focal-backports main restricted universe multiverse\n# deb-src https:\/\/mirrors.ustc.edu.cn\/ubuntu-ports\/ focal-backports main restricted universe multiverse\ndeb https:\/\/mirrors.ustc.edu.cn\/ubuntu-ports\/ focal-security main restricted universe multiverse\n# deb-src https:\/\/mirrors.ustc.edu.cn\/ubuntu-ports\/ focal-security main restricted universe multiverse\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eCAN\u901a\u8baf\u6559\u7a0b\/001.png\" alt=\"\" \/><\/p>\n<h1>2. \u786c\u4ef6\u8fde\u63a5\u548c\u73af\u5883\u51c6\u5907<\/h1>\n<p>\u8fde\u63a5MCP2515\u548c\u6811\u8393\u6d3espi\u63a5\u53e3\uff0c\u5e76\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5f00\u542fspi\uff0c\u6574\u4e2a\u7684\u8fd0\u884c\u539f\u7406\u5c31\u662f\u8ba9MCP2515\u7684CAN \u901a\u8baf\u4f5c\u4e3a\u7f51\u7edc\u901a\u8baf\u63a5\u53e3\uff0c\u6302\u63a5\u5230socketCAN \u4e0a\uff0c\u4f7f\u7528\u7cfb\u7edf\u9a71\u52a8spi\uff0c\u65e0\u9700\u624b\u5de5\u7f16\u5199spi\u9a71\u52a8\u4ee5\u53cacan wrapper\u90e8\u5206\u3002\u9488\u5bf9socketCAN\uff0c\u7f51\u4e0a\u6709\u5f88\u591a\u4f18\u79c0\u7684\u5f00\u6e90\u5de5\u5177\u53ef\u4ee5\u4f7f\u7528\uff0c\u7b14\u8005\u8fd9\u91cc\u4f7f\u7528\u7684\u662fcantools \uff0c\u53ef\u4ee5\u4f7f\u7528dbc\u8fdb\u884c\u62a5\u6587\u683c\u5f0f\u89e3\u6790\u3002<\/p>\n<h2>2.1. \u786c\u4ef6\u8fde\u63a5<\/h2>\n<pre><code class=\"\">RPi Pin    RPi Label     CAN Module\n02---------5V------------VCC\n06---------GND-----------GND\n19---------GPIO10--------MOSI (SI)\n21---------GPIO9---------MISO (SO)\n22---------GPIO25--------INT\n23---------GPIO11--------SCK\n24---------GPIO8---------CS\n<\/code><\/pre>\n<h2>2.2. \u73af\u5883\u51c6\u5907<\/h2>\n<ul>\n<li>\u5b89\u88c5socket can\u5de5\u5177\u4ee5\u53cacantools\u5de5\u5177\n<pre><code class=\"\">sudo apt install can-utils\npip3 install cantools\n<\/code><\/pre>\n<\/li>\n<li>\u4f7f\u80fd\u6811\u8393\u6d3eSPI\u5e76\u52a0\u8f7dMCP2515\u5185\u6838\u9a71\u52a8\n<p>\u9488\u5bf9Ubuntu server \u64cd\u4f5c\u7cfb\u7edf\uff0c\u5728<code>\/boot\/firmware\/usercfg.txt<\/code>\u6587\u4ef6\u540e\u6dfb\u52a0\u5982\u4e0b\u5185\u5bb9\uff0c\u82e5\u64cd\u4f5c\u7cfb\u7edf\u4e3aRaspberry Pi OS\uff0c\u5219\u5728<code>\/boot\/config.txt<\/code>\u6587\u4ef6\u540e\u6dfb\u52a0\u5982\u4e0b\u5185\u5bb9:<\/p>\n<pre><code class=\"\">dtparam=spi=on\ndtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25\ndtoverlay=spi1-1cs\n<\/code><\/pre>\n<\/li>\n<li>\u91cd\u542f <code>sudo reboot -h now<\/code><\/li>\n<\/ul>\n<h2>2.3. \u68c0\u6d4bMCP2515\u662f\u5426\u88ab\u6b63\u786e\u6302\u8f7d<\/h2>\n<p>\u8f93\u5165<code>sudo ifconfig -a<\/code>\u6307\u4ee4\u53ef\u4ee5\u770b\u5230\u5df2\u7ecf\u6302\u8f7d\u4e86\u7f51\u7edc\u901a\u8baf\u5361CAN0\uff0c\u5982\u6ca1\u6709ifconfig\uff0c\u5219\u4f7f\u7528<code>sudo apt install net-tools<\/code>\u8fdb\u884c\u5b89\u88c5<\/p>\n<pre><code class=\"\">ubuntu@ubuntu:~$ sudo ifconfig -a\ncan0: flags=193&lt;UP,RUNNING,NOARP&gt;  mtu 16\n        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)\n        RX packets 9343435  bytes 74747480 (74.7 MB)\n        RX errors 0  dropped 0  overruns 0  frame 0\n        TX packets 8  bytes 64 (64.0 B)\n        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n\neth0: flags=4099&lt;UP,BROADCAST,MULTICAST&gt;  mtu 1500\n        ether b8:27:eb:c2:61:84  txqueuelen 1000  (Ethernet)\n        RX packets 0  bytes 0 (0.0 B)\n        RX errors 0  dropped 0  overruns 0  frame 0\n        TX packets 0  bytes 0 (0.0 B)\n        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n<\/code><\/pre>\n<p>\u8f93\u5165 <code>sudo ip -s -d link show can0<\/code> \u67e5\u770bcan0 \u901a\u8baf\u662f\u5426\u8fdb\u5165ready\u72b6\u6001\u3002<\/p>\n<pre><code class=\"\">ubuntu@ubuntu:~$ sudo ip -s -d link show can0\n3: can0: &lt;NOARP,ECHO&gt; mtu 16 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 10\n    link\/can  promiscuity 0 minmtu 0 maxmtu 0\n    can state STOPPED restart-ms 0\n          bitrate 1000000 sample-point 0.750\n          tq 125 prop-seg 2 phase-seg1 3 phase-seg2 2 sjw 1\n          mcp251x: tseg1 3..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1\n          clock 8000000\n          re-started bus-errors arbit-lost error-warn error-pass bus-off\n          0          0          0          0          0          0         numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535\n    RX: bytes  packets  errors  dropped overrun mcast\n    74955120   9369390  0       0       0       0\n    TX: bytes  packets  errors  dropped carrier collsns\n    64         8        0       0       0       0\n<\/code><\/pre>\n<h1>3. CAN \u901a\u8baf\u8bbe\u7f6e\u53ca\u7a0b\u5e8f\u5f00\u53d1<\/h1>\n<p>\u5982\u4e0a\u6b65\u9aa4\u5982\u679c\u5df2\u7ecf\u5b8c\u6210\uff0c\u5219\u53ef\u8fdb\u884c\u76f8\u5173CAN \u901a\u8baf\u5f00\u53d1\uff0c\u4f7f\u7528python\u548cc\u90fd\u53ef\u4ee5\uff0c\u56e0\u4e3a\u7cfb\u7edf\u652f\u6301\u547d\u4ee4\u884c\u8fdb\u884c\u62a5\u6587\u53d1\u9001\u8bfb\u53d6\u53ca\u8bbe\u5b9a\uff0c\u6240\u4ee5python\u53ef\u4ee5\u7b80\u5355\u7684\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u3002c\u7684\u8bdd\u4e0d\u5efa\u8bae\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\uff0c\u800c\u662f\u4f7f\u7528socket\u63a5\u53e3\u8fdb\u884c\u7f16\u7a0b\u3002<\/p>\n<p>\u9700\u6ce8\u610f\uff0c\u7531\u4e8e\u786c\u4ef6\u9650\u5236\uff0c\u6b64\u65b9\u6848can\u901a\u8baf\u6ce2\u7279\u7387\u6700\u9ad8\u4ec5\u652f\u6301500Kbps<\/p>\n<h2>3.1. \u7cfb\u7edf\u547d\u4ee4<\/h2>\n<ul>\n<li>\u5173\u95edcan0\n<p><code>sudo ip link set can0 down<\/code><\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6e\u6ce2\u7279\u7387 500K ,\u9700\u6ce8\u610fbitrate \u9700\u8981\u96642\u624d\u662f\u5e38\u89c4\u7684\u901a\u8baf\u6ce2\u7279\u7387<\/p>\n<p><code>sudo ip link set can0 type can bitrate 1000000<\/code><\/p>\n<\/li>\n<li>\n<p>\u5f00\u542fcan0<\/p>\n<p><code>sudo ip link set can0 up<\/code><\/p>\n<\/li>\n<li>\n<p>\u67e5\u770b\u72b6\u6001<\/p>\n<p><code>sudo ip -s -d link show can0<\/code><\/p>\n<\/li>\n<li>\n<p>\u63a5\u6536\u62a5\u6587\u547d\u4ee4<\/p>\n<p><code>candump any,0:0,#FFFFFFFF<\/code><\/p>\n<\/li>\n<li>\n<p>\u8054\u5408 cantools \u4f7f\u7528dbc\u6587\u4ef6\u8fdb\u884c\u62a5\u6587\u89e3\u7801<\/p>\n<p><code>candump can0 | cantools decode temp.dbc<\/code><\/p>\n<\/li>\n<li>\n<p>\u53d1\u9001\u62a5\u6587\u547d\u4ee4<\/p>\n<p><code>cansend can0 123#1122334455667788<\/code><\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6e\u56de\u73af \u6ce2\u7279\u7387 250K ,\u7528\u4e8e\u6d4b\u8bd5can\u901a\u8def,\u5728\u6ca1\u6709\u5176\u5b83\u786c\u4ef6\u8fde\u63a5\u6d4b\u8bd5\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8bbe\u5b9a\u6210\u56de\u73af\uff0c\u81ea\u53d1\u81ea\u6536<\/p>\n<p><code>sudo ip link set can0 type can bitrate 500000 loopback on<\/code><\/p>\n<\/li>\n<\/ul>\n<h2>3.2. c\u8bed\u8a00\u8c03\u7528socket\u63a5\u53e3\u8fdb\u884c\u5f00\u53d1<\/h2>\n<pre><code class=\"language-cpp \">\/**\n *-----------------------------------------------------------------------------\n * @file can_control.c\n * @brief \n * @author Tomato\n * @version 0.1\n * @date 2021-07-22\n * @note [change history] \n * \n * @copyright NAAAAA\n *-----------------------------------------------------------------------------\n *\/\n\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;net\/if.h&gt;\n#include &lt;sys\/ioctl.h&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;linux\/can.h&gt;\n#include &lt;linux\/can\/raw.h&gt;\n#define command \"ip link set can0 type can bitrate 1000000\"\/\/\u5c06CAN0\u6ce2\u7279\u7387\u8bbe\u7f6e\u4e3a500K\n#define up \"ifconfig can0 up\"\/\/\u6253\u5f00CAN0\n#define down \"ifconfig can0 down\"\/\/\u5173\u95edCAN0\n\nint can_init()\n{\n    \/\/\u5173\u95edCAN\u8bbe\u5907\uff0c\u8bbe\u7f6e\u6ce2\u7279\u7387\u540e\uff0c\u91cd\u65b0\u6253\u5f00CAN\u8bbe\u5907\n    system(down);\n    system(command);\n    system(up);\n    return 0;\n}\n\nint can_send(can_frame frame)\n{\n    int s, nbytes;\n    struct sockaddr_can addr;\n    struct ifreq ifr;\n    \/\/\u521b\u5efa\u5957\u63a5\u5b57\n    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);\n    strcpy(ifr.ifr_name, \"can0\" );\n    \/\/\u6307\u5b9a can0 \u8bbe\u5907\n    ioctl(s, SIOCGIFINDEX, &amp;ifr); \n    addr.can_family = AF_CAN;\n    addr.can_ifindex = ifr.ifr_ifindex;\n    \/\/\u5c06\u5957\u63a5\u5b57\u4e0e can0 \u7ed1\u5b9a\n    bind(s, (struct sockaddr *)&amp;addr, sizeof(addr));\n    \/\/\u53d1\u9001 frame[0]\n    nbytes = write(s, &amp;frame, sizeof(frame));\n    if(nbytes != sizeof(frame))\n    {\n        printf(\"Send Error frame[0]\\n!\");\n    }\n    close(s);\n    return 0;\n}\n\nint can_receive(struct can_frame * r_frame,unsigned int filter_id)\n{\n    int s, nbytes = 0;\n    struct sockaddr_can addr;\n    struct ifreq ifr;\n    struct can_frame frame;\n    struct can_filter rfilter;\n\n    \/\/ Initial fram\n    memset(&amp;frame,0,sizeof(can_frame));\n    \/\/\u521b\u5efa\u5957\u63a5\u5b57\n    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);\n    strcpy(ifr.ifr_name, \"can0\" );\n    \/\/\u6307\u5b9a can0 \u8bbe\u5907\n    ioctl(s, SIOCGIFINDEX, &amp;ifr); \n    addr.can_family = AF_CAN;\n    addr.can_ifindex = ifr.ifr_ifindex;\n    \/\/\u5c06\u5957\u63a5\u5b57\u4e0e can0 \u7ed1\u5b9a\n    bind(s, (struct sockaddr *)&amp;addr, sizeof(addr));\n    \/\/\u8bbe\u7f6e\u8fc7\u6ee4\u89c4\u5219\uff0c\u53d6\u6d88\u5f53\u524d\u6ce8\u91ca\u4e3a\u7981\u7528\u8fc7\u6ee4\u89c4\u5219\uff0c\u5373\u4e0d\u63a5\u6536\u6240\u6709\u62a5\u6587\uff0c\n    \/\/ \u4e0d\u8bbe\u7f6e\u6b64\u9879\uff08\u5373\u5982\u5f53\u524d\u4ee3\u7801\u88ab\u6ce8\u91ca\uff09\u4e3a\u63a5\u6536\u6240\u6709ID\u7684\u62a5\u6587\u3002\n    if (filter_id != 0)\n    {\n        rfilter.can_id   = 0x123;\n        \/\/ CAN_EFF_MASK | CAN_SFF_MASK\n        rfilter.can_mask = CAN_SFF_MASK;\n        setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &amp;rfilter, sizeof(rfilter));\n    }\n    while (nbytes == 0)\n    {\n        \/\/\u63a5\u6536\u603b\u7ebf\u4e0a\u7684\u62a5\u6587\u4fdd\u5b58\u5728frame\u4e2d\n        nbytes = read(s, &amp;frame, sizeof(frame));\n    }\n    *r_frame = frame;\n#ifdef MSG_DEBUG\n    printf(\"the nbytes:%d\\n\", nbytes);\n    printf(\"length:%d\", sizeof(frame));\n    printf(\"ID=0x%X DLC=%d\\n\", frame.can_id, frame.can_dlc);\n    printf(\"data0=0x%02x\\n\",frame.data[0]);\n    printf(\"data1=0x%02x\\n\",frame.data[1]);\n    printf(\"data2=0x%02x\\n\",frame.data[2]);\n    printf(\"data3=0x%02x\\n\",frame.data[3]);\n    printf(\"data4=0x%02x\\n\",frame.data[4]);\n    printf(\"data5=0x%02x\\n\",frame.data[5]);\n    printf(\"data6=0x%02x\\n\",frame.data[6]);\n    printf(\"data7=0x%02x\\n\",frame.data[7]);\n#endif\n    return 0;\n}\n\nint led_ctl_on(void)\n{\n    struct can_frame frame;\n    memset(&amp;frame, 0, sizeof(can_frame));\n    frame.can_id = 0x101;\n    frame.can_dlc = 8;\n    frame.data[0] = 1;\n    can_send(frame);\n    return 0;\n}\n\nint led_ctl_off(void)\n{\n    struct can_frame frame;\n    memset(&amp;frame, 0, sizeof(can_frame));\n    frame.can_id = 0x101;\n    frame.can_dlc = 8;\n    frame.data[0] = 2;\n    can_send(frame);\n    return 0;\n}\n\nfloat can_get_vol(void)\n{\n    float vol_vle = 0;\n    struct can_frame frame;\n    memset(&amp;frame, 0, sizeof(can_frame));\n    \/\/ wait until can frame 100 received\n    can_receive(&amp;frame,0);\n    printf(\"###############################\\n\");\n    printf(\"length:%d\", sizeof(frame));\n    printf(\"ID=0x%X DLC=%d\\n\", frame.can_id, frame.can_dlc);\n    printf(\"data0=0x%02x\\n\",frame.data[0]);\n    printf(\"data1=0x%02x\\n\",frame.data[1]);\n    printf(\"data2=0x%02x\\n\",frame.data[2]);\n    printf(\"data3=0x%02x\\n\",frame.data[3]);\n    printf(\"data4=0x%02x\\n\",frame.data[4]);\n    printf(\"data5=0x%02x\\n\",frame.data[5]);\n    printf(\"data6=0x%02x\\n\",frame.data[6]);\n    printf(\"data7=0x%02x\\n\",frame.data[7]);\n\n    vol_vle = (float)frame.data[0]\/50;\n    return vol_vle;\n}\n\nint main(int argc, char* argv[])\n{\n    char control_str[15]; \n    float vol_val = 0;\n\n    if (argc &lt; 2) {\n        printf(\"can_control service_type\\n\"\n            \"    example: .\/can_control led_off\/led_on\/get_vol\\n\"\n            );\n        return 0;\n    }\n    strcpy(control_str,argv[1]);\n\n    \/\/ debug\n    printf(\"Argc : %d\\n\",argc);\n    printf(\"Argv : %s\\n , %s\\n\",argv[0], argv[1]);\n\n    \/\/ can_init();\n    if (strcmp(control_str,\"led_off\")==0)\n    {\n        led_ctl_off();\n    }\n    else if (strcmp(control_str,\"led_on\")==0)\n    {\n        led_ctl_on();\n    }\n    else if (strcmp(control_str,\"get_vol\")==0)\n    {\n        vol_val = can_get_vol();\n        printf(\"Voltage is : %5.2f V\\n\", vol_val);\n    }\n    else\n    {\n        \/* Do nothing *\/\n    }\n    return 0;\n}\n<\/code><\/pre>\n<h2>3.3. \u5b9e\u9645\u6548\u679c<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/qq353838430.gitee.io\/blog_picture\/\u6811\u8393\u6d3eCAN\u901a\u8baf\u6559\u7a0b\/can\u7ed3\u679c.png\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6811\u8393\u6d3eCAN\u901a\u8baf\u6559\u7a0b &#8211; MCP2515 1. \u6954 2. \u786c\u4ef6\u8fde\u63a5\u548c\u73af\u5883\u51c6\u5907 2.1. \u786c\u4ef6\u8fde\u63a5 2.2. \u73af [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/100"}],"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=100"}],"version-history":[{"count":2,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiangshibo.cn\/index.php\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}