Lua教程手册

https://www.runoob.com/lua/lua-tutorial.html

LuaSocket使用

  LuaSocket 是 Lua 的网络模块库,它可以很方便地提供 TCP、UDP、DNS、FTP、HTTP、SMTP、MIME 等多种网络协议的访问操作。

socket方式请求

-- socket方式请求
local socket = require("socket")
local host = "100.42.237.125"
local file = "/"
local sock = assert(socket.connect(host, 80))  -- 创建一个 TCP 连接,连接到 HTTP 连接的标准 80 端口上
sock:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
repeat
    local chunk, status, partial = sock:receive(1024) -- 以 1K 的字节块来接收数据,并把接收到字节块输出来
    -- print(chunk or partial)
until status ~= "closed" -- 直到检测到服务端关闭,结束该循环
sock:close()  -- 关闭 TCP 连接
settimeout(0)
-- 表示如果没有数据就立即返回
settimeout(2)
-- 表示2s内如果没有数据就立即返回

方法一

local socket = require("socket.core")
local tcp = socket.tcp()
local host = '172.16.20.55'
local port = '8000'
local clicon = tcp:connect(host,port)
if(clicon) then
    print('connect '..host..' ok!')
else
    print('connect error')
end

var1 = "Test_paizhao"
flag = "0"

local tcpsend = tcp:send(var1)
if(tcpsend) then
    print('tcpsend ok')
else
    print('tcpsend err')
end

sleep(1)
tcp:settimeout(4)

function rec_msg()
    str1 = tcp:receive(20)
    if(str1) then
        print('tcprev:'..str1)    
        flag=string.sub(str1,2,2)
    else
        print('tcp rec err...')
    end
end

rec_msg()
print("start str1: ", str1)

while (true) do
    if str1~=nil then
        str0=string.sub(str1,1,1)
    end
    
    if str0~="#" then               
        tcp:send(var1)
        sleep(1)
        rec_msg()
        print(str1)
    else 
        flag=string.sub(str1,2,2)  
        print("str1: ", str1)
        print("flag: ", flag)       
        break
    end
    sleep(0.2)
end

tcp:close()

方法二

local socket = require("socket.core")
local tcp = socket.tcp()
local host = '172.16.2.18'
local port = '8000'
local clicon = tcp:connect(host,port)
if(clicon) then
    print('connect '..host..' ok!')
else
    print('connect error')
end

var1 = "Test_paizhao"
flag = "0"

tcp:settimeout(4)

while (true) do
    if status =="closed" then
        print("server is closed!")
        break 
    end
    if str1~=nil then
        str0=string.sub(str1,1,1)
    end
    
    if str0~="#" then               
        tcp:send(var1)
        sleep(1)
        s, status, str1 = tcp:receive()
        print(str1)
    else 
        flag=string.sub(str1,2,2)  
        print("str1: ", str1)
        print("flag: ", flag)
        break
    end
    sleep(0.2)
end

tcp:close()

报错:“attempt to compare number with string”

使用 Lua 脚本和海康 VisionMaster 进行 TCP 通信-LMLPHP
出错代码:

rz=string.sub(str2,var3,var2)      
rz=string.format ("%.4f",rz)
print("rz: ", rz)
if((rz >= -180) and (rz <= -90))
then
    rz0 = rz+90
elseif( (rz <= 180) and (rz >= 90))
then
    rz0 = rz-90
else 
    rz0 = rz
end
print("rz0: ", rz0)

原因:需要显式地调用Lua的"tonumber()"内建函数rz 转化为number类型。
修改后如下:

rz=string.sub(str2,var3,var2)      
rz=string.format ("%.4f",rz)
print("rz: ", rz)
rz=tonumber(rz)
if((rz >= -180) and (rz <= -90))
then
    rz0 = rz+90
elseif( (rz <= 180) and (rz >= 90))
then
    rz0 = rz-90
else 
    rz0 = rz
end
print("rz0: ", rz0)
12-06 10:44