4-学习GPRS_Air202(串口)

时间:2022-06-02
本文章向大家介绍4-学习GPRS_Air202(串口),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

实现的功能

有人会想,不就是个串口接收到什么就会什么的程序嘛!!!!!!!!!!!!!!有什么好说的!!

实现此功能的程序是很多,但是,但是,但是.....我写的程序更注重于开发和实用,不信往下看

先贴出来程序,,,

module(...,package.seeall)

local function print(...)
    _G.print(...)
end

print("dofile usart.lua................................................");

local UART_ID = 1 --uart1
local uartReadData = "";
local uartReadDataCopy = "";
local uartReadDataCnt = 0;
local uartReadDataCntCopy = 0;

--定时器空闲中断检测10ms
local function TimerFunc4()
       if  uartReadDataCnt ~= 0 then
        uartReadDataCnt = 0;
        uartReadDataCntCopy = 0;
        uartReadDataCopy = uartReadData;
        uartReadData = "";
        uart.write(UART_ID,uartReadDataCopy)
       else
            uartReadDataCntCopy = uartReadDataCnt;
       end
end
sys.timer_loop_start(TimerFunc4,10)


--读取串口接收到的数据
local function read()
    local data = ""
    while true do
        data = uart.read(UART_ID,"*l",0)
        if not data or string.len(data) == 0 then break end
        uartReadData = uartReadData..data;
        uartReadDataCnt = uartReadDataCnt +1
    end
end

pm.wake("wake")
--注册串口的数据接收函数,串口收到数据后,会以中断方式,调用read接口读取数据
sys.reguart(UART_ID,read)
--配置并且打开串口
uart.setup(UART_ID,115200,8,uart.PAR_NONE,uart.STOP_1)

凡是看懂我这篇文章的才能瞬间看懂我的这个程序

http://www.cnblogs.com/yangfengwu/p/8912072.html      是否看懂了,我没有骗人吧!!凡是串口接收的程序,无论单片机还是上位机这个接收

程序的模式是通吃的......

所以下各个部分的功能

如果一开始调用的  

 pm.wake("aaaa")

假如想让系统休眠----pm.sleep("aaaa")

不过程序上让我产生了疑惑

后面的0是干什么的.....................

 然后就看源码

 算啦说一下整体的思路

串口中断里面有一个负责接收数据的字符串变量,还有一个数据个数累加的变量

假设串口一次发100个数据,,然后间隔1S发一次

定时器是每间隔10Ms检测一次

如果数据没有接收完 uartReadDataCnt 会一直累加,因为每间隔10Ms去检测一次,uartReadDataCnt在这期间会向上累加...

所以数据没有接收完成的时候进入定时器的时候 uartReadDataCntCopy 总是 < uartReadDataCnt

假设接收完了,因为是1S发一次,而定时器是每隔10Ms检测一次,所以会检测到uartReadDataCntCopy == uartReadDataCnt

所以....处理数据就可以了,,,接收到一条完整的数据了....