0

I am working on coding lua script.

What I am coding is..Collecting the data and save it into the certain file.

Situation :
There are two sensors that when they recognize the object in front of it, the value of the sensor will be increased.
I want to save the data of the value of sensor every 100ms with time.
Time format would be "2013-04-25 10:30:004"

What I did is here.

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

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()

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

I know it's not complete code. How can I save the data into certain file? and how can I show the time format like that?

Thank you in advance!

4

2 回答 2

4

要获取您致电的日期和时间:

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()

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

于 2013-04-25T13:32:05.737 回答
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()
于 2013-04-25T13:29:35.090 回答