本文档适合大彩pm 4g系列串口屏产品使用。
1. visualtft软件版本:v3.0.1.1112及以上的版本。
版本查看:
1) 打开visualtft软件启动页面如图2-1软件版本,右上角会显示的软件版本号;
图2-1软件版本
2) 打开visualtft,在软件右下角可以查看软件版本图2-2软件版本,最新版本可登录http://www.gz-dc.com/进行下载。
图2-2软件版本
2. 串口屏硬件版本:m系列固件 >= v6.3.257.00。
版本查看:
1) 查看屏幕背面版本号贴纸;
2) visualtft与屏幕联机成功后,右下角显示的版本号。
例程中,介绍4g tcp单连接的应用。屏幕作为客户端,和服务器进行数据收发。
服务器tcp测试工具:本例程中使用合宙提供的tcp 透传云 做测试验证。该测试平台ip固定为180.97.81.180,demo中只需要更改端口号,在连接即可测试验证。
注意:udp或多链接模式可参考《luat 4g模块at命令手册v4.2.2.pdf》即可
该平台创建服务器必须在3分钟内连上,且长时间未交互,服务器会自动关闭。
1 《lua 脚本api v1.4》可通过以下链接下载物联型开发包获取:
http:/www.gz-dc.com/index.php?s=/list/index/cid/19.html
2 《lua基础学习》可通过以下链接下载物联型开发包获取:
http:/www.gz-dc.com/index.php?s=/list/index/cid/19.html
3 lua脚本初学者可以通过下面链接进行学习。
http://www.runoob.com/lua/lua-arrays.html
4 at指令,可以通过下面子连接了解。
http://www.openluat.com/product/file/asr1802/luat 4g模块at命令手册v4.2.8.pdf
5 合宙tcp测试平台。
http://tcplab.openluat.com
本文主要将以下2点进行说明:
1. 准备工程素材;
2. 配置串口屏工程;
在实现例程前需要作以下3个准备:
1. 硬件平台;
2. 软件平台;
3. ui素材;
该例程使用大彩m系列7寸串口屏dc80480m070_1111_0t为验证开发平台。如图5-1所示;
图5-1 m系列7寸串口屏
其他尺寸串口屏均可借鉴此教程。
5.1.2 软件平台
使用大彩自主研发的上位机软件visualtft配置工程,登录http://www.gz-dc.com/下载。如图5-2所示;
图5-2 下载软件
本文主要介绍以下2点:
(1) 画面配置
(2) lua编辑
5.2.1. 画面配置
在画面id0中,客户端、服务端和信号值/运营商3部分组成。
客户端:
- 文本控件id1:屏幕公网的ip
- 文本控件2~3:分别服务器ip地址、端口号。
- 按钮控件id4:当输入端口号后,点击连接。屏幕向该服务器发起请求信息。
服务器:
- 文本控件id7~9:当服务器下发数据时,将相关信息显示此处,依次为服务器的ip、端口、数据等
4g信号和运营商:
- 图标控件控件id10作为信号显示、文本控件id11用于显示运营商。
画面配置如图5-3所示:
注意:其他非关键控件不在一一介绍,下文不在累述
图5-3 画面配置
5.2.2. lua编辑
本例程中,屏幕上电执行初始化操作,如加载4g at 指令的库、初始化4g模块、定时获取运营商和信号值等。
当用户点击图片下载的时,调用air_http_download()开始下载文件。在下载回调函数on_http_download_file_cb()里进行数据存储,显示下载信息等。若该文件单次下载不完,会多次回调on_http_download_file_cb()函数,直至下载完毕,如图5-4所示。
图5-4 tcp连接
1. 初始化
调用系统函数on_init()执行代码如程序清单 1所示:
程序清单 1初始化
--[[********************************************************************* ** function name: on_init ** descriptions : 系统初始化时,执行此回调函数。 *********************************************************************--]] function on_init() dofile('air724at.lua') --加载 http.lua 文件 uart_set_baudrate3(115200) --设置与4g模块通讯的串口3的波特率为115200 --设置4g库函数的命令发送函数,命令回调函数、调试信息打印函数 air_set_callback(on_air_send_cb,on_air_resp_callback,on_air_log_cb) air_hw_int() --4g模块初始化设置 --开启自动获取型号强度、时间、天气功能 start_timer(timerid_sig_weather, 1000 , 0, 0) end --[[********************************************************************* ** function name: on_timer ** descriptions : 定时器超时回到调函数。 ** @ timer_id : 定时器id *********************************************************************--]] function on_timer(timer_id) on_air_timer(timer_id) --4g库函数的定时处理 --定时获取信号强度 if timer_id == timerid_sig_weather then --定时器计数,timer0_notify_cnt 每秒 1。 timer0_notify_cnt = timer0_notify_cnt 1 if timer0_notify_cnt == 0 then --每15s调用一次,更新信号值 at_cops_csq() timer0_notify_cnt = 0 --标志位清空 end end end --[[********************************************************************* ** function name : at_cops_csq ** descriptions : 获取运营商信息、信号强度 ** @return : nil,无返回值 *********************************************************************--]] function at_cops_csq() air_cmd_add('at cops?','ok',1000) –获取运营商 air_cmd_add('at csq' ,'ok',1000) –获取信号 end --[[********************************************************************* ** function name: on_air_resp_callback ** descriptions : 4g模块-数据回调接口 ** @key : 屏幕向4g模块的发送请求 ** @value : 4g模块返回的数据 *********************************************************************--]] function on_air_resp_callback(key, value) --*********************************************************************** --功能: 判断 key -- 如果 key 为空,则退出函数。 -- 因为 key 为空时,下方 string.find( key , ) 是不正确的使用。 -- 以下key的处理必须不为空, --******************************************************************** if key == nil then return end ...... --****************************************************************** --条件: 4g初始化完成 --功能: 使用使用at指令获取信号强度和运营商。 --调用函数:at_cops_csq() --函数功能:获取信号强度和运营商 --调用函数:at_tcp_client_cofig() --函数功能:tcp配置 ********************************************************************* if string.find(key,' sapbr=1,1') ~= nil and string.find(value,'ok') ~= nil then at_cops_csq() at_tcp_client_cofig() end --******************************************************************** --条件: 4g模块返回运营商信息 --功能: --******************************************************************** if string.find(key,' cops') ~= nil and string.find(value,' cops') ~= nil then --**************************************************************** --value : cops: 0,2,"46000",7 --要提取的值:46000 --正则表达式: ' cops:.*,.*,"(%d*)"' --**************************************************************** local regular_e = ' cops:.*,.*,"(%d*)"'--正则表达式 --获取的值赋给 my_mobile_mccmnc local my_mobile_mccmnc = string.match( value, regular_e ) set_text( screen_main, 2, mobile_mccmnc[my_mobile_mccmnc]) end --******************************************************************** --条件:4g模块返回信号强度信息 --功能: --******************************************************************** if string.find(key,' csq')~=nil and string.find(value,' csq')~=nil then --*************************************************************** --value : csq: 15,99 --要提取的值:15 --正则表达式: ' csq: (.*),.*' --**************************************************************** local regular_e = ' csq: (.*),.*' --正则表达式 local my_csq = tonumber(string.match(value,regular_e)) if my_csq<=11 then set_value( screen_main, 1, 1) --设置信号图标显示第1帧 elseif my_csq>=12 and my_csq<=13 then set_value(screen_main, 1, 2) --设置信号图标显示第2帧 elseif my_csq>=14 and my_csq<=15 then set_value( screen_main, 1, 3) --设置信号图标显示第3帧 elseif my_csq>=16 then set_value( screen_main, 1, 4) --设置信号图标显示第4帧 end end end --[[********************************************************************** ** function name: on_uart_recv_data3 ** descriptions : 接收串口3数据回调函数,连接4g模块。 **********************************************************************--]] function on_uart_recv_data3(packet) --4g at指令库api on_air_recv_data(packet) end
核心api函数
1) dofile (filename)
加载文件:本例程中加载4g at 指令的库
- filename:文件名
2) uart_set_baudrate3(speed)
设置串口3的波特率:串口3为屏幕和4g模块通讯的串口
- speed:通讯的波特率
3) on_air_recv_data(packet)
串口接收4g模块的返回数据的回调。
- packet:形参为表,字节数据
4) air_set_callback (on_air_send_cb,on_air_resp_callback,on_air_log_cb)
设置4g库里的回调函数。形参类型为函数,参数依次为命令发送函数,命令回调函数、调试信息打印函数,可自定义函数名。
- on_air_send_cb:屏幕向4g模块发送回调函数
- on_air_resp_callback:4g向屏幕返回数据回调函数
- on_air_log_cb:用户调试信息回调函数调试
5) air_hw_int()
4g at 指令的库函数,初始化4g模块
6) at_cops_csq()
自定义封装函数,获取运行商和信号值
7) air_cmd_add(sendstr,ackstr,timeout,retry,callback)
屏幕向4g模块发送at指令
- sendstr:屏幕向4g模块发送at指令
- ackstr:4g模块应答屏幕的请求
- timeou:应答超时
- retry: 超时重发次数,可选
- callback: 应答回调函数,可选
注:如果没有设置超时重发次数,则超时时直接发送队列中的下一条指令。
8) on_air_resp_callback(key, value)
4g应答屏幕回调函数:屏幕发送at指令,4g应答后均会回调该函数,初始化设置:air_set_callback(on_air_send_cb,on_air_resp_callback,on_air_log_cb)。
- key:屏幕向4g模块发送请求的at指令
- value:4g模块返回的数据
相关at指令:
本例程中,涉及到获取4g模块初始化、运营商、信号值等at交互指令回调的判断,在on_air_resp_callback(key, value)回调函数中,判断4g收发的相关at指令,如下所示:
1) 网络数据是否激活:
屏幕发送:at sapbr=1,1。air_hw_int()函数里发送。
屏幕接收:ok。on_air_resp_callback(key, value)函数里执行4g返回数据的判断。
2) 获取运营商:
屏幕发送:at cops?。在at_cops_csq()函数了发送。
屏幕接收:ok。on_air_resp_callback(key, value)函数里执行4g返回数据的判断。
3) 获取信号值:
屏幕发送:at csq。在at_cops_csq()函数了发送。
屏幕接收:ok。on_air_resp_callback(key, value)函数里执行4g返回数据的判断。
2. tcp配置
当4g初始化完毕后,在on_air_resp_callback(key, value)函数里调用at_tcp_client_cofig()进行tcp配置,代码如程序清单 2所示:
程序清单 2 tcp配置
--[[********************************************************************* ** function name: on_air_resp_callback ** descriptions : 4g模块-数据回调接口 ** @key : 屏幕向4g模块的发送请求 ** @value : 4g模块返回的数据 *********************************************************************--]] function on_air_resp_callback(key, value) ...... --****************************************************************** --条件: 4g初始化完成 --功能: 使用使用at指令获取信号强度和运营商。 --调用函数:at_cops_csq() --函数功能:获取信号强度和运营商 --调用函数:at_tcp_client_cofig() --函数功能:tcp配置 --***************************************************************** if string.find(key,' sapbr=1,1') ~= nil and string.find(value,'ok') ~= nil then ....... at_tcp_client_cofig() end end --[[********************************************************************* ** function name : at_tcp_client_cofig ** descriptions : tcp配置 ** @return : nil,无返回值 ******************************************************************** --]] function at_tcp_client_cofig(connect) local function my_get_ip_cb(key, value) if string.find(key,' cifsr') ~= nil and value ~= nil then tcp_ip = value set_text(sc_tcp, 1, tcp_ip) end end local function my_ip_state(key, value) air_log('my debugmsg key -> '..key) air_log('my debugmsg value -> '..value) if string.find(key,' cipstatus') ~= nil and string.find(value,'ip status') ~= nil then tcp_state = 1 air_log('at_tcp_client_cofig success !!!!!!') end end if connect == nil then connect = 0 end air_cmd_add('at creg=1', 'ok', 1000, 3) --设置当前gprs注册状态 air_cmd_add('at cgreg=1', 'ok', 1000, 3) --设置当前gprs注册状态 air_cmd_add('at cgatt=1', 'ok', 1000, 3) --设置当前gprs附着状态 air_cmd_add('at cipmux='.. connect, 'ok', 1000, 3) --设置为单、多链接模式 --设置为快发模式(推荐使用这种模式) air_cmd_add('at cipqsend=1', 'ok', 1000, 3) air_cmd_add('at cstt?', 'ok', 1000, 3) --启动任务,查看apn air_cmd_add('at cstt="cmiot"', 'ok', 1000, 3) --启动任务,设置apn air_cmd_add('at ciicr', 'ok', 1000, 3) --激活移动场景 air_cmd_add('at cifsr' , 'ok', 1000, 3, my_get_ip_cb) --查询分配的ip地址 air_cmd_add('at cipstatus' , 'state: ip status', 1000, 3, my_ip_state) --查询下链接状态 end
核心api函数
1) at_tcp_client_cofig(connect)
自定义函数,tcp客户端配置
- connect:0-单链接,非0多链接
相关at指令:
本例程中,tcp配置涉及到相关指令有设置为单、多链接模式、快发模式、激活等,相关关at指令,更多详细的at指令参考《luat 4g模块at命令手册v4.2.2.pdf》,如下所示:
1) 设置链接模式:
屏幕发送:at cipmux=0。设置单链接,非零(1/2)为多连接模式。
屏幕接收:ok
2) 设置移动激活场景:
屏幕发送:at ciicr?
屏幕接收:ok
3) 查询分配的ip地址:
屏幕发送:at cifsr
屏幕接收:ok
3. 连接服务器
当用户输入端口号后,点击联机,调用at_tcp_client_connect()并设置服务器接收模式,代码如程序清单 3所示:
程序清单 3 连接服务器
--[[********************************************************************* ** function name : at_tcp_client_connect ** descriptions : 设置接收模式 ** @head : 是否显示头部 ** @showtp : 是否显示协议 ** @srip : 是否显示ip、端口 ** @return : nil,无返回值 *********************************************************************--]] function at_set_tcp_recv_mode(head, showtp,srip) air_cmd_add('at ciphead='..head, 'ok', 1000, 3) --显示头部 air_cmd_add('at cipshowtp='..showtp, 'ok', 1000, 3) --显示协议 air_cmd_add('at cipsrip='..srip, 'ok', 1000, 3) --显示ip、端口 end --[[********************************************************************* ** function name : at_tcp_client_connect ** descriptions : 连接服务器 ** @mode : 连接模式,tcpupd ** @server_ip : 服务器地址 ** @port : 服务器端口 ** @return : nil,无返回值 ********************************************************************--]] function at_tcp_client_connect(mode, server_ip, port) --连接服务器 air_cmd_add( 'at cipstart="'..mode..'",'..'"'..server_ip..'",'..port, 'ok', 1000, 3) --查询当前gprs注册状态 air_cmd_add('at cipstatus', 'state: connect ok', 1000, 3) end --[[********************************************************************* ** function name : on_control_notify ** descriptions : 系统回调函数,用户通过触摸修改控件后,执行此回调函数。 ** 点击按钮控件,修改文本控件、修改滑动条都会触发此事件。 ** @return : nil,无返回值 ********************************************************************--]] function on_control_notify(screen,control,value) if screen == sc_tcp then if control == 4 and value == 0 and tcp_state == 1 then tcp_server_ip = get_text(sc_tcp, 3) tcp_server_port = get_text(sc_tcp, 3) at_tcp_client_connect('tcp', tcp_server_ip, tcp_server_port) --连接服务器 at_set_tcp_recv_mode(1, 1, 1) --设置接收模式 ...... end end end
核心api函数
1) at_set_tcp_recv_mode(head, showtp,srip)
设置接收模式
- head: 是否显示头部
- showtp: 是否显示协议
- srip: 是否显示ip、端口
- return: nil,无返回值
2) at_tcp_client_connect(mode, server_ip, port)
连接服务器
- mode: 连接模式,tcpupd
- server_ip : 服务器地址
- port : 服务器端口
相关at指令:
1) 请求连接:
屏幕发送:at cipstart="tcp","60.166.18.9",7500。
屏幕接收:ok
2) 设置服务器数据包含头:
屏幕发送:at ciphead=1
屏幕接收:ok
3) 设置服务器协议包含协议:
屏幕发送:at cipshowtp=1
屏幕接收:ok
4) 设置服务器协议包含ip、端口:
屏幕发送:at cipsrip=1
屏幕接收:ok
4. 发送数据到服务器
用户输入文本数据点击发送后,调用at_tcp_send ()发送数据到服务端,代码如程序清单 4所示:
程序清单 4 发送数据
--[[********************************************************************* ** function name : on_control_notify ** descriptions : 系统回调函数,用户通过触摸修改控件后,执行此回调函数。 ** 点击按钮控件,修改文本控件、修改滑动条都会触发此事件。 ** @return : nil,无返回值 *********************************************************************--]] function on_control_notify(screen,control,value) if screen == sc_tcp then ...... local send_data = get_text(sc_tcp, 5) at_tcp_send(send_data) ....... end end --[[********************************************************************* ** function name : at_tcp_send ** descriptions : tcp、发送数据到服务端 ** @str : 发送数据 ** @return : nil,无返回值 ********************************************************************--]] function at_tcp_send(str) air_cmd_add('at cipsend='..string.len(str) , '>', 1000, 3) air_cmd_add(str, 'data accept', 1000, 3) end
核心api函数
1) at_tcp_send(str)
发送数据到服务器
str: 字符串
相关at指令:
1) 发送数据(确定长度):
屏幕发送:at cipsend=10
屏幕接收:>
2) 发送数据(数据内容)
屏幕发送:1234567890
屏幕接收:data accept:10
5. 接收服务器的数据
本例程中,接收服务器的信息是含接收员(目标服务器的ip、端口)、帧头、协议类型、数据内容等,在函数at_set_tcp_recv_mode(head, showtp,srip)配置。程序清单如程序清单 5所示。
程序清单 5 接收服务器数据
--[[********************************************************************* ** function name: split ** descriptions : 切割字符串 ** @ str : 源字符串 ** @ pat : 分割符号 *********************************************************************--]] function split(str, pat) local t = {} local last_end = 0 local s, e = string.find(str, pat, 1) --第一个分割号 local i = 1 while s do table.insert(t, string.sub(str, last_end 1, last_end s - last_end - 1)) last_end = e s, e = string.find(str, pat, last_end 1) i = i 1 end if last_end <= #str then cap = string.sub(str, last_end 1) table.insert(t, cap) end return t end --[[********************************************************************* ** function name: on_air_resp_callback ** descriptions : 4g模块-数据回调接口 ** @key : 屏幕->4g模块的发送请求 ** @value : 4g模块->返回的数据 *********************************************************************--]] function on_air_resp_callback(key, value) ...... if value ~= nil then if string.find(value,'from') ~= nil then -- recv from: 60.166.12.210:7500 --分割成3份,分别为 recv from、60.166.12.210、7500 local serverinfo = split(value,':') set_text(sc_tcp, 7, serverinfo[2]) –- ip set_text(sc_tcp, 8, serverinfo[3]) –-端口 elseif string.find(value,'ipd') ~= nil then -- ipd,tcp,10:123456789 --分割成2份,分别为 ipd,tcp,10、123456789 local serverdata = split(value,':') set_text(sc_tcp, 9, serverdata[2]) -- 数据内容 end end ...... end
相关at指令:
1) 接收数据源(服务器ip、端口):
屏幕接收: recv from: 60.166.12.210:7500
2) 接收数据(帧头 协议 数据长度 数据内容):
屏幕接收: ipd,tcp,10:123456789
工程编译成功后在输出窗口会提示编译成功,如图5-5所示;
图5-5编译成功
在菜单栏中,文件→打开工程目录,在‘dciot_build’目录的‘private’拷贝到sd卡中,如图5-6和图5-7所示;把sd卡接上串口屏后重新上电,等到提示烧录工程成功后,拔掉sd卡重新上电即可。
图5-6下载文件
图5-7拷贝到sd卡