中文字幕天堂手机版-欧美一区二区色大片在线观看-丰满人妻视频一区二区三区-美女视频黄的免费的91

您的位置:首頁   >  關(guān)于我們  >  新聞動態(tài)

大彩醫(yī)用級串口屏Lua應(yīng)用-文件讀寫

發(fā)布人:大彩科技發(fā)布日期:2020-12-31

文件讀寫演示視頻


一、適用范圍


本文檔適合大彩M系列醫(yī)用級的串口屏產(chǎn)品使用。




二、開發(fā)環(huán)境版本


1.VisualTFT軟件版本:V3.0.0.1111及以上的版本。

版本查看:

1)打開VisualTFT軟件啟動頁面如圖2-1軟件版本,右上角會顯示的軟件版本號;

圖片

圖2-1軟件版本


2) 打開VisualTFT,在軟件右下角可以查看軟件版本圖2-2軟件版本,最新版本可登錄http://gc086.com/進(jìn)行下載。

圖片

圖2-2軟件版本


2.串口屏硬件版本:V6.3.249.0 及以上的版本。

版本查看:

1) 查看屏幕背面版本號貼紙;

2) VisualTFT與屏幕聯(lián)機(jī)成功后,右下角顯示的版本號。




、概述


本例程中,介紹Lua系統(tǒng)函數(shù)中的文件API讀寫,其中F系列和M系列僅支持對SD卡讀寫,物聯(lián)型系列支持對屏內(nèi)及SD卡、U盤的讀寫。




四、參考資料


1



《LUA 腳本API V1.4》可通過以下鏈接下載物聯(lián)型開發(fā)包獲取:

http:/gc086.com/index.php?s=/List/index/cid/19.html

2



《LUA基礎(chǔ)學(xué)習(xí)》可通過以下鏈接下載物聯(lián)型開發(fā)包獲取:

http:/gc086.com/index.php?s=/List/index/cid/19.html

3



LUA腳本初學(xué)者可以通過下面鏈接進(jìn)行學(xué)習(xí)。

http://www.runoob.com/lua/lua-arrays.html

4



AT指令,可以通過下面子連接了解

http://www.openluat.com/




五、教程實現(xiàn)


本文主要將以下2點進(jìn)行說明:

1. 準(zhǔn)備工程素材;

2. 配置串口屏工程;







5.1 準(zhǔn)備工程素材

5.1.1 準(zhǔn)備工程素材

在實現(xiàn)例程前需要作以下3個準(zhǔn)備:

1. 硬件平臺;

2. 軟件平臺;

3. UI素材;

該例程使用大彩M系列7寸串口屏DC80480M070_1111_0T為驗證開發(fā)平臺。如圖5-1所示;

圖片

圖5-1 M系列7寸串口屏


其他尺寸、系列的串口屏均可借鑒此教程。


5.1.2 軟件平臺

使用大彩自主研發(fā)的上位機(jī)軟件VisualTFT配置工程,登錄http://gc086.com/下載。如圖5-2所示;

圖5-2下載軟件







5.2 配置串口屏工程

本文的文件主要介紹以下2點:

(1) 讀文件

(2) 寫文件

5.2.1 讀文件

本例程中的讀文件,是讀取SD根目錄下的‘1.txt’文件,并用數(shù)據(jù)記錄控件顯示出來,用戶可滑動數(shù)據(jù)記錄控件,查看‘1.txt’里面的內(nèi)容。


 1. 畫面配置 

在畫面ID1中,添加1個數(shù)據(jù)記錄控件(控件ID1)和一個按鈕控件(控件ID2),其中控件ID1用于顯示‘1.txt’文件的內(nèi)容??丶蘒D2 用于觸發(fā)讀取‘1.txt’文件的按鈕。畫面配置如圖5-3所示:

注意:其他非關(guān)鍵控件不在一一介紹,下文不在累述

圖5-3 畫面配置


 2. LUA腳本編輯 

本例程中,用戶點擊按鈕控件ID2后,將讀取SD目錄下載的‘1.txt’文件,并添加到數(shù)據(jù)記錄控件中顯示;若文件不存在,則彈框提示。代碼如程序清單1所示:


程序清單 1 讀文件



--get the type and length of the variable
--@data: 'string' or 'table'
--@return: len and type of data
function my_getdataLen_Type(data)

    my_debug_print('---------- my_getdataLen_Type ----------')
    local datalen = -1
    --獲取數(shù)據(jù)類型
    local data_type = type(data)

    --計算數(shù)據(jù)長度
    if data_type == 'string'
    then
        datalen = string.len(data)
    elseif data_type == 'table'
    then
        datalen = #(data)
    end
    my_debug_print('Lua_debug data Type and Len: '..data_type..' /'..datalen)

    return datalen,data_type
