fix:删除4G模组tools目录

This commit is contained in:
2026-04-30 17:16:01 +08:00
parent 36fb7fd027
commit 0d7a8564e0
36 changed files with 703 additions and 3205 deletions

View File

@@ -1,14 +1,115 @@
-- CMD
require "linksocket"
require "socket"
require "sys"
-- cmd.lua
-- 功能:解析串口帧,调用 linksocket 接口,执行系统命令
function handleCmd01(subCmd, payload)
--sendToSocket(subCmd, payload) --向socket发送数据
sendToSocket(subCmd, payload)
log.info("CMD 0x01 received, sub:", subCmd, "data len:", #payload)
local log = require "log"
local sys = require "sys"
local uart = require "uart"
local linksocket = require "linksocket"
local uartID = 1
-- 辅助函数:字节转十六进制(调试用)
local function toHexcode(str)
if not str then return "" end
local hex = ""
for i = 1, #str do
hex = hex .. string.format("%02X", str:byte(i))
end
return hex
end
function handleCmd02(subCmd, payload)
-- 发送通用响应帧55 AA 83 subCmd socketId status AA 55
local function sendResponse(subCmd, socketId, status)
local frame = string.char(0x55, 0xAA, 0x83, subCmd, socketId, status, 0xAA, 0x55)
uart.write(uartID, frame)
log.debug("Response: subCmd=", subCmd, "socketId=", socketId, "status=", status)
end
-- 命令处理函数
local function handleCmd01(subCmd, payload)
local socketId = subCmd
if socketId < 1 or socketId > 6 then
log.error("Invalid socket id in cmd 0x01:", socketId)
sendResponse(0x01, socketId, 1)
return
end
local success = linksocket.sendToSocket(socketId, payload)
if not success then
log.warn("Send failed, socket", socketId, "not connected")
sendResponse(0x01, socketId, 1)
else
log.info("Data sent to socket", socketId, "len=", #payload)
end
end
local function handleCmd02()
log.info("System restart command received")
sys.restart()
end
end
local function handleCmd03(subCmd, payload)
if #payload < 1 then
log.error("Cmd 0x03 missing socket id")
return
end
local socketId = payload:byte(1)
if socketId < 1 or socketId > 6 then
log.error("Invalid socket id in cmd 0x03:", socketId)
sendResponse(subCmd, socketId, 1)
return
end
if subCmd == 0x01 then
local ok = linksocket.setKeepConnecting(socketId, true)
if ok then
sendResponse(subCmd, socketId, 3)
else
sendResponse(subCmd, socketId, 1)
end
elseif subCmd == 0x02 then
local ok = linksocket.setKeepConnecting(socketId, false)
if ok then
sendResponse(subCmd, socketId, 0)
else
sendResponse(subCmd, socketId, 1)
end
else
log.error("Unknown subCmd for 0x03:", subCmd)
sendResponse(subCmd, socketId, 1)
end
end
-- 对外接口:处理串口接收到的原始数据
local function process(rawData)
if not rawData or #rawData < 5 then
log.warn("Short data received, ignore")
return
end
log.info("CMD raw data:", toHexcode(rawData))
if rawData:byte(1) == 0x55 and rawData:byte(2) == 0xAA
and rawData:byte(-2) == 0xAA and rawData:byte(-1) == 0x55 then
local mainCmd = rawData:byte(3)
local subCmd = rawData:byte(4)
local payload = rawData:sub(5, -3)
if mainCmd == 0x01 then
handleCmd01(subCmd, payload)
elseif mainCmd == 0x02 then
handleCmd02()
elseif mainCmd == 0x03 then
handleCmd03(subCmd, payload)
else
log.warn("Unknown main command:", mainCmd)
end
else
log.warn("Invalid frame header/footer")
end
end
-- 模块接口
local M = {
process = process,
}
return M

View File

@@ -1,83 +1,160 @@
require "socket"
require "log"
-- linksocket.lua
-- 功能:管理多个 TCP Socket 连接,支持手动连接/断开,自动重连,数据收发
-- 目标服务器IP和端口
local ip, port = "121.43.69.62", "8767"
-- 连接数量
local clientCount = 1
-- 存储所有客户端
local log = require "log"
local sys = require "sys"
local socket = require "socket"
-- 服务器配置
local SERVER_IP = "121.43.69.62"
local SERVER_PORT = 8767
local MAX_CLIENTS = 6
-- 客户端状态表
local clients = {}
local keepConnecting = {}
local recvCallback = nil
-- 初始化多个socket连接
for i = 1, clientCount do
local client = {
-- 初始化所有socket槽位
for i = 1, MAX_CLIENTS do
clients[i] = {
id = i,
socket = nil,
connected = false
}
keepConnecting[i] = false
end
-- 为每个socket创建独立的连接任务
-- 发送连接状态响应
local function sendConnStatus(subCmd, socketId, status)
local uart = require "uart"
local frame = string.char(0x55, 0xAA, 0x83, subCmd, socketId, status, 0xAA, 0x55)
uart.write(1, frame)
log.debug("Socket", socketId, "status response, subCmd=", subCmd, "status=", status)
end
-- 设置是否保持连接
local function setKeepConnecting(id, keep)
if id < 1 or id > MAX_CLIENTS then
return false
end
keepConnecting[id] = keep
if not keep and clients[id].connected and clients[id].socket then
clients[id].socket:close()
clients[id].socket = nil
clients[id].connected = false
sendConnStatus(0x02, id, 0)
end
return true
end
-- 发送数据到指定socket
local function sendToSocket(id, data)
if id < 1 or id > MAX_CLIENTS then
log.error("sendToSocket: invalid id", id)
return false
end
if clients[id].connected and clients[id].socket then
clients[id].socket:asyncSend(data)
log.info("Send to socket", id, "len=", #data)
return true
else
log.warn("Socket", id, "not connected, cannot send")
return false
end
end
-- 注册数据接收回调
local function setRecvCallback(cb)
recvCallback = cb
end
-- 为每个socket创建常驻任务
for id = 1, MAX_CLIENTS do
sys.taskInit(function()
local socketId = id
log.debug("Socket", socketId, "control task started")
while true do
while not socket.isReady() do sys.wait(1000) end
client.socket = socket.tcp()
log.debug("Client " .. client.id .. ": Connecting to " .. ip .. ":" .. port)
while not client.socket:connect(ip, port) do
log.warn("Client " .. client.id .. ": Connection failed, retrying...")
sys.wait(2000)
while not keepConnecting[socketId] do
sys.wait(1000)
end
client.connected = true
log.debug("Client " .. client.id .. ": Connected successfully")
-- 发送连接成功信息到串口
uart.write(1, "Socket " .. client.id .. " connected\r\n")
-- 保持连接并处理数据
while client.socket:asyncSelect(60, "ping") do end
while keepConnecting[socketId] do
while not socket.isReady() do
if not keepConnecting[socketId] then break end
sys.wait(1000)
end
if not keepConnecting[socketId] then break end
client.connected = false
client.socket:close()
log.error("Client " .. client.id .. ": Disconnected")
-- 发送断开连接信息到串口
local sock = socket.tcp()
log.debug("Socket", socketId, "connecting to", SERVER_IP, SERVER_PORT)
local connected = false
while not connected and keepConnecting[socketId] do
connected = sock:connect(SERVER_IP, SERVER_PORT)
if not connected then
log.warn("Socket", socketId, "connect failed, retry in 3s")
sys.wait(3000)
end
end
if not keepConnecting[socketId] then
if sock then sock:close() end
break
end
clients[socketId].socket = sock
clients[socketId].connected = true
log.info("Socket", socketId, "connected")
sendConnStatus(0x01, socketId, 0)
while keepConnecting[socketId] and clients[socketId].connected do
local ok = sock:asyncSelect(60, "recv")
if not ok then
log.warn("Socket", socketId, "connection lost")
break
end
sys.wait(100)
end
if clients[socketId].socket then
clients[socketId].socket:close()
clients[socketId].socket = nil
end
clients[socketId].connected = false
if not keepConnecting[socketId] then
sendConnStatus(0x02, socketId, 0)
break
else
log.info("Socket", socketId, "will reconnect after 3s")
sys.wait(3000)
end
end
end
end)
-- 存储客户端
clients[i] = client
end
local function toHexcode(str)
local hexcode = ""
for i = 1, #str do
hexcode = hexcode .. string.format("%02X", str:byte(i))
end
return hexcode
end
-- 测试代码,异步回调接收数据
sys.subscribe("SOCKET_RECV", function(id)
-- 遍历所有客户端,找到对应的连接
-- 订阅底层Socket接收事件
sys.subscribe("SOCKET_RECV", function(socketId)
for i, client in ipairs(clients) do
if client.socket and client.socket.id == id then
-- 转发到串口添加socket标识并清理数据
if client.socket and client.socket.id == socketId and client.connected then
local data = client.socket:asyncRecv()
log.info("Client " .. client.id .. ": Received data: " .. toHexcode(data))
-- 转发到串口添加socket标识
local frame = string.char(0x55) .. string.char(0xAA) .. string.char(0x01) .. string.char(i) .. string.char(#data) .. data .. string.char(0xAA) .. string.char(0x55)
uart.write(1, frame)
if data and #data > 0 then
log.info("Socket", client.id, "received data len=", #data)
if recvCallback then
recvCallback(client.id, data)
end
end
break
end
end
end)
-- 发送数据到指定socket
function sendToSocket(id_byte, msg)
if clients[id_byte] and clients[id_byte].socket and clients[id_byte].connected then
clients[id_byte].socket:asyncSend(msg)
else
log.error("Socket " .. id_byte .. " not connected or does not exist")
end
end
-- 模块接口
local M = {
setKeepConnecting = setKeepConnecting,
sendToSocket = sendToSocket,
setRecvCallback = setRecvCallback,
}
return M

View File

@@ -1,9 +1,5 @@
--- testSocket
-- @module asyncSocket
-- @author AIRM2M
-- @license MIT
-- @copyright openLuat.com
-- @release 2018.10.27
-- main.lua
-- 功能:初始化硬件、网络、串口,注册回调,启动系统
PROJECT = "4G_NETWORK"
VERSION = "1.0.0"
@@ -11,55 +7,42 @@ VERSION = "1.0.0"
require "sys"
require "net"
require "log"
require "cmd"
require "uart"
require "linksocket"
require "netLed"
require "pmd"
-- 初始化 UART1
uart.setup(1, 115200, 8, uart.PAR_NONE, uart.STOP_1)
-- 初始化UART1
local uartID = 1
uart.setup(uartID, 115200, 8, uart.PAR_NONE, uart.STOP_1)
-- 启动网络指示灯功能模块
require "netLed"
pmd.ldoset(2,pmd.LDO_VLCD)
netLed.setup(true,pio.P0_1,pio.P0_4)
pmd.ldoset(2, pmd.LDO_VLCD)
netLed.setup(true, pio.P0_1, pio.P0_4)
local uartID = 1
-- 初始化网络
net.startQueryAll(8 * 1000, 60 * 1000)
ril.request("AT+RNDISCALL=0,1")
-- 加载自定义模块(必须放在网络初始化之后)
local cmd = require "cmd"
local linksocket = require "linksocket"
local function toHexcode(str)
local hexcode = ""
for i = 1, #str do
hexcode = hexcode .. string.format("%02X", str:byte(i))
end
return hexcode
end
-- 注册Socket数据接收回调
linksocket.setRecvCallback(function(socketId, data)
-- 打包帧格式55 AA 01 id len data AA 55
local frame = string.char(0x55, 0xAA, 0x01, socketId, #data) .. data .. string.char(0xAA, 0x55)
uart.write(uartID, frame)
log.info("Forward data from socket", socketId, "len=", #data)
end)
-- 串口数据接收处理
uart.on(uartID, "receive", function()
local data = uart.read(uartID, 300)
if data and type(data) == "string" and #data > 0 then
log.info("UART received:", toHexcode(data))
if data:byte(1) == 0x55 and data:byte(2) == 0xAA and data:byte(-2) == 0xAA and data:byte(-1) == 0x55 then
local Main_Cmd = data:byte(3)
local Sub_Cmd = data:byte(4)
local payload = data:sub(5, -3)
if Main_Cmd ==0x01 then
handleCmd01(Sub_Cmd, payload)
end
if Main_Cmd == 0x02 then
handleCmd02(Sub_Cmd, payload)
end
end
else
log.warn("UART receive callback triggered but no valid data")
if data and #data > 0 then
cmd.process(data)
end
end)
net.startQueryAll(8 * 1000, 60 * 1000)
ril.request("AT+RNDISCALL=0,1")
-- 启动系统
sys.init(0, 0)
sys.run()