log.txt 日志如上,开始工作正常,约1个小时出现发送数据不成功现象
以下是socket通信代码,基本是官方DEMO
--启动socket客户端任务
sys.taskInit(
function()
local retryConnectCnt = 0
while true do
if not socket.isReady() then
retryConnectCnt = 0
--等待网络环境准备就绪,超时时间是5分钟
sys.waitUntil("IP_READY_IND",300000)
end
if socket.isReady() then
--创建一个socket tcp客户端
local socketClient = socket.tcp()
--阻塞执行socket connect动作,直至成功
if socketClient:connect( ServerIpAddress, ServerPort ) then
retryConnectCnt = 0
ready = true
log.info( "testUartTask.taskInit connect serversucc" )
socketOutMsg.init( )
--循环处理接收和发送的数据
while true do
if not socketInMsg.proc( socketClient ) then log.error("socketTask.socketInMsg.NetWorkDisconnect") break end
if not socketOutMsg.proc( socketClient ) then log.error("socketTask.socketOutMsg NetWorkDisconnect") break end
end
socketOutMsg.unInit()
ready = false
else
log.info( "testUartTask.taskInit connect serverfail" )
retryConnectCnt = retryConnectCnt+1
end
--断开socket连接
socketClient:close()
if retryConnectCnt>=5 then link.shut() retryConnectCnt=0 end
sys.wait(5000)
else
--进入飞行模式,20秒之后,退出飞行模式
net.switchFly(true)
sys.wait(20000)
net.switchFly(false)
end
end
end
)
--- socket客户端数据接收处理
-- @param socketClient,socket客户端对象
-- @return 处理成功返回true,处理出错返回false
-- @usage socketInMsg.proc(socketClient)
function proc(socketClient)
local Result, Data
SocketIndex = 0
while true do
Result, Data = socketClient:recv(2000)
--log.info( "socketInMsg.proc.RecvData", Data )
--接收到数据
if Result then
while true do
if Data~="" and string.len(Data)>=34 then
local Tail = string.find( Data, "ff55" )
if Tail then
SocketMsgQuene[SocketIndex] = string.sub( Data, Tail, Tail+33 )
SocketIndex = SocketIndex + 1
Data = string.sub( Data, Tail+34, -1 )
else
log.info( "socketInMsg.proc", "not found tail" )
break
end
else
--log.info( "socketInMsg.proc.SocketIndex", SocketIndex )
break
end
end
if SocketIndex ~= 0 then
for i=0, SocketIndex-1 do
log.info( "socketInMsg.proc", SocketMsgQuene[i] )
testUartTask.SendToMcu( SocketMsgQuene[i] )
--sys.wait(200)
end
end
else
break
end
end
return Result or Data=="timeout"
end
--- socket客户端数据发送处理
-- @param socketClient,socket客户端对象
-- @return 处理成功返回true,处理出错返回false
-- @usage socketOutMsg.proc(socketClient)
function proc(socketClient)
while #SendMsgQuene>0 do
local outMsg = table.remove(SendMsgQuene,1)
local result = socketClient:send(outMsg)
if not result then
NetWorkState = false
return false
else
NetWorkState = true
end
end
return true
end