前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >6-Air202串口学习(Air202串口1接收到什么内容就回复什么内容)

6-Air202串口学习(Air202串口1接收到什么内容就回复什么内容)

作者头像
杨奉武
发布2018-08-01 11:33:05
3830
发布2018-08-01 11:33:05
举报
文章被收录于专栏:知识分享知识分享

实现的功能

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

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

先贴出来程序,,,

代码语言:javascript
复制
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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档