工程提交
This commit is contained in:
14
4G/code/core/cmd.lua
Normal file
14
4G/code/core/cmd.lua
Normal file
@@ -0,0 +1,14 @@
|
||||
-- CMD
|
||||
require "linksocket"
|
||||
require "socket"
|
||||
require "sys"
|
||||
|
||||
function handleCmd01(subCmd, payload)
|
||||
--sendToSocket(subCmd, payload) --向socket发送数据
|
||||
sendToSocket(subCmd, payload)
|
||||
log.info("CMD 0x01 received, sub:", subCmd, "data len:", #payload)
|
||||
end
|
||||
|
||||
function handleCmd02(subCmd, payload)
|
||||
sys.restart()
|
||||
end
|
||||
83
4G/code/core/linksocket.lua
Normal file
83
4G/code/core/linksocket.lua
Normal file
@@ -0,0 +1,83 @@
|
||||
require "socket"
|
||||
require "log"
|
||||
|
||||
-- 目标服务器IP和端口
|
||||
local ip, port = "121.43.69.62", "8767"
|
||||
-- 连接数量
|
||||
local clientCount = 1
|
||||
-- 存储所有客户端
|
||||
local clients = {}
|
||||
|
||||
-- 初始化多个socket连接
|
||||
for i = 1, clientCount do
|
||||
local client = {
|
||||
id = i,
|
||||
socket = nil,
|
||||
connected = false
|
||||
}
|
||||
|
||||
-- 为每个socket创建独立的连接任务
|
||||
sys.taskInit(function()
|
||||
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)
|
||||
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
|
||||
|
||||
client.connected = false
|
||||
client.socket:close()
|
||||
log.error("Client " .. client.id .. ": Disconnected")
|
||||
-- 发送断开连接信息到串口
|
||||
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)
|
||||
-- 遍历所有客户端,找到对应的连接
|
||||
for i, client in ipairs(clients) do
|
||||
if client.socket and client.socket.id == id then
|
||||
-- 转发到串口,添加socket标识,并清理数据
|
||||
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)
|
||||
|
||||
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
|
||||
65
4G/code/core/main.lua
Normal file
65
4G/code/core/main.lua
Normal file
@@ -0,0 +1,65 @@
|
||||
--- testSocket
|
||||
-- @module asyncSocket
|
||||
-- @author AIRM2M
|
||||
-- @license MIT
|
||||
-- @copyright openLuat.com
|
||||
-- @release 2018.10.27
|
||||
|
||||
PROJECT = "4G_NETWORK"
|
||||
VERSION = "1.0.0"
|
||||
|
||||
require "sys"
|
||||
require "net"
|
||||
require "log"
|
||||
require "cmd"
|
||||
require "uart"
|
||||
require "linksocket"
|
||||
|
||||
-- 初始化 UART1
|
||||
uart.setup(1, 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)
|
||||
|
||||
local uartID = 1
|
||||
|
||||
|
||||
local function toHexcode(str)
|
||||
local hexcode = ""
|
||||
for i = 1, #str do
|
||||
hexcode = hexcode .. string.format("%02X", str:byte(i))
|
||||
end
|
||||
return hexcode
|
||||
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")
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
net.startQueryAll(8 * 1000, 60 * 1000)
|
||||
|
||||
ril.request("AT+RNDISCALL=0,1")
|
||||
|
||||
|
||||
sys.init(0, 0)
|
||||
sys.run()
|
||||
Reference in New Issue
Block a user