{"id":51508,"date":"2026-08-04T20:24:20","date_gmt":"2026-08-04T12:24:20","guid":{"rendered":"https:\/\/www.modbus.cn\/51508.html"},"modified":"2026-08-04T20:24:20","modified_gmt":"2026-08-04T12:24:20","slug":"visualhmi-%e6%96%87%e4%bb%b6-io%ef%bc%9alua-%e8%af%bb%e5%86%99-sd-%e5%8d%a1-u-%e7%9b%98-flash-%e4%b8%8e%e7%9b%ae%e5%bd%95%e5%88%97%e4%b8%be","status":"publish","type":"post","link":"http:\/\/www.modbus.cn\/en\/51508.html","title":{"rendered":"VisualHMI \u6587\u4ef6 IO\uff1aLUA \u8bfb\u5199 SD \u5361\/U \u76d8\/Flash \u4e0e\u76ee\u5f55\u5217\u4e3e"},"content":{"rendered":"<p>LUA \u652f\u6301\u8bfb\u5199 SD \u5361 \/ U \u76d8 \/ \u5185\u90e8 Flash \u6587\u4ef6\uff1a\u76ee\u5f55\u5217\u4e3e\u3001\u6253\u5f00\u3001\u8bfb\u3001\u5199\u3001\u5b9a\u4f4d\u3001\u5173\u6587\u4ef6\u3002\u9002\u5408\u914d\u65b9\u6587\u4ef6\u3001\u6570\u636e\u5bfc\u51fa\u3001\u65e5\u5fd7\u843d\u5730\u3002<\/p>\n<h2>1. \u8def\u5f84\u4e0e\u76d8\u7b26<\/h2>\n<table border=\"1\" cellpadding=\"6\" style=\"border-collapse:collapse;font-size:14px\">\n<tr>\n<th>\u7cfb\u5217<\/th>\n<th>SD\u5361<\/th>\n<th>U\u76d8<\/th>\n<th>\u5185\u90e8Flash<\/th>\n<\/tr>\n<tr>\n<td>M \u7cfb\u5217<\/td>\n<td>1:<\/td>\n<td>2:<\/td>\n<td>3:<\/td>\n<\/tr>\n<tr>\n<td>DH \u7cfb\u5217<\/td>\n<td>\/sdcard<\/td>\n<td>\/udisk<\/td>\n<td>\/data<\/td>\n<\/tr>\n<\/table>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.modbus.cn\/wp-content\/uploads\/2026\/08\/visualhmi\/lu012_1.png\" style=\"max-width:100%;border:1px solid #eee;margin:8px 0\" loading=\"lazy\" title=\"VisualHMI \u6587\u4ef6 IO\uff1aLUA \u8bfb\u5199 SD \u5361\/U \u76d8\/Flash \u4e0e\u76ee\u5f55\u5217\u4e3e\u63d2\u56fe\" alt=\"VisualHMI \u6587\u4ef6 IO\uff1aLUA \u8bfb\u5199 SD \u5361\/U \u76d8\/Flash \u4e0e\u76ee\u5f55\u5217\u4e3e\u63d2\u56fe\" \/><\/p>\n<h2>2. API<\/h2>\n<table border=\"1\" cellpadding=\"6\" style=\"border-collapse:collapse;font-size:14px\">\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<tr>\n<td>list_dir(path)<\/td>\n<td>\u5217\u4e3e\u76ee\u5f55\uff0c\u7ed3\u679c\u901a\u8fc7 on_list_dir \u9010\u6761\u56de\u8c03<\/td>\n<\/tr>\n<tr>\n<td>on_list_dir(path, filename, type, fsize)<\/td>\n<td>\u56de\u8c03\uff1atype 0=\u6587\u4ef6\u5939\/1=\u6587\u4ef6\uff0cfsize \u6587\u4ef6\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td>file_open(path, mode)<\/td>\n<td>\u6253\u5f00\u6587\u4ef6\uff1amode 0=FA_READ\uff08\u8bfb\uff09\/1=FA_WRITE\uff08\u8986\u76d6\u5199\uff09\/2=FA_WRITE_ADD\uff08\u8ffd\u52a0\uff09<\/td>\n<\/tr>\n<tr>\n<td>file_close()<\/td>\n<td>\u5173\u95ed\u5f53\u524d\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>file_size()<\/td>\n<td>\u5f53\u524d\u6587\u4ef6\u5b57\u8282\u6570<\/td>\n<\/tr>\n<tr>\n<td>file_seek(offset)<\/td>\n<td>\u5b9a\u4f4d\u8bfb\u5199\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>file_read(count)<\/td>\n<td>\u8bfb count \u5b57\u8282\uff081~2048\uff09\uff0c\u8fd4\u56de\u4e0b\u6807\u4ece 1 \u7684\u5b57\u8282\u8868<\/td>\n<\/tr>\n<tr>\n<td>file_write(data)<\/td>\n<td>\u5199\u6570\u636e<\/td>\n<\/tr>\n<\/table>\n<h2>3. \u6848\u4f8b\uff1aSD \u5361\u76ee\u5f55\u5217\u8868<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">function on_init()\n  list_dir(\"1:\/music\")   -- \u5217SD\u5361music\u76ee\u5f55\nend\n\nfunction on_list_dir(path, filename, type, fsize)\n  if type == 1 then  -- \u6587\u4ef6\n    print(path .. filename .. \" \" .. fsize .. \"B\")\n  end\nend<\/pre>\n<h2>4. \u6848\u4f8b\uff1a\u8bfb\u6587\u4ef6\uff08\u6d41\u5f0f\u5904\u7406\uff09<\/h2>\n<pre style=\"background:#f5f5f5;padding:10px;border-radius:4px\">function read_file_ok()\n  file_open(\"1:\/data\/recipe.txt\", 0)  -- \u8bfb\u6a21\u5f0f\n  local remain = file_size()\n  local line = \"\"\n  while remain &gt; 0 do\n    local n = (remain &gt; 64) and 64 or remain\n    local buf = file_read(n)          -- \u6bcf\u6b21\u6700\u591a2048, \u5efa\u8bae\u5c0f\u5757\n    -- \u5904\u7406 buf...\n    remain = remain - n\n  end\n  file_close()\nend<\/pre>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.modbus.cn\/wp-content\/uploads\/2026\/08\/visualhmi\/lu012_2.png\" style=\"max-width:100%;border:1px solid #eee;margin:8px 0\" loading=\"lazy\" title=\"VisualHMI \u6587\u4ef6 IO\uff1aLUA \u8bfb\u5199 SD \u5361\/U \u76d8\/Flash \u4e0e\u76ee\u5f55\u5217\u4e3e\u63d2\u56fe1\" alt=\"VisualHMI \u6587\u4ef6 IO\uff1aLUA \u8bfb\u5199 SD \u5361\/U \u76d8\/Flash \u4e0e\u76ee\u5f55\u5217\u4e3e\u63d2\u56fe1\" \/><\/p>\n<div class=\"tip\" style=\"border-left:4px solid #f7941e;padding:10px 14px;background:#fff8ef;margin:14px 0\">\u6587\u4ef6 IO \u6700\u5bb9\u6613\u8e29\u5185\u5b58\u6ea2\u51fa\u7684\u5751\uff1a<strong>\u4e25\u7981\u5168\u5c40\u7d2f\u8ba1\u5927\u7f13\u51b2\u2014\u2014\u6bcf\u6b21 file_read \u540e\u7acb\u5373\u89e3\u6790\u91ca\u653e\uff0c\u5fc5\u987b\u6d41\u5f0f\u5904\u7406\uff0c\u5426\u5219\u5185\u5b58\u6ea2\u51fa\u89e6\u53d1\u770b\u95e8\u72d7\u590d\u4f4d<\/strong>\u3002\u5199 SD \u5361\u6ce8\u610f\u6587\u4ef6\u5df2\u5b58\u5728\u65f6 mode=2 \u8ffd\u52a0\u3001mode=1 \u8986\u76d6\u3002<\/div>\n<p style=\"color:#888;font-size:13px\">\u6574\u7406\u81ea \u5e7f\u5dde\u5927\u5f69\u79d1\u6280 VisualHMI \u5f00\u53d1\u6587\u6863\uff08hmi-doc.gz-dc.com\uff09LUA \u6559\u7a0b\u300c\u6587\u4ef6IO\u300d\uff0c\u7248\u6743\u5f52\u5927\u5f69\u79d1\u6280\u6240\u6709\u3002<\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>LUA \u652f\u6301\u8bfb\u5199 SD \u5361 \/ U \u76d8 \/ \u5185\u90e8 Flash&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[3131,3126],"tags":[3357,3358,3359,716,3134,3356,3360],"class_list":["post-51508","post","type-post","status-publish","format-standard","hentry","category-hmi-lua","category-hmi-dev","tag-file_open","tag-file_read","tag-list_dir","tag-sd","tag-visualhmi","tag-io","tag-3360"],"_links":{"self":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/comments?post=51508"}],"version-history":[{"count":0,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/51508\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=51508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=51508"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=51508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}