{"id":886,"date":"2019-03-11T23:07:51","date_gmt":"2019-03-11T14:07:51","guid":{"rendered":"https:\/\/hakus-blog.mydns.jp\/?p=886"},"modified":"2019-03-11T23:14:16","modified_gmt":"2019-03-11T14:14:16","slug":"can-%e9%80%81%e4%bf%a1%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0","status":"publish","type":"post","link":"https:\/\/hakus-blog.mydns.jp\/?p=886","title":{"rendered":"Raspberry pi CAN \u901a\u4fe1 \u30d7\u30ed\u30b0\u30e9\u30e0"},"content":{"rendered":"\n<p>RS485_CAN_HAT  \u3067CAN\u901a\u4fe1\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<br>python-can \u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u8a18\u4e8b\u81ea\u4f53\u306f\u5225\u30b5\u30a4\u30c8\u3067\u3082\u60c5\u5831\u304c\u5c11\u306a\u3044\u305f\u3081\u3001\u5f79\u306b\u7acb\u3066\u308c\u3070\u3068\u601d\u3044\u8a18\u4e8b\u306b\u6b8b\u3057\u307e\u3059\u3002<br>Python\u521d\u5fc3\u8005\u306a\u306e\u3067\u3001\u9593\u9055\u3063\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport threading\nimport can\nimport time\n\n# CAN\u30ea\u30bd\u30fc\u30b9\u8a2d\u5b9a\nos.system('sudo ip link set can0 type can bitrate 500000')\nos.system('sudo ifconfig can0 up')\n\nFIL_CANID   = 0xXXXXXXXX\nFIL_CANMSK  = 0xXXXXXXXX\nREQ_CANID\u3000 = 0xXXXXXXXX\nID_FORMAT \u3000= True #29bitCANID\u306e\u5834\u5408\n\n# CAN\u8a2d\u5b9a\u3001\u30d5\u30a3\u30eb\u30bf\u8a2d\u5b9a\ncan0 = can.Bus(channel = 'can0', can_filters = [{'can_id': FIL_CANID, 'can_mask': FIL_CANMSK }], bustype = 'socketcan_ctypes')# socketcan_native\ncanlog = can.ASCWriter('\/home\/pi\/public\/canlog.asc')\ncan.Notifier(can0, [canlog, ])\n\n# \u9001\u4fe1\u30c7\u30fc\u30bf\u306f\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u306b\u3057\u305f\u3044\ndef ServiceTest():\n\n    #\u30c6\u30b9\u30c8\u30d5\u30ec\u30fc\u30e0\u9001\u4fe1\n    msg0 = can.Message(arbitration_id=REQ_CANID, data=[1, 3, 0, 0, 0, 0, 0, 0], extended_id=ID_FORMAT)\n    canlog.log_event(msg0, time.time())\n    can0.send(msg0)\n\n\n# main\u95a2\u6570\nif __name__ == '__main__':\n    t1 = threading.Thread(target=ServiceTest)\n    \n    # \u9001\u4fe1\u30d7\u30ed\u30bb\u30b9\u30b9\u30bf\u30fc\u30c8\n    t1.start()\n\n    # \u9001\u4fe1\u30d7\u30ed\u30bb\u30b9\u304c\u5b8c\u4e86\u3059\u308b\u306e\u3092\u5f85\u3064\n    t1.join()\n\n    canlog.stop()\n    can0.shutdown()\n    os.system('sudo ifconfig can0 down')\n    print('end')<\/code><\/pre>\n\n\n\n<p>\u203b \u8a18\u4e8b\u3088\u3046\u306b\u3001\u4e00\u90e8\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3057\u3066\u3044\u307e\u3059\u3002<br>\u5b9f\u884c\u3059\u308b\u3068\u3001CANID\u3001MASK\u306b\u5408\u81f4\u3057\u305f\u53d7\u4fe1\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u307fcanlog.asc\u306b\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<br>\u307e\u305f\u3001canlog.log_event()\u306b\u3066\u9001\u4fe1\u30e1\u30c3\u30bb\u30fc\u30b8\u3082\u30ed\u30b0\u51fa\u529b\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RS485_CAN_HAT \u3067CAN\u901a\u4fe1\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002python-can \u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u8a18\u4e8b\u81ea\u4f53\u306f\u5225\u30b5\u30a4\u30c8\u3067\u3082\u60c5\u5831\u304c\u5c11\u306a\u3044\u305f\u3081\u3001\u5f79\u306b\u7acb\u3066\u308c\u3070\u3068\u601d\u3044\u8a18\u4e8b\u306b\u6b8b\u3057\u307e\u3059\u3002Python\u521d\u5fc3\u8005\u306a\u306e\u3067\u3001\u9593\u9055\u3063\u3066\u3044\u308b &hellip; <a href=\"https:\/\/hakus-blog.mydns.jp\/?p=886\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">Raspberry pi CAN \u901a\u4fe1 \u30d7\u30ed\u30b0\u30e9\u30e0<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,61],"tags":[219,222,221,220],"_links":{"self":[{"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/886"}],"collection":[{"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=886"}],"version-history":[{"count":5,"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions"}],"predecessor-version":[{"id":892,"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions\/892"}],"wp:attachment":[{"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hakus-blog.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}