Article From:https://www.cnblogs.com/yangfengwu/p/9968883.html
 

https://www.cnblogs.com/yangfengwu/p/9968716.html

 

 

Now take a look at the official demo

 

 Actually, as long as there are two, that’s all.

module(...,package.seeall)

--[[
Function name: printFunction: Print interface, all prints in this file will be prefixed with testParameter: NoneReturn value: None]]Local function print (...)_G.print ("test",...)
end


--Serial port configurationLocal UART_ID= 1 --uart1
local uartReadData = "";
local uartReadDataCnt = 0;


--Timer idle interrupt detection (serial port idle receiving data)Local Uart Timer Cnt= 0;
local function UartTimer()
    if  uartReadDataCnt ~= 0 then
        UartTimerCnt = UartTimerCnt+1;
        if  UartTimerCnt >= 20 then
            UartTimerCnt=0;
            uartReadDataCnt = 0;
            uart.write(UART_ID,uartReadData);
            uartReadData = "";
        end
    end
end
sys.timer_loop_start(UartTimer,10)


--Read data received by serial portLocal uartdata= ""
local function read()
    uartdata = ""
    while true do
        uartdata = uart.read(UART_ID,"*l",0)
        if not uartdata or string.len(uartdata) == 0 then break end
        uartReadData = uartReadData..uartdata;
        uartReadDataCnt = uartReadDataCnt +1
        UartTimerCnt = 0;
    end
end


--Register the data receiving function of serial port. When serial port receives data, it will call read interface to read data by interrupt mode.Sys. reguart (UART_ID, read)--Configure and open the serial portUart. setup (UART_ID,115200,8,uart.PAR_NONE,uart.STOP_1)

 

 

 Now the download test is to send back whatever is sent to serial port 1.

 

 

 

 

Okay, now send the command to control GPIO5.

Instruction is used.

{data:switch,bit:1,status:0}   Control GPIO5 Output Low Level

{data:switch,bit:1,status:1}   Control GPIO5 Output High Level

Maybe you will ask, why do you make such complicated instructions? In order to unify the later period, and now the Internet of Things communication JSON format is used a lot, so…

 

module(...,package.seeall)

--[[
Function name: printFunction: Print interface, all prints in this file will be prefixed with testParameter: NoneReturn value: None]]Local function print (...)_G.print ("test",...)
end


--Serial port configurationLocal UART_ID= 1 --uart1
local uartReadData = "";
local uartReadDataCnt = 0;


pio.pin.setdir(pio.OUTPUT,pio.P0_5)
pio.pin.setval(0,pio.P0_5)


--Timer idle interrupt detection (serial port idle receiving data)Local Uart Timer Cnt= 0;
local function UartTimer()
    if  uartReadDataCnt ~= 0 then
        UartTimerCnt = UartTimerCnt+1;
        if  UartTimerCnt >= 20 then--If 200 ms does not interrupt to accept data, it will be considered that a data has been collected.UartTimerCnt=0;
            uartReadDataCnt = 0;
      
            if  uartReadData=="{data:switch,bit:1,status:0}" then
                pio.pin.setval(0,pio.P0_5)
                uart.write(UART_ID,uartReadData);--Return dataElseif uartReadData=="{data:switch,bit:1,status:1}" then
                pio.pin.setval(1,pio.P0_5)      
                uart.write(UART_ID,uartReadData);--Return dataEndUartReadData= "";
        end
    end
end
sys.timer_loop_start(UartTimer,10)--10MSCyclic timer--Read data received by serial portLocal uartdata= ""
local function read()
    uartdata = ""
    while true do
        uartdata = uart.read(UART_ID,"*l",0)
        if not uartdata or string.len(uartdata) == 0 then break end
        uartReadData = uartReadData..uartdata;--receive dataUartReadDataCnt= uartReadDataCnt +1
        UartTimerCnt = 0;
    end
end


--Register the data receiving function of serial port. When serial port receives data, it will call read interface to read data by interrupt mode.Sys. reguart (UART_ID, read)--Configure and open the serial portUart. setup (UART_ID,115200,8,uart.PAR_NONE,uart.STOP_1)

 

 

 

 

 

 

 

 

 

Source Link

Link: https://pan.baidu.com/s/1-SRfsKGQ7rZVvFmp1ObHWw password: p9qs

If the link of the source code of the basic course is invalid, please download it in the introduction of Taobao. Because the link is still invalid, please contact the seller. Thank you.

https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w4004-18540610442.6.36a74814ZSaRsu&id=569295486025

 

https://www.cnblogs.com/yangfengwu/p/9969179.html

 

Link of this Article: 17-GPRS (Air202) Serial Port

Leave a Reply

Your email address will not be published. Required fields are marked *