{"id":1037352,"date":"2024-12-31T12:14:21","date_gmt":"2024-12-31T04:14:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1037352.html"},"modified":"2024-12-31T12:14:23","modified_gmt":"2024-12-31T04:14:23","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9python%e4%b8%8e%e5%8d%95%e7%89%87%e6%9c%ba%e5%af%b9%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1037352.html","title":{"rendered":"\u5982\u4f55\u8ba9python\u4e0e\u5355\u7247\u673a\u5bf9\u63a5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/bbddec0d-e294-4443-863b-7fabbbc5d2bd.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u8ba9python\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u4e32\u53e3\u901a\u4fe1\u3001\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u63a5\u53e3\u3001\u5b89\u88c5\u76f8\u5173\u9a71\u52a8\u3001\u7f16\u5199\u901a\u4fe1\u534f\u8bae\u3001\u5904\u7406\u6570\u636e\u683c\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u4e32\u53e3\u901a\u4fe1\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u5b83\u901a\u8fc7\u4e32\u53e3\u63a5\u53e3\u5c06Python\u7a0b\u5e8f\u4e0e\u5355\u7247\u673a\u8fdb\u884c\u6570\u636e\u4ea4\u6362\uff0c\u5177\u6709\u7b80\u5355\u6613\u7528\u3001\u517c\u5bb9\u6027\u5f3a\u7684\u4f18\u70b9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u4e32\u53e3\u901a\u4fe1\u5b9e\u73b0Python\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4e32\u53e3\u901a\u4fe1<\/strong><\/h2>\n<p><p>\u4e32\u53e3\u901a\u4fe1\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u786c\u4ef6\u901a\u4fe1\u65b9\u5f0f\uff0c\u901a\u8fc7\u4e32\u884c\u63a5\u53e3\u4f20\u8f93\u6570\u636e\u3002Python\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\u65f6\uff0c\u901a\u5e38\u4f7f\u7528USB\u8f6c\u4e32\u53e3\u6a21\u5757\u8fde\u63a5\u5355\u7247\u673a\u7684\u4e32\u53e3\u63a5\u53e3\u3002\u4f7f\u7528Python\u5185\u7f6e\u7684<code>pyserial<\/code>\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5b89\u88c5pyserial\u5e93<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>pyserial<\/code>\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pyserial<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u914d\u7f6e\u4e32\u53e3\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u4e32\u53e3\u901a\u4fe1\u65f6\uff0c\u9700\u8981\u914d\u7f6e\u4e32\u53e3\u7684\u53c2\u6570\uff0c\u5982\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u548c\u6821\u9a8c\u4f4d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import serial<\/p>\n<p>ser = serial.Serial(<\/p>\n<p>    port=&#39;COM3&#39;,        # \u4e32\u53e3\u7aef\u53e3\u53f7<\/p>\n<p>    baudrate=9600,      # \u6ce2\u7279\u7387<\/p>\n<p>    bytesize=serial.EIGHTBITS,  # \u6570\u636e\u4f4d<\/p>\n<p>    parity=serial.PARITY_NONE,  # \u6821\u9a8c\u4f4d<\/p>\n<p>    stopbits=serial.STOPBITS_ONE  # \u505c\u6b62\u4f4d<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u914d\u7f6e\u597d\u4e32\u53e3\u53c2\u6570\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>write<\/code>\u65b9\u6cd5\u53d1\u9001\u6570\u636e\uff0c\u901a\u8fc7<code>read<\/code>\u65b9\u6cd5\u63a5\u6536\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53d1\u9001\u6570\u636e<\/p>\n<p>ser.write(b&#39;Hello, MCU!&#39;)<\/p>\n<h2><strong>\u63a5\u6536\u6570\u636e<\/strong><\/h2>\n<p>data = ser.read(10)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u63a5\u53e3<\/strong><\/h2>\n<p><p>\u5728\u8fdb\u884cPython\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u63a5\u53e3\u975e\u5e38\u91cd\u8981\u3002\u5e38\u89c1\u7684\u786c\u4ef6\u63a5\u53e3\u5305\u62ec\u4e32\u53e3\uff08UART\uff09\u3001SPI\u3001I2C\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4e32\u53e3\uff08UART\uff09<\/h2>\n<\/p>\n<p><p>\u4e32\u53e3\u662f\u6700\u5e38\u7528\u7684\u786c\u4ef6\u63a5\u53e3\u4e4b\u4e00\uff0c\u5177\u6709\u7b80\u5355\u6613\u7528\u3001\u517c\u5bb9\u6027\u5f3a\u7684\u4f18\u70b9\u3002\u901a\u8fc7\u4e32\u53e3\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0Python\u4e0e\u5355\u7247\u673a\u7684\u6570\u636e\u4ea4\u6362\u3002\u4e32\u53e3\u901a\u4fe1\u7684\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u548c\u6821\u9a8c\u4f4d\u9700\u8981\u4e0e\u5355\u7247\u673a\u7684\u914d\u7f6e\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h2>2\u3001SPI\u63a5\u53e3<\/h2>\n<\/p>\n<p><p>SPI\uff08Serial Peripheral Interface\uff09\u662f\u4e00\u79cd\u9ad8\u901f\u4e32\u884c\u901a\u4fe1\u534f\u8bae\uff0c\u5e38\u7528\u4e8e\u4f20\u611f\u5668\u3001\u663e\u793a\u5c4f\u7b49\u5916\u8bbe\u7684\u901a\u4fe1\u3002\u4e0e\u4e32\u53e3\u901a\u4fe1\u76f8\u6bd4\uff0cSPI\u901a\u4fe1\u5177\u6709\u66f4\u9ad8\u7684\u6570\u636e\u4f20\u8f93\u901f\u7387\uff0c\u4f46\u9700\u8981\u66f4\u591a\u7684\u786c\u4ef6\u8d44\u6e90\u3002\u901a\u8fc7Python\u7684<code>spidev<\/code>\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0SPI\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><h2>3\u3001I2C\u63a5\u53e3<\/h2>\n<\/p>\n<p><p>I2C\uff08Inter-Integrated Circuit\uff09\u662f\u4e00\u79cd\u591a\u4e3b\u4ece\u7684\u4e32\u884c\u901a\u4fe1\u534f\u8bae\uff0c\u5e38\u7528\u4e8e\u4f20\u611f\u5668\u3001\u5b58\u50a8\u5668\u7b49\u5916\u8bbe\u7684\u901a\u4fe1\u3002I2C\u901a\u4fe1\u5177\u6709\u7b80\u5355\u3001\u5360\u7528\u8d44\u6e90\u5c11\u7684\u4f18\u70b9\uff0c\u4f46\u6570\u636e\u4f20\u8f93\u901f\u7387\u8f83\u4f4e\u3002\u901a\u8fc7Python\u7684<code>smbus<\/code>\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0I2C\u901a\u4fe1\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5b89\u88c5\u76f8\u5173\u9a71\u52a8<\/strong><\/h2>\n<p><p>\u5728\u8fdb\u884cPython\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\u65f6\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u5e38\u89c1\u7684\u9a71\u52a8\u7a0b\u5e8f\u5305\u62ecUSB\u8f6c\u4e32\u53e3\u9a71\u52a8\u3001SPI\u9a71\u52a8\u3001I2C\u9a71\u52a8\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1\u3001USB\u8f6c\u4e32\u53e3\u9a71\u52a8<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528USB\u8f6c\u4e32\u53e3\u6a21\u5757\u65f6\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u5e38\u89c1\u7684USB\u8f6c\u4e32\u53e3\u82af\u7247\u5305\u62ecCH340\u3001FT232\u3001CP2102\u7b49\uff0c\u53ef\u4ee5\u901a\u8fc7\u82af\u7247\u5382\u5546\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h2>2\u3001SPI\u9a71\u52a8<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528SPI\u63a5\u53e3\u65f6\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684SPI\u9a71\u52a8\u7a0b\u5e8f\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u52a0\u8f7d\u5185\u6838\u6a21\u5757\u7684\u65b9\u5f0f\u542f\u7528SPI\u9a71\u52a8\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u7684SPI\u9a71\u52a8\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>3\u3001I2C\u9a71\u52a8<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528I2C\u63a5\u53e3\u65f6\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684I2C\u9a71\u52a8\u7a0b\u5e8f\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u52a0\u8f7d\u5185\u6838\u6a21\u5757\u7684\u65b9\u5f0f\u542f\u7528I2C\u9a71\u52a8\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u7684I2C\u9a71\u52a8\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u7f16\u5199\u901a\u4fe1\u534f\u8bae<\/strong><\/h2>\n<p><p>\u5728\u8fdb\u884cPython\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\u65f6\uff0c\u9700\u8981\u7f16\u5199\u901a\u4fe1\u534f\u8bae\uff0c\u4ee5\u5b9a\u4e49\u6570\u636e\u7684\u683c\u5f0f\u548c\u4f20\u8f93\u65b9\u5f0f\u3002\u5e38\u89c1\u7684\u901a\u4fe1\u534f\u8bae\u5305\u62ec\u81ea\u5b9a\u4e49\u534f\u8bae\u3001Modbus\u534f\u8bae\u3001CAN\u534f\u8bae\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u81ea\u5b9a\u4e49\u534f\u8bae<\/h2>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u534f\u8bae\u662f\u6839\u636e\u5177\u4f53\u9700\u6c42\u8bbe\u8ba1\u7684\u4e00\u79cd\u901a\u4fe1\u534f\u8bae\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9a\u4e49\u6570\u636e\u7684\u683c\u5f0f\u548c\u4f20\u8f93\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u534f\u8bae\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u5e27\u683c\u5f0f\uff1a\u5e27\u5934\uff081\u5b57\u8282\uff09+ \u6570\u636e\u957f\u5ea6\uff081\u5b57\u8282\uff09+ \u6570\u636e\uff08N\u5b57\u8282\uff09+ \u6821\u9a8c\u548c\uff081\u5b57\u8282\uff09<\/p>\n<h2><strong>\u6784\u9020\u6570\u636e\u5e27<\/strong><\/h2>\n<p>def construct_frame(data):<\/p>\n<p>    frame = bytearray()<\/p>\n<p>    frame.append(0xAA)  # \u5e27\u5934<\/p>\n<p>    frame.append(len(data))  # \u6570\u636e\u957f\u5ea6<\/p>\n<p>    frame.extend(data)  # \u6570\u636e<\/p>\n<p>    checksum = sum(frame) &amp; 0xFF  # \u6821\u9a8c\u548c<\/p>\n<p>    frame.append(checksum)<\/p>\n<p>    return frame<\/p>\n<h2><strong>\u89e3\u6790\u6570\u636e\u5e27<\/strong><\/h2>\n<p>def parse_frame(frame):<\/p>\n<p>    if frame[0] != 0xAA:<\/p>\n<p>        return None<\/p>\n<p>    data_length = frame[1]<\/p>\n<p>    data = frame[2:2+data_length]<\/p>\n<p>    checksum = frame[-1]<\/p>\n<p>    if checksum != (sum(frame[:-1]) &amp; 0xFF):<\/p>\n<p>        return None<\/p>\n<p>    return data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001Modbus\u534f\u8bae<\/h2>\n<\/p>\n<p><p>Modbus\u534f\u8bae\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5de5\u4e1a\u901a\u4fe1\u534f\u8bae\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5de5\u4e1a\u81ea\u52a8\u5316\u9886\u57df\u3002\u901a\u8fc7Python\u7684<code>pymodbus<\/code>\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0Modbus\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Modbus\u901a\u4fe1\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pymodbus.client.sync import ModbusSerialClient<\/p>\n<p>client = ModbusSerialClient(<\/p>\n<p>    method=&#39;rtu&#39;,<\/p>\n<p>    port=&#39;COM3&#39;,<\/p>\n<p>    baudrate=9600,<\/p>\n<p>    stopbits=1,<\/p>\n<p>    parity=&#39;N&#39;,<\/p>\n<p>    bytesize=8,<\/p>\n<p>    timeout=1<\/p>\n<p>)<\/p>\n<h2><strong>\u8bfb\u53d6\u4fdd\u6301\u5bc4\u5b58\u5668<\/strong><\/h2>\n<p>result = client.read_holding_registers(1, 1, unit=1)<\/p>\n<p>print(result.registers)<\/p>\n<h2><strong>\u5199\u5165\u4fdd\u6301\u5bc4\u5b58\u5668<\/strong><\/h2>\n<p>client.write_register(1, 100, unit=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001CAN\u534f\u8bae<\/h2>\n<\/p>\n<p><p>CAN\u534f\u8bae\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6c7d\u8f66\u603b\u7ebf\u901a\u4fe1\u534f\u8bae\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6c7d\u8f66\u7535\u5b50\u9886\u57df\u3002\u901a\u8fc7Python\u7684<code>python-can<\/code>\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0CAN\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684CAN\u901a\u4fe1\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import can<\/p>\n<p>bus = can.interface.Bus(channel=&#39;can0&#39;, bustype=&#39;socketcan&#39;)<\/p>\n<h2><strong>\u53d1\u9001CAN\u6d88\u606f<\/strong><\/h2>\n<p>msg = can.Message(arbitration_id=0x123, data=[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88], extended_id=False)<\/p>\n<p>bus.send(msg)<\/p>\n<h2><strong>\u63a5\u6536CAN\u6d88\u606f<\/strong><\/h2>\n<p>msg = bus.recv(1.0)<\/p>\n<p>print(msg)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5904\u7406\u6570\u636e\u683c\u5f0f<\/strong><\/h2>\n<p><p>\u5728\u8fdb\u884cPython\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\u65f6\uff0c\u9700\u8981\u5904\u7406\u6570\u636e\u7684\u683c\u5f0f\uff0c\u4ee5\u4fbf\u4e8e\u6570\u636e\u7684\u4f20\u8f93\u548c\u89e3\u6790\u3002\u5e38\u89c1\u7684\u6570\u636e\u683c\u5f0f\u5305\u62ec\u4e8c\u8fdb\u5236\u683c\u5f0f\u3001\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4e8c\u8fdb\u5236\u683c\u5f0f<\/h2>\n<\/p>\n<p><p>\u4e8c\u8fdb\u5236\u683c\u5f0f\u662f\u6700\u5e38\u89c1\u7684\u6570\u636e\u683c\u5f0f\u4e4b\u4e00\uff0c\u5177\u6709\u7d27\u51d1\u3001\u6548\u7387\u9ad8\u7684\u4f18\u70b9\u3002\u5728\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f<\/p>\n<p>data = 123<\/p>\n<p>binary_data = data.to_bytes(2, byteorder=&#39;big&#39;)<\/p>\n<p>print(binary_data)<\/p>\n<h2><strong>\u5c06\u4e8c\u8fdb\u5236\u683c\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>data = int.from_bytes(binary_data, byteorder=&#39;big&#39;)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5341\u516d\u8fdb\u5236\u683c\u5f0f<\/h2>\n<\/p>\n<p><p>\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u8868\u793a\u65b9\u5f0f\uff0c\u5177\u6709\u76f4\u89c2\u3001\u6613\u8bfb\u7684\u4f18\u70b9\u3002\u5728\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u683c\u5f0f<\/p>\n<p>data = 123<\/p>\n<p>hex_data = hex(data)<\/p>\n<p>print(hex_data)<\/p>\n<h2><strong>\u5c06\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>data = int(hex_data, 16)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u5b57\u7b26\u4e32\u683c\u5f0f<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u8868\u793a\u65b9\u5f0f\uff0c\u5177\u6709\u6613\u4e8e\u8c03\u8bd5\u3001\u6613\u4e8e\u5904\u7406\u7684\u4f18\u70b9\u3002\u5728\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f<\/p>\n<p>data = 123<\/p>\n<p>str_data = str(data)<\/p>\n<p>print(str_data)<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>data = int(str_data)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u901a\u8fc7\u4e32\u53e3\u901a\u4fe1\u3001\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u63a5\u53e3\u3001\u5b89\u88c5\u76f8\u5173\u9a71\u52a8\u3001\u7f16\u5199\u901a\u4fe1\u534f\u8bae\u3001\u5904\u7406\u6570\u636e\u683c\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0Python\u4e0e\u5355\u7247\u673a\u7684\u5bf9\u63a5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u63a5\u53e3\u548c\u901a\u4fe1\u534f\u8bae\uff0c\u5e76\u5904\u7406\u597d\u6570\u636e\u683c\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u901a\u4fe1\u7684\u6548\u7387\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u7684\u5355\u7247\u673a\u4e0ePython\u8fdb\u884c\u5bf9\u63a5\uff1f<\/strong><br \/>\u9009\u62e9\u5355\u7247\u673a\u65f6\uff0c\u9700\u8981\u8003\u8651\u5176\u4e0ePython\u7684\u517c\u5bb9\u6027\u548c\u652f\u6301\u7684\u901a\u4fe1\u534f\u8bae\u3002\u5e38\u89c1\u7684\u5355\u7247\u673a\u5982Arduino\u3001ESP8266\u3001ESP32\u7b49\uff0c\u901a\u5e38\u63d0\u4f9b\u4e30\u5bcc\u7684\u5e93\u548c\u6587\u6863\u652f\u6301Python\u7684\u96c6\u6210\u3002\u786e\u4fdd\u6240\u9009\u5355\u7247\u673a\u5177\u6709\u5408\u9002\u7684GPIO\u3001UART\u6216I2C\u63a5\u53e3\uff0c\u4ee5\u4fbf\u4e8e\u5b9e\u73b0\u4e0ePython\u7a0b\u5e8f\u7684\u901a\u4fe1\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u53d1\u9001\u548c\u63a5\u6536\u5355\u7247\u673a\u6570\u636e\uff1f<\/strong><br \/>\u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7\u4e32\u53e3\u901a\u4fe1\u5e93\uff08\u5982pySerial\uff09\u4e0e\u5355\u7247\u673a\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5pySerial\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u5bf9\u5e94\u7684\u4e32\u53e3\uff0c\u8bbe\u7f6e\u6ce2\u7279\u7387\u7b49\u53c2\u6570\uff0c\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002\u786e\u4fdd\u5728\u5355\u7247\u673a\u7aef\u4e5f\u914d\u7f6e\u4e86\u76f8\u5e94\u7684\u4e32\u53e3\u8bbe\u7f6e\uff0c\u4ee5\u4fdd\u8bc1\u6570\u636e\u4f20\u8f93\u7684\u6b63\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5Python\u4e0e\u5355\u7247\u673a\u7684\u901a\u4fe1\u8fc7\u7a0b\uff1f<\/strong><br \/>\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e32\u53e3\u76d1\u89c6\u5de5\u5177\uff08\u5982PuTTY\u6216Tera Term\uff09\u6765\u76d1\u63a7\u6570\u636e\u7684\u53d1\u9001\u4e0e\u63a5\u6536\u60c5\u51b5\u3002\u540c\u65f6\uff0c\u901a\u8fc7\u5728Python\u4ee3\u7801\u4e2d\u6dfb\u52a0\u65e5\u5fd7\u8f93\u51fa\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8ffd\u8e2a\u6570\u636e\u6d41\u52a8\u3002\u786e\u4fdd\u5728\u5355\u7247\u673a\u7aef\u4e5f\u6709\u76f8\u5e94\u7684\u8c03\u8bd5\u4fe1\u606f\u8f93\u51fa\uff0c\u4ee5\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e0e\u5355\u7247\u673a\u5bf9\u63a5\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u4e32\u53e3\u901a\u4fe1\u3001\u9009\u62e9\u5408\u9002\u7684\u786c\u4ef6\u63a5\u53e3\u3001\u5b89\u88c5\u76f8\u5173\u9a71\u52a8\u3001\u7f16\u5199\u901a\u4fe1\u534f\u8bae\u3001\u5904\u7406\u6570\u636e\u683c\u5f0f\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1037357,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037352"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1037352"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037352\/revisions"}],"predecessor-version":[{"id":1037362,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037352\/revisions\/1037362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1037357"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1037352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1037352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1037352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}