繁体   English   中英

Lua脚本-收集数据并保存到文件中

[英]Lua script - collection data and save into a file

我正在编写lua脚本。

我正在编码的是..收集数据并将其保存到特定文件中。

情况
有两个传感器,当它们识别前面的物体时,传感器的值将增加。
我想每隔100ms保存一次传感器值数据。
时间格式为“ 2013-04-25 10:30:004”

我所做的就是在这里。

===========================================================

require("TIMER")
require("TIMESTAMP")
require("ANALOG_IN")

function OnExit()
    print("Exit code...do something")
end

function main()

    timer = "TIMER"
    analogsensor_1 = "AIR_1"
    analogsensor_2 = "AIR_2"

    while true do 
        valueOfSensor_1 = ANALOG_IN.readAnalogIn(analogsensor_1);
        valueOfSensor_2 = ANALOG_IN.readAnalogIn(analogsensor_2);

        write(colltection_of_data.txt)
        go(print(valueOfSensor_1), 0.1)     //print value of sensor every 100ms
        print(time)
        go(print(valueOfSensor_2), 0.1)
        print(time)
    end 
    TIMER.sleep(timer,500)

end 

print("start main")
main()

================================================================

我知道这不是完整的代码。 如何将数据保存到特定文件中? 怎样显示这样的时间格式?

先感谢您!

要获取日期和时间,请致电:

local timestr = os.date("%Y-%m-%d %H:%M:%S")

现在要将其保存到文件中,您需要打开文件

local filehandle = io.open(filename[, mode]) - 手册

要输出所需的数据,您可以使用

local filehandle = io.open("Log.txt", "w+")
filehandle:write(timestr, " - Sensor1: ", tostring(valueOfSensor1), "\n")

当然,您只打开文件一次,然后每x(milli)秒发出一次写命令。 完成后:

filehandle:close()

附言:请尽可能使用当地人。 它比全局变量要快得多( local analogSensor_1而不是analogSensor_1

抱歉,没有小数秒

-- Open file
local file = assert(io.open('collection_of_data.txt','wb'))

-- Write to file
local dt = os.date'*t'
local time_string = 
   dt.year..'-'..('0'..dt.month):sub(-2)..'-'..('0'..dt.day):sub(-2)..' '..
   ('0'..dt.hour):sub(-2)..':'..('0'..dt.min):sub(-2)..':'..('0'..dt.sec):sub(-2)
file:write(valueOfSensor_1, '\n', time_string, '\n')

-- Close file
file:close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM