Skip to content

✏️ 基础语法 ​

掌握 Lua 变量、数据类型和运算符


📖 变量与赋值 ​

Lua 中变量默认是全局的,使用 local 声明局部变量:

lua
-- 全局变量
count = 10

-- 局部变量(推荐)
local name = "LPPLC"
local temperature = 25.5
local is_running = true

建议:始终使用 local,避免全局变量污染。在 LPPLC 中全局变量会一直存在,可能导致内存泄漏。

多重赋值 ​

lua
local a, b = 1, 2       -- a=1, b=2
local x, y = 10, 20, 30  -- x=10, y=20, 30被丢弃
local m, n = 100          -- m=100, n=nil

📦 数据类型 ​

Lua 的 8 种基本数据类型

Lua 有 8 种基本数据类型:

类型说明示例
nil空值,表示不存在nil
boolean布尔值true, false
number数值(整数和浮点数统一)42, 3.14
string字符串"hello", 'world'
table表(数组+字典){1,2,3}, {x=1}
function函数function() end
userdataC 数据(LPPLC 模块对象)http.creat() 返回值
thread协程coroutine.create()

类型判断 ​

lua
local val = 42
print(type(val))        -- "number"
print(type("hello"))    -- "string"
print(type(nil))        -- "nil"

-- 判断是否为某类型
if type(val) == "number" then
    print("这是一个数字")
end

nil 与 false ​

在 Lua 中,只有 nil 和 false 是假值,其余(包括 0 和空字符串 "")都是真值:

lua
-- 这些都是真值!
if 0 then print("0 是真") end           -- 输出
if "" then print("空字符串是真") end     -- 输出
if {} then print("空表是真") end         -- 输出

-- 这些是假值
if nil then print("不会执行") end
if false then print("不会执行") end

🔢 数值 ​

Lua 5.3 中数值分为整数(integer)和浮点数(float):

lua
local a = 42          -- 整数
local b = 3.14        -- 浮点数
local c = 0xff        -- 十六进制 = 255
local d = 1e3         -- 科学计数法 = 1000.0

-- 数值转换
local e = math.floor(3.7)   -- 3(向下取整,结果为整数)
local f = tonumber("123")   -- 123
local g = tostring(42)      -- "42"

整数除法与取模 ​

lua
local a = 10 // 3     -- 3(整数除法,Lua 5.3+)
local b = 10 / 3      -- 3.3333...(浮点除法)
local c = 10 % 3      -- 1(取模)

📝 字符串 ​

lua
-- 三种定义方式
local s1 = "双引号字符串"
local s2 = '单引号字符串'
local s3 = [[多行
字符串]]

-- 常用操作
local s = "Hello, LPPLC"
print(#s)                    -- 13(长度)
print(string.upper(s))       -- "HELLO, LPPLC"
print(string.sub(s, 1, 5))   -- "Hello"
print(string.find(s, "LP"))  -- 8, 9
print(string.format("温度: %.1f℃", 25.5))  -- "温度: 25.5℃"

-- 字符串连接
local result = "温度" .. 25 .. "℃"   -- "温度25℃"

注意:LPPLC 环境中字符串占用堆内存,避免频繁拼接大字符串,建议用 string.format 或 table.concat。


➕ 运算符 ​

算术运算符 ​

运算符说明示例
+加3 + 2 → 5
-减5 - 2 → 3
*乘3 * 2 → 6
/浮点除7 / 2 → 3.5
//整数除7 // 2 → 3
%取模7 % 2 → 1
^幂2 ^ 3 → 8
-负号-5

比较运算符 ​

运算符说明示例
==等于1 == 1 → true
~=不等于1 ~= 2 → true
<小于1 < 2 → true
>大于2 > 1 → true
<=小于等于1 <= 1 → true
>=大于等于2 >= 1 → true

注意:Lua 使用 ~= 表示不等于,而不是 !=。

逻辑运算符 ​

运算符说明示例
and与true and false → false
or或true or false → true
not非not true → false

and 和 or 是短路求值,返回的是操作数而非布尔值:

lua
local a = nil and 5     -- nil(nil 为假,直接返回 nil)
local b = 3 and 5       -- 5(3 为真,返回第二个操作数)
local c = nil or 10     -- 10(nil 为假,返回第二个操作数)
local d = 3 or 10       -- 3(3 为真,直接返回第一个操作数)

-- 实用技巧:设置默认值
local port = port or 502   -- 如果 port 为 nil,则使用 502

📌 注释 ​

lua
-- 单行注释

--[[
  多行注释
  第二行
]]

---[[
  巧用注释开关:取消第一行的 -- 即可启用此块代码
  这段代码现在会执行
--]]

⚠️ LPPLC 环境注意事项 ​

  1. 内存有限:ESP32 可用 Lua 堆内存约 40-80KB,避免创建超大表或字符串
  2. 全局变量持久:脚本修改后全局变量依然存在,建议全部使用 local
  3. 没有 io 写权限:文件操作仅限 SPIFFS 分区,不建议频繁写入
  4. 整数精度:D 寄存器 16 位有符号范围 -32768~32767,32 位范围 -2147483648~2147483647

上一篇:Lua 概述 | 下一篇:流程控制

LPPLC 工业物联网云平台 · 软硬件与云平台全栈自研