end

--Write data to the end of the file
--@file:file path
--@data:The type of '@data' only be [string] or [byte array]
--@open_mode:open file mode
function my_write_filedata(file, data, open_mode)

    my_debug_print('---------- my_write_filedata ----------')
    my_debug_print('Lua_debug: file -> '..file..' / data -> '..type(data)..' / open_mode -> '..open_mode)
    local count     = 0
    local write_cnt = 0
    local offset    = 0
    local all_byte  = 0
    --獲取待寫入數(shù)據(jù)的數(shù)據(jù)類型和長度
    local wrire_len, data_type = my_getdataLen_Type(data)

    local write_byte_Tb = {}
    local open_state = file_open(file, open_mode)

    if open_state == true
    then
        --獲取當(dāng)前文件大小,僅在追加文件末尾寫入執(zhí)行
        if open_mode == add_write
        then
            all_byte = file_size()
        end

        if wrire_len > 0
        then
            --計算'@data'要寫多少次
            write_cnt =  math.modf(wrire_len / WriteOnceSize)
            if wrire_len % WriteOnceSize > 0
            then
                write_cnt = write_cnt + 1
            end
            my_debug_print('Lua_debug: need write allcnt -> '..write_cnt)

            for i = 1, write_cnt
            do
                --復(fù)位寫字節(jié)數(shù)組
                write_byte_Tb = {}

                --計算寫的位置
                offset = (i - 1) * WriteOnceSize +all_byte
                local offst_result = file_seek(offset)
                --文件偏移失敗
                if offst_result == false 
                then
                    set_text(sc_prompt, 1, 'When reading the file, an offset error occurred. please try again! ! !')
                    set_text_roll(sc_prompt, 1, 50)
                    change_child_screen(sc_prompt)
                    break
                end
                my_debug_print('Lua_debug: cur offset  -> '..offset)

                --計算本次寫的個數(shù)
                count = WriteOnceSize
                if i == write_cnt
                then
                    if wrire_len % WriteOnceSize > 0
                    then
                        count = wrire_len % WriteOnceSize
                    end
                end
                my_debug_print('Lua_debug: cur write  -> '..write_cnt..'th /wrire count '..count)
                --填充寫入flash的字節(jié)數(shù)組
                for j = 1, count 
                do
                    if data_type == 'string'
                    then
                    --字符串類型,將每個字符轉(zhuǎn)換為字節(jié)數(shù)組
                    write_byte_Tb[j - 1] = tonumber(string.byte(data, ((i - 1) * WriteOnceSize + j), ((i - 1) * WriteOnceSize + j)))

                    elseif data_type == 'table'
                    then
                        --數(shù)組類型,字節(jié)賦值
                        write_byte_Tb[j - 1] = 
                        data[((i - 1) * WriteOnceSize + j)]
                    end
                end
                local IswriteOK = file_write(write_byte_Tb)
                if IswriteOK == false
                then
                    i = i - 1
                end    
            end
        end
    else
        set_text(sc_prompt, 1, 'The file don`t exist, please check the contents of the SD car! ! !')
        set_text_roll(sc_prompt, 1, 50)
        change_child_screen(sc_prompt)    
    end

    --關(guān)閉文件
    file_close()
end

--用戶通過觸摸修改控件后,執(zhí)行此回調(diào)函數(shù)。
--點擊按鈕控件,修改文本控件、修改滑動條都會觸發(fā)此事件。
function on_control_notify(screen,control,value)
    ......
    --追加寫
    elseif screen == sc_WriteFile and control == 3 and value == 0
then
        sc_ShowRecord = sc_WriteFile
        --已插入SD卡
        if IsinsertSD == 1
        then
            record_clear(sc_WriteFile, 1)

            local str = 'n'..get_text(sc_WriteFile, 2)    
            my_write_filedata(sd_dir..'/'..'1.txt', str, add_write)

            my_read_filedata(sd_dir..'/'..'1.txt')
            local allrecordcnt = record_get_count(sc_WriteFile, 1)
            record_setoffset(sc_WriteFile, 1, allrecordcnt - 1)
            record_select(sc_WriteFile   , 1, allrecordcnt - 1)
        --未插入SD卡
        else
            set_text(sc_prompt, 1, 'Please insert the SD card, or check if the SD is compatible! ! !')
            set_text_roll(sc_prompt, 1, 50)
            change_child_screen(sc_prompt)
        end

    --覆蓋寫
    elseif screen == sc_WriteFile and control == 5 and value == 0
    then
        sc_ShowRecord = sc_WriteFile
        --已插入SD卡
        if IsinsertSD == 1
        then
            record_clear(sc_WriteFile, 1)

            cnt = cnt + 1
            local str = (cnt + 1)..'th write -> '..get_text(sc_WriteFile, 4) 
            my_write_filedata(sd_dir..'/'..'NewTxtFile.txt',str,over_write)

            my_read_filedata(sd_dir..'/'..'NewTxtFile.txt')
        --未插入SD卡
        else
            set_text(sc_prompt, 1, 'Please insert the SD card, or check if the SD is compatible! ! !')
            set_text_roll(sc_prompt, 1, 50)
            change_child_screen(sc_prompt)
        end
    ......
    end
end

▲下滑查看


 核心API函數(shù) 

1) file_open(path,mode)

打開文件,成功返回true,失敗false


  • path-文件路徑
  • mode-打開模式,如下組合方式:



打開模式

FA_OPEN_EXISTING

0x00

FA_READ

0x01

FA_WRITE

0x02

FA_CREATE_NEW

0x04

FA_CREATE_ALWAYS

0x08

FA_OPEN_ALWAYS

0x10

例如:

打開文件用于讀取file_open(path, 0x01) 

創(chuàng)建文件用于寫入file_open(path, 0x02|0x08)


2) file_size()

獲取當(dāng)前文件大小,返回字節(jié)數(shù)

例如:all_byte = file_size()


3) file_seek(offset)

定位文件讀取位置,成功返回true,失敗false


  • offset-文件偏移位置


例如:定位到第2048個字節(jié),file_seek(2048)


4) file_read(count)

讀取文件內(nèi)容,成功返回table數(shù)組,失敗返回nil


  • count-讀取字節(jié)數(shù),最大讀取2048個字節(jié)


例如:讀取2048個字節(jié),read_byte_Tb = file_read(2048)


5) file_close()

關(guān)閉文件,成功返回true,失敗false 


基本思路:當(dāng)按鈕控件ID2按下的時候,觸發(fā)觸摸控件回調(diào)函數(shù)on_control_notify(),在調(diào)用自定義函數(shù)my_read_filedata()。

1) 打開文件:以只讀的方式FA_READ(0x01)打開指定文件。
2) 獲取文件大小:文件打開成功后,調(diào)用file_size() Api函數(shù)獲取該文件的大小。
3) 計算讀取次數(shù):根據(jù)文件大小,得出讀取的次數(shù)。本例程采取一次讀取2048個字節(jié)(注意:屏幕一次讀取最大字節(jié) <= 2048)
4) 計算讀取偏移量:每次讀取需要調(diào)用file_seek(offset)定位讀取位置,offset相當(dāng)于已讀取的字節(jié)數(shù)
5) 讀出數(shù)據(jù):本文讀取的數(shù)據(jù),轉(zhuǎn)換成字符拼接字符串顯示出來,詳細(xì)應(yīng)用根據(jù)實際情況出來。

6) 關(guān)閉文件:文件讀取完畢,將該文件關(guān)閉。


5.2.2 寫文件

常用的寫文件操作主要有以下兩種:

1) 追加寫:寫在文件末尾。本例程是寫在SD根目錄下的‘1.txt’文件末尾,并用數(shù)據(jù)記錄控件顯示出來。

2) 覆蓋寫:清空在寫入數(shù)據(jù)。本例程在SD卡目錄下,每次寫入創(chuàng)建一個新的NewFile.txt,并寫入數(shù)據(jù)。


追加寫和覆蓋寫的功能實現(xiàn)區(qū)別:

1) 打開方式:file_open(path,mode)。


  • 追加寫:mode = FA_WRITE|FA_READ;
  • 覆蓋寫:mode = FA_CREATE_ALWAYS|FA_WRITE;


2) 寫入位置:file_seek(offset)。


  • 追加寫:offset = file_size() + (i - 1) * WriteOnceSize;其中WriteOnceSize為一次寫入數(shù)據(jù)的大小,i為循環(huán)寫入的次數(shù)。
  • 覆蓋寫:offset = 0;



 1. 畫面配置 

