我想用串口做简单的收发数据。发数据可以了但是收数据不行,收数据想单独一个函数来处理逻辑。

这是收发线程和http在线程在一起的,http在网络上的找json  然后串口还给单片机

module(...,package.seeall)

require"ril"

require"pins"

require"sys"

local a=0;

local uart_id=0;

local console_task

log.info("App_Task_YYJ")


local function Uart_Recive()


  while true do

   print("I find I")

   --

  end

end


function Uart_TimerTest()

    uart_id =  1

    baudrate = 115200

    -- 创建console处理的协程

console_task = coroutine.create(Uart_Recive)

    uart.setup(uart_id, baudrate, 8, uart.PAR_NONE, uart.STOP_1)

    -- 串口收到数据时唤醒console协程

    uart.on(uart_id, "receive", function()

        coroutine.resume(console_task)

    end)

    coroutine.resume(console_task)


    while true do

a=a+1

print("Uart_TimerTest.",a)

uart.write(uart_id,"123456")

sys.wait(1000);

end

end



function Http_TimerTest()

    while true do

a=a+1

print("Http_TimerTest.",a)

sys.wait(1000);

end

end

sys.taskInit(Uart_TimerTest)

sys.taskInit(Http_TimerTest)

--sys.timerLoopStart(Uart_TimerTest, 3000)

--sys.timerLoopStart(Http_TimerTest, 3000)

--建立两个线程

--一个去守护串口

--一个去守护http连接




--开启一个循环定时器

--sys.timerLoopStart(loopTimerTest, 3000)

--如果关闭定时器,可以使用如下代码:

--sys.timerStop(timerTest)

  1. --sys.timerStop(loopTimerTest)
打印日志如下
19-07-23 01:05:11.101]: /lua/taskapp.lua:14: in function </lua/taskapp.lua:10>
[2019-07-23 01:05:11.106]: [E]-[errDump.appendErr] cannot resume running coroutine
程序运行错误,请根据上方提示,找到对应lua文件修改程序
[2019-07-23 01:05:11.110]: stack traceback:
[2019-07-23 01:05:11.118]: /lua/patch.lua:65: in function </lua/patch.lua:63>
[2019-07-23 01:05:11.120]: (tail call): ?


请先 登录 后评论

1 个回答

晨旭 - 菜鸟

coroutine使用错误,如果不熟练推荐直接用task框架里的任务+消息收发来实现

请先 登录 后评论
  • 0 关注
  • 0 收藏,1911 浏览
  • 尹义军 提出于 2019-07-23 01:06

相似问题