tcp_test.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. local libnet = require "libnet"
  2. -- 下面演示用阻塞方式做串口透传远程服务器,简单的串口DTU
  3. local d1Online = false
  4. local com_buff = zbuff.create(4096)
  5. local d1Name = "D1_TASK"
  6. local function netCB(msg)
  7. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  8. end
  9. local filename = "/spectrum.json"
  10. local function dtuTask(uart_id, ip, port)
  11. sys.wait(5000)
  12. d1Online = false
  13. local tx_buff = zbuff.create(1024)
  14. local rx_buff = zbuff.create(1024)
  15. local netc
  16. local result, param, is_err
  17. -- result = uart.setup(uart_id, 115200, 8, 1)
  18. -- uart.on(uart_id, "receive", function(id, len)
  19. -- uart.rx(id, com_buff)
  20. -- if d1Online then -- 如果已经在线了,则发送socket.EVENT消息来打断任务里的阻塞等待状态,让任务循环继续
  21. -- sysplus.sendMsg(d1Name, socket.EVENT, 0)
  22. -- end
  23. -- end)
  24. netc = socket.create(nil, d1Name)
  25. socket.debug(netc, true)
  26. socket.config(netc, nil, nil, nil)
  27. -- socket.config(netc, nil, true)
  28. while true do
  29. log.info(rtos.meminfo("sys"))
  30. result = libnet.waitLink(d1Name, 0, netc)
  31. result = libnet.connect(d1Name, 15000, netc, ip, port)
  32. -- result = libnet.connect(d1Name, 5000, netc, "112.125.89.8",34607)
  33. d1Online = result
  34. if result then
  35. log.info("服务器连上了")
  36. libnet.tx(d1Name, 0, netc, "helloworld")
  37. if not io.exists(filename) then
  38. log.error("文件", "频谱文件不存在: " .. filename)
  39. return nil
  40. end
  41. local file = io.open(filename, "r")
  42. if not file then
  43. log.error("文件", "无法打开文件: " .. filename)
  44. return nil
  45. end
  46. local content = file:read("*a")
  47. file:close()
  48. if not content or #content == 0 then
  49. log.error("文件", "文件内容为空")
  50. return nil
  51. end
  52. log.info("文件", string.format("读取了 %d 字节", #content))
  53. rx_buff:del()
  54. libnet.tx(d1Name, 0, netc, content)
  55. end
  56. while result do
  57. succ, param, _, _ = socket.rx(netc, rx_buff)
  58. if not succ then
  59. log.info("服务器断开了", succ, param, ip, port)
  60. break
  61. end
  62. if rx_buff:used() > 0 then
  63. log.info("收到服务器数据,长度", rx_buff:used())
  64. -- uart.tx(uart_id, rx_buff)
  65. end
  66. tx_buff:copy(nil, com_buff)
  67. com_buff:del()
  68. if tx_buff:used() > 0 then
  69. result, param = libnet.tx(d1Name, 15000, netc, tx_buff)
  70. end
  71. if not result then
  72. log.info("发送失败了", result, param)
  73. break
  74. end
  75. tx_buff:del()
  76. if com_buff:len() > 4096 then
  77. com_buff:resize(4096)
  78. end
  79. if tx_buff:len() > 1024 then
  80. tx_buff:resize(1024)
  81. end
  82. if rx_buff:len() > 1024 then
  83. rx_buff:resize(1024)
  84. end
  85. log.info(rtos.meminfo("sys"))
  86. -- 阻塞等待新的消息到来,比如服务器下发,串口接收到数据
  87. result, param = libnet.wait(d1Name, 15000, netc)
  88. if not result then
  89. log.info("服务器断开了", result, param)
  90. break
  91. end
  92. end
  93. d1Online = false
  94. libnet.close(d1Name, 5000, netc)
  95. log.info(rtos.meminfo("sys"))
  96. sys.wait(1000)
  97. end
  98. end
  99. function dtuDemo(uart_id, ip, port)
  100. sysplus.taskInitEx(dtuTask, d1Name, netCB, uart_id, ip, port)
  101. end