{"id":6268,"date":"2023-06-13T09:18:15","date_gmt":"2023-06-13T01:18:15","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=6268"},"modified":"2023-06-13T09:18:15","modified_gmt":"2023-06-13T01:18:15","slug":"nanopi-%e6%8e%a7%e5%88%b6%e7%a1%ac%e9%ab%94","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=6268","title":{"rendered":"NanoPi - \u63a7\u5236\u786c\u9ad4"},"content":{"rendered":"<p>--<\/p>\n<h2>LED<\/h2>\n<ul>\n<li><a class=\"question-hyperlink\" href=\"https:\/\/raspberrypi.stackexchange.com\/questions\/697\/how-do-i-control-the-system-leds-using-my-software\" target=\"_blank\" rel=\"noopener\">How do I control the system LEDs using my software?<\/a><\/li>\n<\/ul>\n<p>\u958b\u95dc\u677f\u5b50\u4e0a\u7684\u7da0\u71c8\uff0c0 \u95dc 1 \u958b<\/p>\n<pre class=\"lang:default decode:true\"># echo 0 &gt; \/sys\/class\/leds\/status_led\/brightness<\/pre>\n<p>\/sys\/class\/leds\/status_led\/nanopi:green:pwr \u5c31\u662f\u65c1\u908a\u7684\u7d05\u71c8\uff0c\u96d6\u7136\u76ee\u9304\u540d\u7a31\u662f green ...<\/p>\n<p>--<\/p>\n<h2>UART<\/h2>\n<ul>\n<li><a href=\"https:\/\/superhbin.pixnet.net\/blog\/post\/34926973-%e3%80%90adruino%e5%85%a5%e9%96%80%e3%80%91-08.%e9%97%9c%e6%96%bcgpio%e3%80%81uart%e3%80%81i2c%e3%80%81spi\" target=\"_blank\" rel=\"noopener\">\u3010Adruino\u5165\u9580\u3011 08.\u95dc\u65bcGPIO\u3001UART\u3001I2C\u3001SPI<\/a><\/li>\n<li><a href=\"http:\/\/icodding.blogspot.com\/2016\/05\/python-pyserial.html\" target=\"_blank\" rel=\"noopener\">Python \u7814\u7a76 - pyserial \u7528\u6cd5<\/a><\/li>\n<li>https:\/\/pyserial.readthedocs.io\/en\/latest\/pyserial_api.html<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\"># pip install pyserial<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">import serial\r\nimport gpio\r\n\r\ngpio.setup(6, 'out')  # TR Pin\r\npySerial = serial.Serial(\"\/dev\/ttyS1\", baudrate=9600, timeout=None, bytesize=8, parity='N', stopbits=1, xonxoff=0, rtscts=0, dsrdtr=0)\r\n\r\n# def init_gpio():\r\n    # gpio.log.setLevel(logging.WARNING)\r\n\r\n\r\ndef get_modbus_crc(int_array):\r\n    value = 0xFFFF\r\n    for i in range(len(int_array)):\r\n        value ^= int_array[i]\r\n        for j in range(8):\r\n            if (value &amp; 0x01) == 1:\r\n                value = (value &gt;&gt; 1) ^ 0xA001\r\n            else:\r\n                value &gt;&gt;= 1\r\n    return [value &amp; 0xff, (value &gt;&gt; 8) &amp; 0xff]\r\n\r\n\r\n# , port=\"\/dev\/ttyS1\", baudrate=9600, timeout=10, write_timeout=10\r\ndef write_modbus(command_list):\r\n    full_command_list = command_list + get_modbus_crc(command_list)\r\n    read_count = full_command_list[5] * 2 + 5 if full_command_list[1] == 3 else len(full_command_list)\r\n    gpio.set(6, 1)\r\n    pySerial.write(full_command_list)\r\n    pySerial.flush()\r\n    gpio.set(6, 0)\r\n    read_data = pySerial.read(read_count)\r\n    data_list = list(read_data)\r\n    # pySerial.cancel_read()\r\n    # pySerial.cancel_write()\r\n    # pySerial.close()\r\n    return data_list\r\n\r\n# port = serial.Serial(\"\/dev\/ttyS1\", baudrate=9600, timeout=1, bytesize=8, parity='N', stopbits=1, xonxoff=0,rtscts=0,dsrdtr=0)\r\n\r\nwhile True:\r\n    # cmd = '3,5,0,1,0,0,157,232'.encode()\r\n    # port.write('3,5,0,1,255,0,220,24'.encode()) # \u958b\r\n\r\n    # cmd = [3,5,0,1,0,0,157,232]\r\n    # command_list = [3,5,0,1,0,0]\r\n    command_list = [1,3,0,0,0,2]\r\n    print(format(command_list))\r\n    data_list = write_modbus(command_list)\r\n    print(format(data_list))\r\n\r\n    # command_list = [3,5,0,1,255,0]\r\n    # print(format(command_list))\r\n    # data_list = write_modbus(command_list)\r\n    # print(format(data_list))\r\n<\/pre>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"6268\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;3,868&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- LED How do I...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"6268\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;3,868&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[272,308],"tags":[346,345],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6268"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6268"}],"version-history":[{"count":9,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6268\/revisions"}],"predecessor-version":[{"id":12305,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/6268\/revisions\/12305"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}