在畫面ID2中,添加1個數(shù)據(jù)記錄控件、2個文本控件(控件ID2、控件ID4)和2個按鈕控件(控件ID3、控件ID5),其中數(shù)據(jù)記錄控件僅做顯示文件內(nèi)容效果。按鈕控件3用于體現(xiàn)追加寫功能,按鈕控件5用于覆蓋寫功能。畫面配置如圖5-4所示:

圖片

圖5-4 畫面配置


2. LUA腳本編輯 

按鈕控件ID3按下時候,將文本控件ID2的內(nèi)容寫在SD根目錄下的‘1.txt’文件末尾(追加寫);按鈕控件ID5按下時候,將文本控件ID4的內(nèi)容寫在SD根目錄下的‘NewFile.txt’文件(覆蓋寫)代碼如程序清單 2所示。


程序清單 2 寫文件

--get the type and length of the variable
--@data: 'string' or 'table'
--@return: len and type of data
function my_getdataLen_Type(data)

    my_debug_print('---------- my_getdataLen_Type ----------')
    local datalen = -1
    --獲取數(shù)據(jù)類型
    local data_type = type(data)

    --計算數(shù)據(jù)長度
    if data_type == 'string'
    then
        datalen = string.len(data)
    elseif data_type == 'table'
    then
        datalen = #(data)
    end
    my_debug_print('Lua_debug data Type and Len: '..data_type..' /'..datalen)

    return datalen,data_type
end

--Write data to the end of the file
--@file:file path
--@data:The type of '@data' only be [string] or [byte array]
--@open_mode:open file mode
function my_write_filedata(file, data, open_mode)

    my_debug_print('---------- my_write_filedata ----------')
    my_debug_print('Lua_debug: file -> '..file..' / data -> '..type(data)..' / open_mode -> '..open_mode)
    local count     = 0
    local write_cnt = 0
    local offset    = 0
    local all_byte  = 0
    --獲取待寫入數(shù)據(jù)的數(shù)據(jù)類型和長度
    local wrire_len, data_type = my_getdataLen_Type(data)

    local write_byte_Tb = {}
    local open_state = file_open(file, open_mode)

    if open_state == true
    then
        --獲取當(dāng)前文件大小,僅在追加文件末尾寫入執(zhí)行
        if open_mode == add_write
        then
            all_byte = file_size()
        end

        if wrire_len > 0
        then
            --計算'@data'要寫多少次
            write_cnt =  math.modf(wrire_len / WriteOnceSize)
            if wrire_len % WriteOnceSize > 0
            then
                write_cnt = write_cnt + 1
            end
            my_debug_print('Lua_debug: need write allcnt -> '..write_cnt)

            for i = 1, write_cnt
            do
                --復(fù)位寫字節(jié)數(shù)組
                write_byte_Tb = {}

                --計算寫的位置
                offset = (i - 1) * WriteOnceSize +all_byte
                local offst_result = file_seek(offset)
                --文件偏移失敗
                if offst_result == false 
                then
                    set_text(sc_prompt, 1, 'When reading the file, an offset error occurred. please try again! ! !')
                    set_text_roll(sc_prompt, 1, 50)
                    change_child_screen(sc_prompt)
                    break
                end
                my_debug_print('Lua_debug: cur offset  -> '..offset)

                --計算本次寫的個數(shù)
                count = WriteOnceSize
                if i == write_cnt
                then
                    if wrire_len % WriteOnceSize > 0
                    then
                        count = wrire_len % WriteOnceSize
                    end
                end
                my_debug_print('Lua_debug: cur write  -> '..write_cnt..'th /wrire count '..count)
                --填充寫入flash的字節(jié)數(shù)組
                for j = 1, count 
                do
                    if data_type == 'string'
                    then
                    --字符串類型,將每個字符轉(zhuǎn)換為字節(jié)數(shù)組
                    write_byte_Tb[j - 1] = tonumber(string.byte(data, ((i - 1) * WriteOnceSize + j), ((i - 1) * WriteOnceSize + j)))

                    elseif data_type == 'table'
                    then
                        --數(shù)組類型,字節(jié)賦值
                        write_byte_Tb[j - 1] = 
                        data[((i - 1) * WriteOnceSize + j)]
                    end
                end
                local IswriteOK = file_write(write_byte_Tb)
                if IswriteOK == false
                then
                    i = i - 1
                end    
            end
        end
    else
        set_text(sc_prompt, 1, 'The file don`t exist, please check the contents of the SD car! ! !')
        set_text_roll(sc_prompt, 1, 50)
        change_child_screen(sc_prompt)    
    end

    --關(guān)閉文件
    file_close()
