{"id":2799,"date":"2024-11-05T20:28:36","date_gmt":"2024-11-05T12:28:36","guid":{"rendered":"https:\/\/saludpcb.com\/zh\/?p=2799"},"modified":"2025-09-26T06:16:11","modified_gmt":"2025-09-25T22:16:11","slug":"learn-c-try-catch-with-esp32-powerful-spi-scan","status":"publish","type":"post","link":"https:\/\/saludpcb.com\/zh\/learn-c-try-catch-with-esp32-powerful-spi-scan\/","title":{"rendered":"\u4f7f\u7528 C++ Try-Catch \u548c ESP32 | \u638c\u63e1\u5f37\u5927 SPI Scan \u6280\u8853"},"content":{"rendered":"\n<hr class=\"wp-block-separator alignwide has-text-color has-palette-color-1-color has-alpha-channel-opacity has-palette-color-1-background-color has-background is-style-wide\"\/>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">\u672c\u7bc7\u4ecb\u7d39\u5982\u4f55\u5728 ESP32 \u7684 C++ \u958b\u767c\u74b0\u5883\u4e2d\uff0c\u900f\u904e try-catch \u6a5f\u5236\u5be6\u73fe SPI \u8a2d\u5099\u6383\u63cf\u3002C++ \u7684\u7570\u5e38\u8655\u7406\u4e0d\u50c5\u8b93\u958b\u767c\u8005\u80fd\u6709\u6548\u6355\u6349\u4e26\u8655\u7406\u932f\u8aa4\uff0c\u9084\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u66f4\u5177\u5065\u58ef\u6027\u3002\u6211\u5011\u6703\u9010\u6b65\u4ecb\u7d39\u5982\u4f55\u914d\u7f6e\u958b\u767c\u74b0\u5883\u3001\u5be6\u4f5c SPI \u6383\u63cf\u5668\u3001\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 std::exception \u985e\u5225\u4f86\u8655\u7406\u4e26\u56de\u5831\u6f5b\u5728\u7684 SPI \u932f\u8aa4\u3002<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Learn-C-Try-Catch-with-ESP32-Powerful-SPI-Scan-1.jpg\" alt=\"Learn C++ Try-Catch with ESP32 | Powerful SPI Scan\" class=\"wp-image-2806\" title=\"\" srcset=\"https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Learn-C-Try-Catch-with-ESP32-Powerful-SPI-Scan-1.jpg 1024w, https:\/\/saludpcb.com\/zh\/wp-content\/uploads\/2024\/11\/Learn-C-Try-Catch-with-ESP32-Powerful-SPI-Scan-1-768x768.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>\u5167\u5bb9<\/h2><nav><ul><li class=\"\"><a href=\"#\u7c21\u4ecb-1\">\u7c21\u4ecb<\/a><\/li><li class=\"\"><a href=\"#\u901a\u9053\u6578\u91cf-1-2\">\u958b\u767c\u74b0\u5883\u8a2d\u7f6e<\/a><\/li><li class=\"\"><a href=\"#\u89e3\u6790\u5ea6\">\u70ba SPI Scan \u9805\u76ee\u8a2d\u7f6e ESP32<\/a><\/li><li class=\"\"><a href=\"#\u8f38\u5165\u7bc4\u570d\">\u6a94\u6848\u7d50\u69cb<\/a><\/li><li class=\"\"><a href=\"#\u901a\u9053\u6578\u91cf\">\u4ec0\u9ebc\u662f C++ \u7684 Try-Catch \u6a5f\u5236<\/a><\/li><li class=\"\"><a href=\"#\u901a\u9053\u6578\u91cf-1\">Try-Catch \u7684\u57fa\u672c\u7d50\u69cb<\/a><\/li><li class=\"\"><a href=\"#\u901a\u9053\u6578\u91cf-1-1\">\u4ec0\u9ebc\u662f std::exception\uff1f<\/a><\/li><li class=\"\"><a href=\"#\u901a\u9053\u6578\u91cf-1-2-1\">\u81ea\u5b9a\u7fa9\u7570\u5e38\u985e<\/a><\/li><li class=\"\"><a href=\"#\u66f4\u63db\u5176\u5b83-esp-idf-\u7248\u672c\">\u4ec0\u9ebc\u662f SPI\uff08\u4e32\u884c\u5916\u8a2d\u63a5\u53e3\uff09<\/a><\/li><li class=\"\"><a href=\"#\u66f4\u63db\u5176\u5b83-esp-idf-\u7248\u672c-1\">\u4ee3\u78bc\u5be6\u73fe<\/a><\/li><li class=\"\"><a href=\"#\u7d50\u8ad6-1\">\u914d\u7f6e ESP32 \u958b\u767c\u74b0\u5883<\/a><\/li><li class=\"\"><a href=\"#\u7d50\u8ad6-1-1\">\u7de8\u8b6f\u8207\u71d2\u9304<\/a><\/li><li class=\"\"><a href=\"#\u7d50\u8ad6-1-1-2\">\u7a0b\u5f0f\u904b\u884c\u6548\u679c<\/a><\/li><li class=\"\"><a href=\"#\u7d50\u8ad6-1-1-1\">\u7d50\u8ad6<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7c21\u4ecb-1\">\u7c21\u4ecb<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">C++ \u7684 try-catch \u6a5f\u5236\u662f\u4e00\u7a2e\u7570\u5e38\u8655\u7406\u65b9\u5f0f\uff0c\u5141\u8a31\u958b\u767c\u8005\u6355\u6349\u5728\u7a0b\u5f0f\u904b\u884c\u6642\u767c\u751f\u7684\u932f\u8aa4\u4e26\u505a\u51fa\u53cd\u61c9\uff0c\u9019\u5728\u8655\u7406\u4e0d\u7a69\u5b9a\u786c\u9ad4\u6642\u5c24\u5176\u6709\u7528\u3002\u900f\u904e\u8a2d\u8a08\u4e00\u500b SPIScanner \u985e\u5225\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 try-catch \u4f86\u4fdd\u8b49 SPI \u8a2d\u5099\u5728\u521d\u59cb\u5316\u6216\u901a\u8a0a\u6642\u7684\u5b89\u5168\u6027\u3002\u5982\u679c\u901a\u8a0a\u5931\u6557\uff0c\u7570\u5e38\u8655\u7406\u6a5f\u5236\u53ef\u4ee5\u9632\u6b62\u7a0b\u5f0f\u5d29\u6f70\uff0c\u4e26\u63d0\u4f9b\u932f\u8aa4\u8cc7\u8a0a\u7d66\u958b\u767c\u8005\u6216\u4f7f\u7528\u8005\u3002SPI \u6383\u63cf\u5668\u6703\u521d\u59cb\u5316 SPI \u7e3d\u7dda\u3001\u57f7\u884c\u8a2d\u5099\u6383\u63cf\uff0c\u4e26\u5c07\u7d50\u679c\u56de\u5831\u3002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u901a\u9053\u6578\u91cf-1-2\">\u958b\u767c\u74b0\u5883\u8a2d\u7f6e<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">1. \u5f9e\u5b98\u65b9\u7db2\u7ad9\u5b89\u88dd <a href=\"https:\/\/code.visualstudio.com\" target=\"_blank\" rel=\"noopener\">Visual Studio Code<\/a>\u3002<br>2. \u5b89\u88dd <a href=\"https:\/\/saludpcb.com\/zh\/esp32-tutorial-esp-idf-with-vscode\/\">ESP-IDF \u63d2\u4ef6<\/a>\u3002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u89e3\u6790\u5ea6\">\u70ba SPI Scan \u9805\u76ee\u8a2d\u7f6e ESP32<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">1. In VS Code, press Ctrl (Cmd) + Shift + P, and enter ESP-IDF: New Project.<br><br>2. Choose a template project (e.g., blink example), set the project name (e.g., my_project), and choose a storage path.<br><br>3. The system will generate a new ESP-IDF project with a basic CMake file and example code.<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8f38\u5165\u7bc4\u570d\">\u6a94\u6848\u7d50\u69cb<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">my_project\/<br>\u251c\u2500\u2500 CMakeLists.txt     # \u69cb\u5efa\u914d\u7f6e<br>\u251c\u2500\u2500 main\/<br>\u2502   \u251c\u2500\u2500 CMakeLists.txt # \u4e3b\u7d44\u4ef6\u7684 CMake \u914d\u7f6e<br>\u2502   \u2514\u2500\u2500 main.cpp       # LED \u71c8\u5e36\u985e\u548c\u61c9\u7528\u908f\u8f2f<br>\u2514\u2500\u2500 sdkconfig          # \u9805\u76ee\u914d\u7f6e<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u901a\u9053\u6578\u91cf\">\u4ec0\u9ebc\u662f C++ \u7684 Try-Catch \u6a5f\u5236<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">C++ \u7684\u7570\u5e38\u8655\u7406\u6a5f\u5236\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7ba1\u7406\u7a0b\u5f0f\u57f7\u884c\u968e\u6bb5\u932f\u8aa4\u7684\u65b9\u5f0f\u3002\u900f\u904e\u4f7f\u7528 try \u548c catch \u5340\u584a\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728 try \u5340\u584a\u4e2d\u57f7\u884c\u53ef\u80fd\u5f15\u767c\u932f\u8aa4\u7684\u7a0b\u5f0f\u78bc\uff0c\u4e26\u5728 catch \u5340\u584a\u4e2d\u64f7\u53d6\u548c\u8655\u7406\u9019\u4e9b\u932f\u8aa4\u3002\u9019\u7a2e\u6a5f\u5236\u4f7f\u5f97\u7a0b\u5f0f\u66f4\u52a0\u5065\u58ef\uff0c\u4e26\u80fd\u907f\u514d\u56e0\u672a\u8655\u7406\u7684\u932f\u8aa4\u800c\u5c0e\u81f4\u7684\u5d29\u6f70\u3002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u901a\u9053\u6578\u91cf-1\">Try-Catch \u7684\u57fa\u672c\u7d50\u69cb<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">\u5728 C++ \u4e2d\uff0ctry-catch \u8a9e\u53e5\u7528\u65bc\u6355\u6349\u548c\u8655\u7406\u7570\u5e38\uff0c\u9019\u4f7f\u5f97\u7a0b\u5f0f\u80fd\u5728\u51fa\u73fe\u932f\u8aa4\u6642\u7a69\u5b9a\u904b\u884c\u4e26\u59a5\u5584\u8655\u7406\u3002\u4e0b\u9762\u662f\u9019\u6bb5 try-catch \u8a9e\u53e5\u7684\u8a73\u89e3\uff1a<\/pre>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-a6fce3ca649d7f98cf0a54955d09006c\"><code lang=\"cpp\" class=\"language-cpp\">try {\n    \/\/ Code that might throw an exception\n} catch (const std::exception&amp; e) {\n    \/\/ Handle the exception\n    ESP_LOGE(\"Error\", \"Caught exception: %s\", e.what());\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">1. \u7576 try \u5340\u584a\u5167\u7684\u7a0b\u5f0f\u78bc\u62cb\u51fa\u7570\u5e38\u6642\uff0c\u7a0b\u5f0f\u6703\u8df3\u81f3\u5c0d\u61c9\u7684 catch \u5340\u584a\u57f7\u884c\u7570\u5e38\u8655\u7406\u3002<br><br>2. catch (const std::exception&amp; e) \u8868\u793a\u6355\u6349\u6240\u6709 std::exception \u985e\u578b\u53ca\u5176\u6d3e\u751f\u985e\u578b\u7684\u7570\u5e38\u3002<br><br>3. e.what() \u662f\u4e00\u500b\u6210\u54e1\u51fd\u6578\uff0c\u8fd4\u56de\u95dc\u65bc\u7570\u5e38\u7684\u7c21\u77ed\u63cf\u8ff0\uff0c\u6709\u52a9\u65bc\u958b\u767c\u4eba\u54e1\u4e86\u89e3\u7570\u5e38\u7684\u5177\u9ad4\u60c5\u6cc1\u3002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u901a\u9053\u6578\u91cf-1-1\">\u4ec0\u9ebc\u662f std::exception\uff1f<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">std::exception \u662f C++ \u6a19\u6e96\u5eab\u4e2d\u6240\u6709\u7570\u5e38\u985e\u7684\u57fa\u985e\u3002\u5b83\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u7570\u5e38\u8655\u7406\u529f\u80fd\uff0c\u5141\u8a31\u7528\u6236\u5b9a\u7fa9\u81ea\u5df1\u7684\u7570\u5e38\u985e\u4e26\u7e7c\u627f\u81ea std::exception\u3002\u6240\u6709\u81ea\u5b9a\u7fa9\u7570\u5e38\u985e\u90fd\u53ef\u4ee5\u5229\u7528 std::exception \u63d0\u4f9b\u7684\u63a5\u53e3\uff0c\u4f7f\u5f97\u7570\u5e38\u8655\u7406\u66f4\u52a0\u4e00\u81f4\u3002<br><br>std::exception \u7684\u4e3b\u8981\u529f\u80fd<br>1. what() \u65b9\u6cd5: std::exception \u63d0\u4f9b\u4e86\u4e00\u500b\u865b\u51fd\u6578 what()\uff0c\u7528\u65bc\u8fd4\u56de\u63cf\u8ff0\u7570\u5e38\u7684 C \u98a8\u683c\u5b57\u7b26\u4e32\u3002\u7528\u6236\u53ef\u4ee5\u91cd\u5beb\u8a72\u65b9\u6cd5\u4ee5\u63d0\u4f9b\u66f4\u5177\u9ad4\u7684\u7570\u5e38\u4fe1\u606f\u3002<br><br>2. \u985e\u578b\u5b89\u5168: \u4f7f\u7528 std::exception \u53ca\u5176\u5b50\u985e\u53ef\u4ee5\u78ba\u4fdd\u7570\u5e38\u8655\u7406\u7684\u985e\u578b\u5b89\u5168\uff0c\u56e0\u70ba catch \u8a9e\u53e5\u53ef\u4ee5\u6355\u7372\u6240\u6709\u985e\u578b\u7684\u7570\u5e38\u3002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u901a\u9053\u6578\u91cf-1-2-1\">\u81ea\u5b9a\u7fa9\u7570\u5e38\u985e<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">\u4ee5\u4e0b\u662f\u500b\u81ea\u5b9a\u7fa9\u7570\u5e38\u985e\u7684\u793a\u4f8b\uff0c\u7e7c\u627f\u81ea std::exception\uff1a<\/pre>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-34ec62934ef9e65f380d6680d0b5581f\"><code lang=\"cpp\" class=\"language-cpp\">#include &lt;exception>\n#include &lt;string>\n\nclass MyException : public std::exception {\nprivate:\n    std::string msg;\npublic:\n    MyException(const std::string&amp; message) : msg(message) {}\n    const char* what() const noexcept override {\n        return msg.c_str();\n    }\n};<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0cMyException \u7e7c\u627f\u81ea std::exception\uff0c\u4e26\u63d0\u4f9b\u4e86\u81ea\u5b9a\u7fa9\u7684\u932f\u8aa4\u6d88\u606f\u3002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u66f4\u63db\u5176\u5b83-esp-idf-\u7248\u672c\">\u4ec0\u9ebc\u662f SPI\uff08\u4e32\u884c\u5916\u8a2d\u63a5\u53e3\uff09<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">SPI\uff08Serial Peripheral Interface\uff09\u662f\u4e00\u7a2e\u540c\u6b65\u4e32\u884c\u901a\u4fe1\u5354\u8b70\uff0c\u5ee3\u6cdb\u7528\u65bc\u5fae\u63a7\u5236\u5668\u8207\u5404\u7a2e\u5916\u90e8\u8a2d\u5099\uff08\u5982\u50b3\u611f\u5668\u3001\u5b58\u5132\u5668\u548c\u986f\u793a\u5668\uff09\u4e4b\u9593\u7684\u901a\u4fe1\u3002SPI \u901a\u904e\u56db\u500b\u57fa\u672c\u4fe1\u865f\u7dda\u4f86\u5be6\u73fe\u6578\u64da\u50b3\u8f38\uff1a<br><br>1. MOSI\uff08Master Out Slave In\uff09: \u4e3b\u8a2d\u5099\u8f38\u51fa\uff0c\u5916\u8a2d\u8f38\u5165\u7684\u6578\u64da\u7dda\u3002<br><br>2. MISO\uff08Master In Slave Out\uff09: \u4e3b\u8a2d\u5099\u8f38\u5165\uff0c\u5916\u8a2d\u8f38\u51fa\u7684\u6578\u64da\u7dda\u3002<br><br>3. SCLK\uff08Serial Clock\uff09: \u7531\u4e3b\u8a2d\u5099\u751f\u6210\u7684\u6642\u9418\u4fe1\u865f\u3002<br><br>4. CS\uff08Chip Select\uff09: \u7528\u65bc\u9078\u64c7\u5177\u9ad4\u7684\u5916\u8a2d\u3002<br><br>SPI \u7684\u4e3b\u8981\u512a\u9ede\u5305\u62ec\u9ad8\u901f\u50b3\u8f38\u548c\u5168\u96d9\u5de5\u901a\u4fe1\u80fd\u529b\uff0c\u4f46\u5b83\u9700\u8981\u66f4\u591a\u7684\u5f15\u8173\u548c\u66f4\u8907\u96dc\u7684\u6642\u5e8f\u63a7\u5236\u3002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u66f4\u63db\u5176\u5b83-esp-idf-\u7248\u672c-1\">\u4ee3\u78bc\u5be6\u73fe<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">\u4e0b\u9762\u662f\u4e00\u500b\u4f7f\u7528 C++ \u7570\u5e38\u8655\u7406\u6a5f\u5236\u7684 ESP32 SPI \u6383\u63cf\u5668\u793a\u4f8b\u4ee3\u78bc\u3002\u8a72\u4ee3\u78bc\u4f7f\u7528 ESP-IDF \u5eab\u9032\u884c SPI \u901a\u4fe1\uff0c\u4e26\u901a\u904e\u81ea\u5b9a\u7fa9\u7570\u5e38\u985e\u4f86\u8655\u7406\u53ef\u80fd\u51fa\u73fe\u7684\u932f\u8aa4\u3002<\/pre>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-fc474abace4ede7f4746d7f62da38e51\"><code lang=\"cpp\" class=\"language-cpp\">\/\/ main.cpp\n#include &lt;stdio.h>\n#include &lt;stdint.h>\n#include &lt;stddef.h>\n#include &lt;string>\n#include \"freertos\/FreeRTOS.h\"\n#include \"freertos\/task.h\"\n#include \"driver\/spi_master.h\"\n#include \"driver\/gpio.h\"\n#include \"esp_log.h\" \/\/ Include ESP log header\n\n\/\/ Define a tag for logging\nstatic const char *TAG = \"SPIScanner\"; \/\/ Tag for log messages\n\n\/\/ Custom exception class for handling SPI errors\nclass SPIException : public std::exception {\nprivate:\n    std::string msg; \/\/ Message describing the exception\npublic:\n    \/\/ Constructor that initializes the exception message\n    SPIException(const std::string&amp; message) : msg(message) {}\n    \n    \/\/ Override the what() method to return the exception message\n    const char* what() const noexcept override {\n        return msg.c_str();\n    }\n};\n\n\/\/ Class responsible for scanning SPI devices\nclass SPIScanner {\nprivate:\n    spi_device_handle_t spi; \/\/ Handle for the SPI device\n\n    \/\/ Initialize the SPI bus\n    void initSPI(int mosi, int miso, int sclk, int cs) {\n        \/\/ Configuration structure for the SPI bus\n        spi_bus_config_t buscfg = {\n            .mosi_io_num = mosi,  \/\/ Master Out Slave In pin\n            .miso_io_num = miso,  \/\/ Master In Slave Out pin\n            .sclk_io_num = sclk,  \/\/ Serial Clock pin\n            .quadwp_io_num = -1,  \/\/ Not used in this configuration\n            .quadhd_io_num = -1,  \/\/ Not used in this configuration\n            .max_transfer_sz = 0, \/\/ Maximum transfer size\n            .flags = SPICOMMON_BUSFLAG_MASTER \/\/ Set as master\n        };\n\n        \/\/ Configuration structure for the SPI device\n        spi_device_interface_config_t devcfg = {\n            .mode = 0, \/\/ SPI mode (clock polarity and phase)\n            .clock_speed_hz = 1000000, \/\/ Clock speed in Hz\n            .spics_io_num = cs, \/\/ Chip Select pin\n            .queue_size = 7, \/\/ Transaction queue size\n        };\n\n        \/\/ Initialize the SPI bus\n        esp_err_t ret = spi_bus_initialize(HSPI_HOST, &amp;buscfg, SPI_DMA_CH_AUTO);\n        if (ret != ESP_OK) {\n            throw SPIException(\"Failed to initialize SPI bus\"); \/\/ Throw an exception if initialization fails\n        }\n\n        \/\/ Add the SPI device to the bus\n        ret = spi_bus_add_device(HSPI_HOST, &amp;devcfg, &amp;spi);\n        if (ret != ESP_OK) {\n            throw SPIException(\"Failed to add device to SPI bus\"); \/\/ Throw an exception if adding the device fails\n        }\n    }\n\npublic:\n    \/\/ Constructor for SPIScanner that initializes SPI\n    SPIScanner(int mosi, int miso, int sclk, int cs) {\n        try {\n            initSPI(mosi, miso, sclk, cs); \/\/ Call the initialization method\n            ESP_LOGI(TAG, \"SPI Scanner initialized successfully\"); \/\/ Log successful initialization\n        } catch (const SPIException&amp; e) {\n            ESP_LOGE(TAG, \"SPI initialization failed: %s\", e.what()); \/\/ Log initialization failure\n            throw; \/\/ Rethrow the exception\n        }\n    }\n\n    \/\/ Destructor to clean up SPI device\n    ~SPIScanner() {\n        spi_bus_remove_device(spi); \/\/ Remove the device from the bus\n        spi_bus_free(HSPI_HOST); \/\/ Free the SPI bus resources\n    }\n\n    \/\/ Method to scan for SPI devices\n    void scan() {\n        uint8_t tx_data = 0xFF; \/\/ Data to transmit (dummy data)\n        uint8_t rx_data = 0; \/\/ Variable to store received data\n\n        \/\/ Transaction structure for the SPI communication\n        spi_transaction_t t = {\n            .length = 8, \/\/ Transaction length in bits\n            .tx_buffer = &amp;tx_data, \/\/ Pointer to the transmit buffer\n            .rx_buffer = &amp;rx_data \/\/ Pointer to the receive buffer\n        };\n\n        ESP_LOGI(TAG, \"Starting SPI scan...\"); \/\/ Log the start of the scan\n\n        \/\/ Transmit the data and receive the response\n        esp_err_t ret = spi_device_transmit(spi, &amp;t);\n        if (ret != ESP_OK) {\n            throw SPIException(\"SPI transmission failed\"); \/\/ Throw an exception if transmission fails\n        }\n\n        ESP_LOGI(TAG, \"Received data: 0x%02X\", rx_data); \/\/ Log the received data\n    }\n};\n\n\/\/ ESP-IDF entry point for the application\nextern \"C\" void app_main(void) {\n    try {\n        SPIScanner scanner(13, 12, 14, 15); \/\/ Create an instance of SPIScanner with specified GPIO pins\n        while(1) {\n            try {\n                scanner.scan(); \/\/ Scan for SPI devices\n            } catch (const SPIException&amp; e) {\n                ESP_LOGW(TAG, \"Scan iteration failed: %s, Continuing...\", e.what()); \/\/ Log scan failure\n            }\n            vTaskDelay(pdMS_TO_TICKS(1000)); \/\/ Wait for 1 second before the next scan\n        }\n    } catch (const SPIException&amp; e) {\n        ESP_LOGE(TAG, \"Fatal error: %s, System halted.\", e.what()); \/\/ Log fatal errors\n    }\n}<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7d50\u8ad6-1\">\u914d\u7f6e ESP32 \u958b\u767c\u74b0\u5883<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">\u5728\u958b\u59cb\u7de8\u5beb\u7a0b\u5f0f\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u5df2\u7d93\u8a2d\u5b9a\u597d ESP32 \u7684\u958b\u767c\u74b0\u5883\u3002\u6211\u5011\u5c07\u4f7f\u7528 VS Code \u642d\u914d ESP-IDF \u63d2\u4ef6\u9032\u884c\u958b\u767c\u3002<br><br>\u914d\u7f6e sdkconfig<br>\u82e5\u8981\u5728 ESP32 \u958b\u767c\u4e2d\u555f\u7528 C++ \u7684\u7570\u5e38\u8655\u7406\u529f\u80fd\uff0c\u4f60\u9700\u8981\u5728 SDK \u914d\u7f6e\u4e2d\u958b\u555f C++ \u7570\u5e38\u8655\u7406\u3002\u5728 VS Code \u4e2d\uff0c\u958b\u555f ESP-IDF \u63d2\u4ef6\u63d0\u4f9b\u7684 SDK Configuration Editor\uff0c\u627e\u5230\u4e26\u555f\u7528\u4ee5\u4e0b\u9078\u9805\uff1a<br><br><strong>Compiler options: \u52fe\u9078 \"Enable C++ exceptions\"<\/strong><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6211\u5011\u9084\u9700\u8981\u5728 CMakeLists.txt \u4e2d\u52a0\u5165\u7de8\u8b6f\u9078\u9805\u4f86\u555f\u7528\u7570\u5e38\u8655\u7406\u548c\u8a2d\u5b9a C++ \u6a19\u6e96<br><br># For more information about build system see<br># https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/api-guides\/build-system.html# The following five lines of boilerplate have to be in your project's<br># CMakeLists in this exact order for cmake to work correctly<br><br>cmake_minimum_required(VERSION 3.5)<br><br><strong>set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -fexceptions\")<br>set(CMAKE_CXX_STANDARD 11)<br>set(CMAKE_CXX_STANDARD_REQUIRED ON)<\/strong><br><br>include($ENV{IDF_PATH}\/tools\/cmake\/project.cmake)<br><br>....<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7d50\u8ad6-1-1\">\u7de8\u8b6f\u8207\u71d2\u9304<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">\u5728 VSCode \u8996\u7a97\u6700\u4e0b\u65b9\u4e2d\u627e\u5230 \"ESP32-IDF : Build, Flash and Monitor\" ICON \u57f7\u884c\u548c\u71d2\u9304\u3002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7d50\u8ad6-1-1-2\">\u7a0b\u5f0f\u904b\u884c\u6548\u679c<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">\u904b\u884c\u7a0b\u5f0f\u5f8c\uff0c\u60a8\u5c07\u5728\u7d42\u7aef\u6a5f\u4e2d\u770b\u5230\u4ee5\u4e0b\u8f38\u51fa\uff1a<\/pre>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-183f7ce1c4f3eae9debfde6efba741a1\"><code lang=\"bash\" class=\"language-bash\">I (123) [SPIScanner]: SPI Scanner initialized successfully\nI (223) [SPIScanner]: Starting SPI scan...\nI (223) [SPIScanner]: Received data: 0xAA\nI (1233) [SPIScanner]: Starting SPI scan...\nI (1233) [SPIScanner]: Received data: 0xBB\nI (2233) [SPIScanner]: Starting SPI scan...\nW (2233) [SPIScanner]: Scan iteration failed: SPI transmission failed, Continuing...\nI (3233) [SPIScanner]: Starting SPI scan...\nI (3233) [SPIScanner]: Received data: 0xCC<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u7d50\u8ad6-1-1-1\">\u7d50\u8ad6<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:0px\"><\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">\u5728 C++ \u4e2d\uff0c\u5229\u7528 try-catch \u6a5f\u5236\u5c0d SPI \u901a\u4fe1\u4e2d\u7684\u932f\u8aa4\u9032\u884c\u6709\u6548\u7ba1\u7406\u5c0d\u65bc\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\u81f3\u95dc\u91cd\u8981\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u9084\u78ba\u4fdd\u610f\u5916\u554f\u984c\u80fd\u5920\u5f97\u5230\u512a\u96c5\u8655\u7406\uff0c\u9632\u6b62\u61c9\u7528\u7a0b\u5f0f\u5d29\u6f70\u3002<br><br>\u5728\u6211\u5011\u5be6\u73fe\u7684 SPIScanner \u985e\u4e2d\uff0ctry-catch \u5340\u584a\u7528\u65bc\u6355\u7372\u8a2d\u5099\u6383\u63cf\u904e\u7a0b\u4e2d\u7684\u7570\u5e38\uff0c\u540c\u6642\u8a18\u9304\u4fe1\u606f\u8c50\u5bcc\u7684\u932f\u8aa4\u8a0a\u606f\uff0c\u4e26\u5141\u8a31\u6383\u63cf\u904e\u7a0b\u7e7c\u7e8c\u9032\u884c\u3002\u9019\u78ba\u4fdd\u4e86\u6240\u6709\u6f5b\u5728\u5730\u5740\u90fd\u88ab\u6aa2\u67e5\uff0c\u5f9e\u800c\u6700\u5927\u9650\u5ea6\u5730\u63d0\u9ad8\u4e86\u6aa2\u6e2c\u53ef\u7528\u8a2d\u5099\u7684\u6a5f\u6703\u3002<br><br>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u7406\u89e3\u5982\u4f55\u5728 SPI \u5c08\u6848\u4e2d\u6709\u6548\u5229\u7528 try-catch\u3002\u904b\u7528\u9019\u4e9b\u6280\u8853\u4f86\u69cb\u5efa\u66f4\u5177\u97cc\u6027\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u63d0\u5347\u4f60\u7684\u7269\u806f\u7db2\u958b\u767c\u4e4b\u65c5\u3002\u795d\u4f60\u7de8\u78bc\u6109\u5feb\uff01<\/pre>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator alignwide has-text-color has-palette-color-1-color has-alpha-channel-opacity has-palette-color-1-background-color has-background is-style-wide\"\/>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u4ecb\u7d39\u5982\u4f55\u5728 ESP32 \u7684 C++ \u958b\u767c\u74b0\u5883\u4e2d\uff0c\u900f\u904e try-catch \u6a5f\u5236\u5be6\u73fe SPI \u8a2d\u5099\u6383\u63cf\u3002C+ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2805,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,53,83],"tags":[61,27,63,20,60,10,64,11,62],"class_list":["post-2799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-module","category-esp32","category-featured","tag-bluetooth","tag-c","tag-firmware","tag-hardware","tag-iot","tag-mcu","tag-module","tag-tutorial","tag-wifi"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/comments?post=2799"}],"version-history":[{"count":7,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2799\/revisions"}],"predecessor-version":[{"id":2808,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/posts\/2799\/revisions\/2808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/media\/2805"}],"wp:attachment":[{"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/media?parent=2799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/categories?post=2799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saludpcb.com\/zh\/wp-json\/wp\/v2\/tags?post=2799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}