{"id":6055,"date":"2016-09-24T15:17:40","date_gmt":"2016-09-24T07:17:40","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6055"},"modified":"2016-09-24T15:17:40","modified_gmt":"2016-09-24T07:17:40","slug":"%e5%88%a9%e7%94%a8pyserial%e5%ae%9e%e7%8e%b0%e8%af%bb%e5%8d%a1%e5%99%a8%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6055.html","title":{"rendered":"\u5229\u7528pyserial\u5b9e\u73b0\u8bfb\u5361\u5668\u5f00\u53d1"},"content":{"rendered":"<p>\u4e3b\u8981\u901a\u8fc7\u84dd\u7259\u534f\u8bae\uff0c\u901a\u8fc7 pyserial \u8fd9\u4e2apython\u6a21\u5757\u6765\u5b9e\u73b0\u84dd\u7259\u6570\u636e\u7684\u6536\u53d1\u3002<\/p>\n<p>\u8bfb\u5361\u5668\u8bbe\u5907\u4e3a\u534e\u89c6\u7535\u5b50\u7684CVR-100B\u8fd9\u6b3e\u8eab\u4efd\u8bc1\u8bfb\u5361\u5668\u3002\u5b98\u7f51\uff1ahttp:\/\/www.chinaidcard.com\/archives\/72\/<\/p>\n<p>\u9996\u5148\u628a\u84dd\u7259\u8fde\u63a5\u4e0a\uff0c\u7136\u540e\u627e\u5230\u84dd\u7259\u5bf9\u5e94\u7684\u8bbe\u5907\u540d\u79f0\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nls \/dev\/tty*\r\n\r\n\/dev\/tty.CVR-100B-SPPDev\r\n\/dev\/tty                         \r\n\/dev\/tty.Bluetooth-Incoming-Port\r\n\/dev\/tty.CVR-100B-SPPDev \r\n<\/pre>\n<p>\u627e\u5230\u8bbe\u5907\u53f7\u4e3a `\/dev\/tty.CVR-100B-SPPDev `\uff0c \u8fd9\u4e2a\u4e00\u4f1a\u4ee3\u7801\u91cc\u9762\u8fde\u63a5\u4f1a\u7528\u5230\u3002<\/p>\n<p>\u7136\u540e\u76f4\u63a5\u8fde\u63a5\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n   def __init__(self, port_name=None):\r\n        self.ser = serial.Serial()\r\n        self.ser.baudrate = self.baudrate\r\n        if port_name:\r\n            self.port_name = port_name\r\n        self.ser.port = self.port_name\r\n        self.ser.timeout = 1\r\n<\/pre>\n<p>\u5411\u84dd\u7259\u63a5\u53e3\u5199\u547d\u4ee4\u6570\u636e\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ncmd_request = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x20, 0x01, 0x22]\r\nself.ser.open()\r\nself.ser.write(serial.to_bytes(self.cmd_request))\r\n<\/pre>\n<p>\u8bfb\u53d6\u6570\u636e\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n    def serial_read(self):\r\n        bytes_data = self.ser.read(1)\r\n        while True:\r\n            bytes_data += self.ser.read(self.ser.in_waiting)\r\n            time.sleep(0.1)\r\n            if self.ser.in_waiting&lt;=0:\r\n                break\r\n        return bytes_data\r\n<\/pre>\n<p>\u6839\u636e\u5b98\u65b9\u7684\u4f8b\u5b50\uff0c\u9700\u8981\u5148\u53d1\u9001\u4e24\u6b62\u6307\u4ee4\uff0c\u624d\u80fd\u8bfb\u53d6\u5361\u7684\u6570\u636e\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n    cmd_request = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x20, 0x01, 0x22]\r\n    cmd_select = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x20, 0x02, 0x21]\r\n    cmd_read = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x30, 0x01, 0x32]\r\n    cmd_sam = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x12, 0xFF, 0xEE]\r\n\r\n    def read(self):\r\n        info = None\r\n        try:\r\n            self.ser.open()\r\n            self.ser.write(serial.to_bytes(self.cmd_request))\r\n            buffer1 = self.serial_read()\r\n\r\n            self.ser.write(serial.to_bytes(self.cmd_select))\r\n            buffer2 = self.serial_read()\r\n\r\n            self.ser.write(serial.to_bytes(self.cmd_read))\r\n            buffer0 = self.serial_read()\r\n\r\n            info = self.decode_info(buffer0)\r\n        except Exception as e:\r\n            gen_log.error(e)\r\n        finally:\r\n            self.ser.close()\r\n\r\n        return info\r\n<\/pre>\n<p>decode_info \u65b9\u6cd5\u5c31\u662f\u628a\u8bfb\u53d6\u5230\u7684\u6570\u636e\uff0c\u6309\u7167\u534f\u8bae\u89e3\u6790\u6210\u6587\u672c\u3002<\/p>\n<p>\u8fd9\u4e00\u6b65\u6709\u4e00\u4e2a\u7f16\u7801\u7684\u5751\uff0c\u4e00\u76f4\u6309\u7167utf-8\u7684\u601d\u8def\u53bb encode, decode\uff0c\u90fd\u4e0d\u884c\u3002<\/p>\n<p>\u6700\u540e\u624d\u53d1\u73b0\u7f16\u7801\u4e3a utf-16\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n    info_pos_map = {\r\n        'name': &#x5B;14, 30], # \u5f00\u59cb\u4f4d\u7f6e\uff0c\u957f\u5ea6\r\n        'sex': &#x5B;44, 2],\r\n        'nation': &#x5B;46, 4],\r\n        'birthday': &#x5B;50, 16],\r\n        'address': &#x5B;66, 70],\r\n        'idcode': &#x5B;136, 36],\r\n        'department': &#x5B;176, 30],\r\n        'sdate': &#x5B;202, 16],\r\n        'tdate': &#x5B;218, 16],\r\n        # 'photo': &#x5B;270, 1024],\r\n    }\r\n\r\n    def decode_info(self, data):\r\n        info = {}\r\n        for name, pos in self.info_pos_map.items():\r\n            if name!='photo':\r\n                info&#x5B;name] = data&#x5B;pos&#x5B;0]:sum(pos)].decode('utf-16').strip()\r\n            else:\r\n                info&#x5B;name] = data&#x5B;pos&#x5B;0]:sum(pos)]\r\n        info&#x5B;'nation'] = self._decode_nation(info&#x5B;'nation'])\r\n        info&#x5B;'sex'] = self._decode_sex(info&#x5B;'sex'])\r\n        return info\r\n<\/pre>\n<p>\u5b8c\u6574\u4ee3\u7801<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#coding=utf-8\r\n\r\nimport serial\r\nimport time\r\n\r\nfrom axmservice.lib.log import gen_log\r\n\r\nnation_code_map = {\r\n    &quot;01&quot;: u&quot;\u6c49&quot;,\r\n    &quot;02&quot;: u&quot;\u8499\u53e4&quot;,\r\n    &quot;03&quot;: u&quot;\u56de&quot;,\r\n    &quot;04&quot;: u&quot;\u85cf&quot;,\r\n    &quot;05&quot;: u&quot;\u7ef4\u543e\u5c14&quot;,\r\n    &quot;06&quot;: u&quot;\u82d7&quot;,\r\n    &quot;07&quot;: u&quot;\u5f5d&quot;,\r\n    &quot;08&quot;: u&quot;\u58ee&quot;,\r\n    &quot;09&quot;: u&quot;\u5e03\u4f9d&quot;,\r\n    &quot;10&quot;: u&quot;\u671d\u9c9c&quot;,\r\n    &quot;11&quot;: u&quot;\u6ee1&quot;,\r\n    &quot;12&quot;: u&quot;\u4f97&quot;,\r\n    &quot;13&quot;: u&quot;\u7476&quot;,\r\n    &quot;14&quot;: u&quot;\u767d&quot;,\r\n    &quot;15&quot;: u&quot;\u571f\u5bb6&quot;,\r\n    &quot;16&quot;: u&quot;\u54c8\u5c3c&quot;,\r\n    &quot;17&quot;: u&quot;\u54c8\u8428\u514b&quot;,\r\n    &quot;18&quot;: u&quot;\u50a3&quot;,\r\n    &quot;19&quot;: u&quot;\u9ece&quot;,\r\n    &quot;20&quot;: u&quot;\u5088\u50f3&quot;,\r\n    &quot;21&quot;: u&quot;\u4f64&quot;,\r\n    &quot;22&quot;: u&quot;\u7572&quot;,\r\n    &quot;23&quot;: u&quot;\u9ad8\u5c71&quot;,\r\n    &quot;24&quot;: u&quot;\u62c9\u795c&quot;,\r\n    &quot;25&quot;: u&quot;\u6c34&quot;,\r\n    &quot;26&quot;: u&quot;\u4e1c\u4e61&quot;,\r\n    &quot;27&quot;: u&quot;\u7eb3\u897f&quot;,\r\n    &quot;28&quot;: u&quot;\u666f\u9887&quot;,\r\n    &quot;29&quot;: u&quot;\u67ef\u5c14\u514b\u5b5c&quot;,\r\n    &quot;30&quot;: u&quot;\u571f&quot;,\r\n    &quot;31&quot;: u&quot;\u8fbe\u65a1\u5c14&quot;,\r\n    &quot;32&quot;: u&quot;\u4eeb\u4f6c&quot;,\r\n    &quot;33&quot;: u&quot;\u7f8c&quot;,\r\n    &quot;34&quot;: u&quot;\u5e03\u6717&quot;,\r\n    &quot;35&quot;: u&quot;\u6492\u62c9&quot;,\r\n    &quot;36&quot;: u&quot;\u6bdb\u5357&quot;,\r\n    &quot;37&quot;: u&quot;\u4ee1\u4f6c&quot;,\r\n    &quot;38&quot;: u&quot;\u9521\u4f2f&quot;,\r\n    &quot;39&quot;: u&quot;\u963f\u660c&quot;,\r\n    &quot;40&quot;: u&quot;\u666e\u7c73&quot;,\r\n    &quot;41&quot;: u&quot;\u5854\u5409\u514b&quot;,\r\n    &quot;42&quot;: u&quot;\u6012&quot;,\r\n    &quot;43&quot;: u&quot;\u4e4c\u5b5c\u522b\u514b&quot;,\r\n    &quot;44&quot;: u&quot;\u4fc4\u7f57\u65af&quot;,\r\n    &quot;45&quot;: u&quot;\u9102\u6e29\u514b&quot;,\r\n    &quot;46&quot;: u&quot;\u5fb7\u6602&quot;,\r\n    &quot;47&quot;: u&quot;\u4fdd\u5b89&quot;,\r\n    &quot;48&quot;: u&quot;\u88d5\u56fa&quot;,\r\n    &quot;49&quot;: u&quot;\u4eac&quot;,\r\n    &quot;50&quot;: u&quot;\u5854\u5854\u5c14&quot;,\r\n    &quot;51&quot;: u&quot;\u72ec\u9f99&quot;,\r\n    &quot;52&quot;: u&quot;\u9102\u4f26\u6625&quot;,\r\n    &quot;53&quot;: u&quot;\u8d6b\u54f2&quot;,\r\n    &quot;54&quot;: u&quot;\u95e8\u5df4&quot;,\r\n    &quot;55&quot;: u&quot;\u73de\u5df4&quot;,\r\n    &quot;56&quot;: u&quot;\u57fa\u8bfa&quot;,\r\n}\r\n\r\nclass IDCardReaderCVR100U(object):\r\n    '''\u534e\u89c6\u8eab\u4efd\u8bc1\u8bfb\u5361\u5668, \u578b\u53f7 CVR-100B\r\n    \u5148\u901a\u8fc7\u84dd\u7259\u8fde\u63a5\u4e0a\u8bfb\u5361\u5668, \u7136\u540e  ls \/dev\/tty* \u627e\u5230\u5bf9\u5e94\u7684\u4e32\u53e3\u540d\u79f0, \u5b9e\u4f8b\u5316\u7684\u65f6\u5019\u4f20\u9012\u8fdb\u6765\r\n    '''\r\n\r\n    baudrate = 9600\r\n    port_name = '\/dev\/tty.CVR-100B-SPPDev'\r\n    cmd_request = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x20, 0x01, 0x22]\r\n    cmd_select = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x20, 0x02, 0x21]\r\n    cmd_read = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x30, 0x01, 0x32]\r\n    cmd_sam = &#x5B;0xAA, 0xAA, 0xAA, 0x96, 0x69, 0x00, 0x03, 0x12, 0xFF, 0xEE]\r\n\r\n    info_pos_map = {\r\n        'name': &#x5B;14, 30], # \u5f00\u59cb\u4f4d\u7f6e\uff0c\u957f\u5ea6\r\n        'sex': &#x5B;44, 2],\r\n        'nation': &#x5B;46, 4],\r\n        'birthday': &#x5B;50, 16],\r\n        'address': &#x5B;66, 70],\r\n        'idcode': &#x5B;136, 36],\r\n        'department': &#x5B;176, 30],\r\n        'sdate': &#x5B;202, 16],\r\n        'tdate': &#x5B;218, 16],\r\n        # 'photo': &#x5B;270, 1024],\r\n    }\r\n\r\n    def __init__(self, port_name=None):\r\n        self.ser = serial.Serial()\r\n        self.ser.baudrate = self.baudrate\r\n        if port_name:\r\n            self.port_name = port_name\r\n        self.ser.port = self.port_name\r\n        self.ser.timeout = 1\r\n\r\n    def decode_info(self, data):\r\n        info = {}\r\n        for name, pos in self.info_pos_map.items():\r\n            if name!='photo':\r\n                info&#x5B;name] = data&#x5B;pos&#x5B;0]:sum(pos)].decode('utf-16').strip()\r\n            else:\r\n                info&#x5B;name] = data&#x5B;pos&#x5B;0]:sum(pos)]\r\n        info&#x5B;'nation'] = self._decode_nation(info&#x5B;'nation'])\r\n        info&#x5B;'sex'] = self._decode_sex(info&#x5B;'sex'])\r\n        return info\r\n\r\n    def _decode_sex(self, data):\r\n        if data == '1':\r\n            return u'\u7537'\r\n        elif data == '2':\r\n            return u'\u5973'\r\n        elif data == '9':\r\n            return u'\u5176\u5b83'\r\n        else: return ''\r\n\r\n    def _decode_nation(self, data):\r\n        return nation_code_map.get(data, '')\r\n\r\n    def serial_read(self):\r\n        bytes_data = self.ser.read(1)\r\n        while True:\r\n            bytes_data += self.ser.read(self.ser.in_waiting)\r\n            time.sleep(0.1)\r\n            if self.ser.in_waiting&lt;=0:\r\n                break\r\n        return bytes_data\r\n\r\n    def read(self):\r\n        info = None\r\n        try:\r\n            self.ser.open()\r\n            self.ser.write(serial.to_bytes(self.cmd_request))\r\n            buffer1 = self.serial_read()\r\n\r\n            self.ser.write(serial.to_bytes(self.cmd_select))\r\n            buffer2 = self.serial_read()\r\n\r\n            self.ser.write(serial.to_bytes(self.cmd_read))\r\n            buffer0 = self.serial_read()\r\n\r\n            info = self.decode_info(buffer0)\r\n        except Exception as e:\r\n            gen_log.error(e)\r\n        finally:\r\n            self.ser.close()\r\n\r\n        return info\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u8981\u901a\u8fc7\u84dd\u7259\u534f\u8bae\uff0c\u901a\u8fc7 pyserial \u8fd9\u4e2apython\u6a21\u5757\u6765\u5b9e\u73b0\u84dd\u7259\u6570\u636e\u7684\u6536\u53d1\u3002 \u8bfb\u5361\u5668\u8bbe\u5907\u4e3a\u534e\u89c6\u7535\u5b50\u7684C [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6055","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6055"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6055\/revisions"}],"predecessor-version":[{"id":6056,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6055\/revisions\/6056"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}