end

--用戶通過觸摸修改控件后,執(zhí)行此回調(diào)函數(shù)。
--點擊按鈕控件,修改文本控件、修改滑動條都會觸發(fā)此事件。
function on_control_notify(screen,control,value)
    ......
    --追加寫
    elseif screen == sc_WriteFile and control == 3 and value == 0
then
        sc_ShowRecord = sc_WriteFile
        --已插入SD卡
        if IsinsertSD == 1
        then
            record_clear(sc_WriteFile, 1)

            local str = 'n'..get_text(sc_WriteFile, 2)    
            my_write_filedata(sd_dir..'/'..'1.txt', str, add_write)

            my_read_filedata(sd_dir..'/'..'1.txt')
            local allrecordcnt = record_get_count(sc_WriteFile, 1)
            record_setoffset(sc_WriteFile, 1, allrecordcnt - 1)
            record_select(sc_WriteFile   , 1, allrecordcnt - 1)
        --未插入SD卡
        else
            set_text(sc_prompt, 1, 'Please insert the SD card, or check if the SD is compatible! ! !')
            set_text_roll(sc_prompt, 1, 50)
            change_child_screen(sc_prompt)
        end

    --覆蓋寫
    elseif screen == sc_WriteFile and control == 5 and value == 0
    then
        sc_ShowRecord = sc_WriteFile
        --已插入SD卡
        if IsinsertSD == 1
        then
            record_clear(sc_WriteFile, 1)

            cnt = cnt + 1
            local str = (cnt + 1)..'th write -> '..get_text(sc_WriteFile, 4) 
            my_write_filedata(sd_dir..'/'..'NewTxtFile.txt',str,over_write)

            my_read_filedata(sd_dir..'/'..'NewTxtFile.txt')
        --未插入SD卡
        else
            set_text(sc_prompt, 1, 'Please insert the SD card, or check if the SD is compatible! ! !')
            set_text_roll(sc_prompt, 1, 50)
            change_child_screen(sc_prompt)
        end
    ......
    end
end

▲下滑查看


 核心API函數(shù) 

1) file_open(path,mode)

相關(guān)說明參考讀文件章節(jié),不在贅述


2) file_size()

相關(guān)說明參考讀文件章節(jié),不在贅述


3) file_seek(offset)

相關(guān)說明參考讀文件章節(jié),不在贅述


4) file_write(data)

寫文件內(nèi)容,成功返回true,失敗返回false


  • data-待寫入的table數(shù)組,索引從0開始,最大一次性寫2048個字節(jié)



5) file_close()

相關(guān)說明參考讀文件章節(jié),不在贅述


基本思路:當(dāng)按鈕控件ID3或按鈕控件ID5按下的時候,觸發(fā)觸摸控件回調(diào)函數(shù)on_control_notify(),在調(diào)用自定義函數(shù)my_write_filedata()。

1) 打開文件:


  • 追加寫:以讀寫的方式(FA_READ|FA_WRITE:0x01|0x02)打開SD卡目錄下的1.txt文件。
  • 覆蓋寫:以新建寫入的方式(FA_CREATE_ALWAYS|FA_WRITE:0x08|0x02)打開SD卡目錄下的NewFile.txt文件


2) 獲取文件大小:文件打開成功后,若追加寫模式,則執(zhí)行file_size() Api代碼段。
3) 計算寫入次數(shù):根據(jù)寫入數(shù)據(jù)的大小,得出讀取的次數(shù)。本例程采取一次寫入2048個字節(jié)(注意:屏幕一次讀取最大字節(jié) <= 2048)
4) 計算寫入的偏移量:每次寫入需要調(diào)用file_seek(offset)定位讀取位置
5) 寫入數(shù)據(jù)

6) 關(guān)閉文件:文件寫完后完畢,將該文件關(guān)閉。







5.3 下載工程

工程編譯成功后在輸出窗口會提示編譯成功,如圖5-4所示。編譯成功后點擊菜單欄中【工具】→【量產(chǎn)向?qū)А?,如圖5-5所示;

圖片

圖5-4編譯成功

圖片

圖5-5量產(chǎn)向?qū)?/span>


在彈窗中選中【SD卡下載】,然后把將文件夾中的private文件夾拷貝到SD卡中,如圖5-6和圖5-7所示;把SD卡接上串口屏后重新上電,等到提示燒錄工程成功后,拔掉SD卡重新上電即可。

圖片

圖5-6量產(chǎn)向?qū)?/span>

圖片

圖5-7拷貝到SD卡