Lua programming - serial communication tutorial for secondary development of air724

    With the basis of the previous chapter " lua programming - air724 secondary development of LED lighting tutorial ", the next step is to introduce how to use lua language for serial communication.

1. Hardware connection

According to the introduction of Yinerda hardware information, as shown below:

b69cbebfdd2242efaa027fecc565c3bd.png

Directly connect the usb to ttl tool with the 4G module, and the receiving and sending lines need to be cross-connected.

2. Programming

Use vscode software for development, the APIs related to the uart serial port of the lua language library are as follows:

61442e9804a3ac934015c29d5a03f84c.png

The function function is briefly introduced as follows:

uart.on() means: register the processing function of the serial port event

uart.set(): configure the serial port

uart.write(): write string or integer data to the serial port

uart.read(): read a string from the serial port

The entire directory structure is as follows: (there are main.lua and test.Uart.lua files under the folder uart)

0753358a0caa9f41b15d00bf84038b24.png

The test code is as follows:

The testUart.lua code is as follows (test only):

--- 模块功能:串口1功能测试


module(...,package.seeall)


require"utils"
require"pm"


--串口ID,1对应uart1
--如果要修改为uart2,把UART_ID赋值为2即可
local UART_ID = 1


--[[
函数名:read
功能  :读取串口接收到的数据
参数  :无
返回值:无
]]
local function read()
    local data = ""
    --底层core中,串口收到数据时:
    --如果接收缓冲区为空,则会以中断方式通知Lua脚本收到了新数据;
    --如果接收缓冲器不为空,则不会通知Lua脚本
    --所以Lua脚本中收到中断读串口数据时,每次都要把接收缓冲区中的数据全部读出,这样才能保证底层core中的新数据中断上来,此read函数中的while语句中就保证了这一点
    while true do        
        data = uart.read(UART_ID,"*l")
        if not data or string.len(data) == 0 then break end
        write(data);         
    end
end


--[[
函数名:write
功能  :通过串口发送数据
参数  : s:要发送的数据
返回值:无
]]
function write(s)
    log.info("testUart.write",s)
    uart.write(UART_ID,s.."\r\n")
end


local function writeOk()
    log.info("testUart.writeOk")
end


--注册串口的数据接收函数,串口收到数据后,会以中断方式,调用read接口读取数据
uart.on(UART_ID,"receive",read)
--注册串口的数据发送通知函数
uart.on(UART_ID,"sent",writeOk)


--配置并且打开串口
uart.setup(UART_ID,115200,8,uart.PAR_NONE,uart.STOP_1)
--如果需要打开“串口发送数据完成后,通过异步消息通知”的功能,则使用下面的这行setup,注释掉上面的一行setup
--uart.setup(UART_ID,115200,8,uart.PAR_NONE,uart.STOP_1,nil,1)

The code of the main.lua file is as follows:

--必须在这个位置定义PROJECT和VERSION变量
--PROJECT:ascii string类型,可以随便定义,只要不使用,就行
--VERSION:ascii string类型,如果使用Luat物联云平台固件升级的功能,必须按照"X.X.X"定义,X表示1位数字;否则可随便定义
PROJECT = "UART"
VERSION = "2.0.0"


--加载日志功能模块,并且设置日志输出等级
--如果关闭调用log模块接口输出的日志,等级设置为log.LOG_SILENT即可
require "log"
LOG_LEVEL = log.LOGLEVEL_TRACE
--[[
如果使用UART输出日志,打开这行注释的代码"--log.openTrace(true,1,115200)"即可,根据自己的需求修改此接口的参数
如果要彻底关闭脚本中的输出日志(包括调用log模块接口和Lua标准print接口输出的日志),执行log.openTrace(false,第二个参数跟调用openTrace接口打开日志的第二个参数相同),例如:
1、没有调用过sys.opntrace配置日志输出端口或者最后一次是调用log.openTrace(true,nil,921600)配置日志输出端口,此时要关闭输出日志,直接调用log.openTrace(false)即可
2、最后一次是调用log.openTrace(true,1,115200)配置日志输出端口,此时要关闭输出日志,直接调用log.openTrace(false,1)即可
]]
--log.openTrace(true,1,115200)


require "sys"


require "net"
--每1分钟查询一次GSM信号强度
--每1分钟查询一次基站信息
net.startQueryAll(60000, 60000)


--此处关闭RNDIS网卡功能
--否则,模块通过USB连接电脑后,会在电脑的网络适配器中枚举一个RNDIS网卡,电脑默认使用此网卡上网,导致模块使用的sim卡流量流失
--如果项目中需要打开此功能,把ril.request("AT+RNDISCALL=0,1")修改为ril.request("AT+RNDISCALL=1,1")即可
--注意:core固件:V0030以及之后的版本、V3028以及之后的版本,才以稳定地支持此功能
ril.request("AT+RNDISCALL=0,1")


--加载控制台调试功能模块(此处代码配置的是uart2,波特率115200)
--此功能模块不是必须的,根据项目需求决定是否加载
--使用时注意:控制台使用的uart不要和其他功能使用的uart冲突
--使用说明参考demo/console下的《console功能使用说明.docx》
--require "console"
--console.setup(2, 115200)


--加载网络指示灯和LTE指示灯功能模块
--根据自己的项目需求和硬件配置决定:1、是否加载此功能模块;2、配置指示灯引脚
--合宙官方出售的Air720U开发板上的网络指示灯引脚为pio.P0_1,LTE指示灯引脚为pio.P0_4
require "netLed"
pmd.ldoset(2,pmd.LDO_VLCD)
netLed.setup(true,pio.P0_1,pio.P0_4)
--网络指示灯功能模块中,默认配置了各种工作状态下指示灯的闪烁规律,参考netLed.lua中ledBlinkTime配置的默认值
--如果默认值满足不了需求,此处调用netLed.updateBlinkTime去配置闪烁时长
--LTE指示灯功能模块中,配置的是注册上4G网络,灯就常亮,其余任何状态灯都会熄灭


--加载错误日志管理功能模块【强烈建议打开此功能】
--如下2行代码,只是简单的演示如何使用errDump功能,详情参考errDump的api
require "errDump"
errDump.request("udp://dev_msg1.openluat.com:12425", nil, true)


--加载远程升级功能模块【强烈建议打开此功能,如果使用了阿里云的OTA功能,可以不打开此功能】
--如下3行代码,只是简单的演示如何使用update功能,详情参考update的api以及demo/update
--PRODUCT_KEY = "v32xEAKsGTIEQxtqgwCldp5aPlcnPs3K"
--require "update"
--update.request()


--加载串口功能测试模块
require "testUart"


--启动系统框架
sys.init(0, 0)
sys.run()

3. Experimental results

Download the entire script file into the module, as follows:

85f317a623efc6541630f594fe36b3e5.png

Running effect: send the string "hello world, hi!!!" and return to normal.

6a90052409c09bca89b7bd5d54667ea5.png

Personal Official Account: Embedded Learning and Practice

reference:

http://wikitest.openluat.com/luat/%E7%A4%BA%E4%BE%8B/LuatOS-Air%E7%A4%BA%E4%BE%8B/uart%28%E4%B8%B2%E5%8F%A3%29.html
https://wiki.openluat.com/doc/luatApi/#uartsetup

 

Guess you like

Origin blog.csdn.net/weixin_46158019/article/details/131179767