{"id":361948,"date":"2021-02-22T20:27:54","date_gmt":"2021-02-22T12:27:54","guid":{"rendered":"http:\/\/4563.org\/?p=361948"},"modified":"2021-03-06T13:28:58","modified_gmt":"2021-03-06T05:28:58","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%8b%ef%bc%8c-c-%e8%af%ad%e8%a8%80%e4%b8%ad%e5%a6%82%e4%bd%95%e7%9b%b4%e6%8e%a5%e5%8f%91%e9%80%81-16-%e8%bf%9b%e5%88%b6%e5%88%b0%e4%b8%b2%e5%8f%a3","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=361948","title":{"rendered":"\u8bf7\u6559\u4e00\u4e0b\uff0c C \u8bed\u8a00\u4e2d\u5982\u4f55\u76f4\u63a5\u53d1\u9001 16 \u8fdb\u5236\u5230\u4e32\u53e3"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e00\u4e0b\uff0c C \u8bed\u8a00\u4e2d\u5982\u4f55\u76f4\u63a5\u53d1\u9001 16 \u8fdb\u5236\u5230\u4e32\u53e3               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : commoccoom <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u56e0\u4e3a\u6709\u51e0\u4e2a\u8bbe\u5907\u662f RS485 \u901a\u8baf\u7684\uff0c\u5947\u602a\u5728\u4e8e\u4f7f\u7528\u4e86\u51e0\u4e2a\u73b0\u6210\u7684 Modbus \u5e93\uff0c\u603b\u662f\u5728\u6700\u540e\u7684 CRC \u51fa\u9519\uff0c\u6240\u4ee5\u60f3\u76f4\u63a5\u53d1\u9001\u6570\u636e\u5230\u8bbe\u5907\u3002<\/p>\n<pre><code># \u4f46\u662f\u65e0\u8bba\u662f unsigned char *data = \"01 03 00 00 00 01 84 0A\"; # \u8fd8\u662f unsigned char *data = \"0x01 0x03 0x00 0x00 0x00 0x01 0x84 0x0A\"; <\/code><\/pre>\n<p>\u53d1\u9001\u5230\u4e32\u53e3\u90fd\u4e0d\u662f 16 \u8fdb\u5236\u7684\u6570\u636e<\/p>\n<p>\u5411\u5404\u4f4d\u5927\u4f6c\u6765\u8bf7\u6559\u4e00\u4e0b\uff0c\u521a\u63a5\u89e6 C \u6ca1\u591a\u4e45      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5418978\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Hconk <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u5b9a\u4e49\u7684\u90fd\u662f\u5b57\u7b26\u4e32\uff0c= =..<br \/>unsigned char *data = [0x01,0x03,0x00,0x00,0x00,0x01,0x84,0x0A];                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418979\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Hconk <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Hconk \u53d1\u51fa\u53bb\u5c31\u770b\u5230\u6211\u5199\u7684\u6709\u6bdb\u75c5\u4e86\u3002\u3002=_=<br \/>unsigned char data[] ={0x01,0x03,0x00,0x00,0x00,0x01,0x84,0x0A};                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418980\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : clef <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             {0x01, 0x03};<br \/>or<br \/>&#8220;x01x03&#8221;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418981\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : commoccoom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Hconk \u8fd9\u662f\u4ec0\u4e48\u6570\u636e\u7c7b\u578b\u554a\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418982\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Hconk <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @commoccoom #4 data \u662f\u5b58 unsigned char \u7684\u6570\u7ec4                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418983\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wzb0909 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u95ee\u4e00\u4e2a\u8bed\u8a00\u7684\u95ee\u9898\u4e4b\u524d\uff0c\u603b\u8be5\u628a\u8bed\u6cd5\u5148\u641e\u660e\u767d\u5427\uff0c\u6d6a\u8d39\u522b\u4eba\u611f\u60c5\u70b9\u8fdb\u6765\uff5e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418984\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : commoccoom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wzb0909 \u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u4e5f\u662f\u601d\u8003\u4e86\u5f88\u4e45\uff0c\u4f46\u662f\u53ef\u80fd\u6c34\u5e73\u592a\u5dee\uff0c\u6d6a\u8d39\u4e86\u5404\u4f4d\u65f6\u95f4\u4e86\uff0c\u62b1\u6b49                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418985\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wzb0909 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @commoccoom \u554a\u5bf9\u4e0d\u8d77\uff0c\u6211\u4e5f\u662f\u542c\u4f60\u8bb2\u4e32\u53e3\u4ec0\u4e48\u7684\uff0c\u6211\u6bd4\u8f83\u597d\u5947\u70b9\u8fdb\u6765\u3002<br \/>\u8981\u662f\u80fd\u6709\u66f4\u591a\u4e00\u70b9\u4fe1\u606f\uff0c\u6bd4\u5982\uff0c\u4f60\u7684\u5de5\u4f5c\u7684\u66f4\u7ec6\u8282\u7684\u60c5\u51b5\u554a\uff08\u5148\u5c1d\u8bd5\u4e86\u4ec0\u4e48\uff0c\u53c8\u5c1d\u8bd5\u4e86\u4ec0\u4e48\uff0c\u9047\u5230\u4e86\u54ea\u4e9b\u95ee\u9898\uff0c\u5177\u4f53\u5728\u4ec0\u4e48\u5730\u65b9\u53d1\u73b0\u95ee\u9898\uff09\uff0c\u80fd\u6ee1\u8db3\u5927\u5bb6\u7684\u597d\u5947\u5fc3\uff0c\u56de\u7b54\u95ee\u9898\u5c31\u4f1a\u66f4\u5f00\u5fc3\u4e9b\u3002<\/p>\n<p>\u554a\u5f53\u7136\u8fd9\u4e5f\u4e0d\u662f\u4f60\u7684\u8d23\u4efb\u3002\u4f60\u80fd\u89e3\u51b3\u95ee\u9898\u5c31\u597d\uff5e                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418986\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : pkookp8 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6362\u4e00\u79cd\u601d\u8def<br \/>\u53ef\u4ee5\u5728\u7b97 crc \u7684\u65f6\u5019\u628a\u8f93\u5165\u6570\u636e\u6253\u5370\u51fa\u6765<br \/>\u5c31\u4f1a\u53d1\u73b0\u4f60\u7684\u8f93\u5165(\u5b57\u7b26\u4e32\u8f6c\u6362\u7684 acsii)\u548c\u4f60\u7684\u9884\u671f(\u4f60\u7684 16 \u8fdb\u5236\u6570\u636e)\u4e0d\u5339\u914d<br \/>\u4f46\u662f\u4f60\u4f1a\u53d1\u73b0\u6709\u4e00\u4e2a\u5b57\u7b26(\u7a7a\u683c)\u6bcf\u9694\u51e0\u4e2a\u5b57\u7b26\u5c31\u51fa\u73b0\u4e00\u6b21\uff0c\u5c31\u80fd\u60f3\u5230\u8fd9\u4e2a\u95ee\u9898\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418987\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : commoccoom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wzb0909 \u5de5\u4f5c\u5c31\u662f\u5728 Linux \u4e0a\u901a\u8fc7\u4e32\u53e3\u628a\u4f7f\u7528 ModBus485 \u534f\u8bae\u7684\u4f20\u611f\u5668\u7684\u6570\u636e\u83b7\u53d6\u5230\u3002\u4e4b\u524d\u662f Python \u5199\u7684\uff0c\u4f46\u662f\u4f20\u611f\u5668\u6570\u91cf\u591a\u4e86\u4e4b\u540e\uff0c\u6027\u80fd\u4e0d\u597d\uff0c\u6240\u4ee5\u60f3\u7528 C \u6765\u5199\u4e86\u3002<\/p>\n<p>\u5927\u90e8\u5206\u4f20\u611f\u5668\u90fd\u662f\u53ef\u4ee5\u7528 libmodbus \u8fd9\u4e2a\u5e93\u6765\u89e3\u51b3\u7684\uff0c\u4f46\u6709\u51e0\u4e2a\u7c7b\u578b\u7684\u4f20\u611f\u5668\u5728\u6700\u540e\u7684 CRC \u6821\u9a8c\u91cc\u51fa\u95ee\u9898\uff08 Python \u7248\u4e5f\u4e00\u6837\uff09\uff0c\u6240\u4ee5\u60f3\u76f4\u63a5\u53d1\u9001\u6307\u4ee4\u5230\u4e32\u53e3\u4e86\u3002<\/p>\n<p>\u5728\u7f51\u4e0a\u627e\u4e86 Libserialport \u8fd9\u4e2a\u5e93\uff0c\u4f46\u662f\u770b\u5b83\u7684\u793a\u4f8b\u7a0b\u5e8f\u91cc\u9762 https:\/\/sigrok.org\/api\/libserialport\/unstable\/a00046.html \u90fd\u662f\u53d1\u9001\u5b57\u7b26\u4e32\u6570\u636e\u7684\u3002\u4e5f\u5305\u62ec \u8fd9\u4e2a https:\/\/stackoverflow.com\/questions\/6947413\/how-to-open-read-and-write-from-serial-port-in-c<\/p>\n<p>\u50cf\u8fd9\u79cd <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/03\/8giJ88h5.jpeg\" alt=\"\u8bf7\u6559\u4e00\u4e0b\uff0c C \u8bed\u8a00\u4e2d\u5982\u4f55\u76f4\u63a5\u53d1\u9001 16 \u8fdb\u5236\u5230\u4e32\u53e3\"> \u4e32\u53e3\u52a9\u624b\u91cc\u5341\u516d\u8fdb\u5236\u53d1\u9001\u5230\u5e95\u662f\u600e\u4e48\u5b9e\u73b0\u7684<\/p>\n<p>\u56e0\u4e3a\u521a\u63a5\u89e6 C\uff0c\u6240\u4ee5\u662f\u7f1d\u5408\u602a\uff0c\u4e1c\u62fc\u897f\u51d1\u7684\uff0c\u8fd8\u671b\u6307\u6559\u554a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418988\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : junyee <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e32\u53e3\u53ef\u4ee5\u6309\u5b57\u8282\u53d1\u9001\u554a\uff0c\u4f46\u662f\u6709\u5f88\u591a\u4e0d\u53ef\u6253\u5370\u5b57\u7b26\uff0c\u8981\u8f6c\u6210 hex,\u5982\uff0cprintf \u7684 %02x                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418989\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : commoccoom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Hconk <br \/>@wzb0909 <\/p>\n<p>\u7ecf\u8fc7\u4e00\u756a\u641c\u7d22\u4e4b\u540e\uff0c\u53ea\u8981 unsigned long value = strtoul(&#8220;010300000001840A&#8221;,NULL,16); \u8fd9\u6837\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f46\u662f\u6211\u63a5\u6536\u5230\u7684\u6570\u636e\u5374\u662f\u201c0A84010000000301\u201d\u8fd9\u6837\u7684\u5012\u53d9\uff0c\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\uff0c\u53e6\u5916\uff0c\u6211\u4ece\u4e32\u53e3\u52a9\u624b\u53d1\u9001\u5230 linux \u7684 \u6570\u636e \u7528 int ret = snprintf(buffer, 50, &#8220;%lu&#8221;, *buf); \u8f6c\u6362\u4e4b\u540e \u662f\u8fd9\u6837\u7684 &#8220;220356377377177\u000000\u000000\u000000344306345267177\u000000\u000000\u000000\u000000d&#8221; \u600e\u4e48\u624d\u80fd\u53d8\u6210&#8221;010300000001840A&#8221; \u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u5462                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418990\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wzb0909 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @commoccoom \u6211\u9519\u4e86\uff0c\u6211\u53ea\u662f\u8def\u8fc7\u7684\uff0c\uff0c\uff0c\uff0c\u6211\u4e5f\u4e0d\u719f\u6089\u8fd9\u4e2a\u65b9\u9762\u3002<br \/>\u7136\u540e\uff0c<br \/>\u6570\u662f\u4ece\u4f4e\u5f80\u9ad8\u6570\uff0c\u5b57\u8282\u7801\u4ece\u524d\u5f80\u540e\u6570\uff0c\uff0c\u987a\u5e8f\u6ca1\u95ee\u9898\u554a\u3002\u3002\u3002\u7b26\u5408\u9884\u671f\u3002<br \/>lu \u662f\u5341\u8fdb\u5236\u5427\uff0c\u5341\u516d\u8fdb\u5236\u662f lX \u554a\u3002\u3002\u3002\u7136\u540e\u6211\u4e5f\u4e0d\u4e86\u89e3\u4f60\u8fd9\u4e2a\u8f93\u51fa\u3002<\/p>\n<p>\u7136\u540e\uff0c<br \/>\u6ca1\u6709\u7136\u540e\u4e86\u3002\u8fd9\u5df2\u7ecf\u8fc7\u4e8e\u7ec6\u8282\u4e86\u3002\u3002\u3002\u3002\u6211\u5148\u8d70\u4e86\uff5e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5418991\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : commoccoom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wzb0909 \u597d\u5427\uff0c\u73b0\u5728\u5df2\u7ecf\u89e3\u51b3\u4e86\uff0c\u8fd8\u5269\u4e2a\u5c0f\u95ee\u9898\u518d\u7814\u7a76\u4e0b\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e0b\uff0c C \u8bed\u8a00\u4e2d\u5982\u4f55\u76f4\u63a5\u53d1\u9001&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/361948"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=361948"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/361948\/revisions"}],"predecessor-version":[{"id":361953,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/361948\/revisions\/361953"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=361948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=361948"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=361948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}