Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --vars
- master_rx_port=314
- master_tx_port=315
- modem_side = "right"
- data = {}
- number_o_slaves = 7
- wait_time = 8
- --Functions:
- --Transmit
- --Asking for data
- function inforeq()
- modem.transmit(master_tx_port,master_rx_port,"SendInfo")
- logging("transmit")
- end
- --/Transmit
- --Recieve
- --The slaves have 8 seconds to send data. If not
- --all data are recieved in that time, then the
- --error flag is set. Incoming date is stored in
- --datain table varriable.
- function recieve_info(exp_msg,time_out)
- local cnt=0 --counter for the recieved messages
- local datain = {}
- local timerID=os.startTimer(time_out) --starting timer
- print(timerID)
- while(cnt<=exp_msg) do --loop for recieving all messages
- local event,side,rxCH,txCH,msg = os.pullEvent()
- if event == "modem_message" and rxCH == master_rx_port then --data recieved
- datain[cnt]=msg --storing data
- cnt=cnt+1
- end
- if event == "timer" then --not all messages have arrived
- erc = 01 --setting the error flag
- logging("timeout",cnt)
- break --if timer went off, then no more looping
- end
- end
- if cnt == exp_msg then
- os.cancelTimer(timerID)
- logging("info_req_ok")
- return datain
- else
- local retrycount = retrycount+1
- term.setCursorPos(1,2)
- if retrycount == 1 then
- print("Timed out, retrying for the 1st time...")
- elseif retrycount == 2 then
- print("Timed out, retrying for the 2nd time...")
- elseif retrycount == 3 then
- print("Timed out, retrying for the 3rd time...")
- else
- print("Timed out, retrying for the "..retrycount.."th time")
- end
- end
- end
- --/Recieve
- --Counter
- --Counts the number of full, empty and half-empty
- --RECs, then calls the evaluator.
- function counter (data)
- local full = 0
- local some = 0
- local empty = 0
- local i = 1
- local v = 1
- for i,v in pairs (data) do
- if v == "Full" then full = full+1
- elseif v == "Some" then some = some+1
- else empty = empty+1
- end
- end
- logging("counter",full,some,empty)
- evaluator(full, some, empty)
- end
- --/Counter
- --Evaluator
- --Turning on and off redstone signals (engines)
- --according to the set rules. Lazy to type them...
- function evaluator(full, some, empty)
- if some < 1 then
- if full <= 3 and empty >= 5 then
- rs.setOutput("top",true)
- rs.setOutput("bottom",true)
- logging("evaluator",2)
- elseif empty <= 4 and empty >= 1 then
- rs.setOutput("top",true)
- rs.setOutput("bottom",false)
- logging("evaluator",1)
- else
- rs.setOutput("top",false)
- rs.setOutput("bottom",false)
- logging("evaluator",0)
- end
- else
- if full <= 2 and empty >=5 then
- rs.setOutput("top",true)
- rs.setOutput("bottom",true)
- logging("evaluator",2)
- elseif empty <=4 and empty >= 2 then
- rs.setOutput("top",false)
- rs.setOutput("bottom",true)
- logging("evaluator",1)
- else
- rs.setOutput("top",false)
- rs.setOutput("bottom",false)
- logging("evaluator",0)
- end
- end
- end
- --/Evaluator
- --Logging
- --Creates a r_mst.log file and then writes notes
- --to it as the program runs.
- function logging(specifier, p1,p2,p3)
- local time = textutils.formatTime(os.time(),true)
- local day = os.day()
- local activity
- local f
- if not fs.exists("r_mst.log") then
- f=fs.open("r_mst.log","w")
- else
- f=fs.open("r_mst.log","a")
- end
- if specifier == "transmit" then
- activity = "Transmitting request..."
- elseif specifier == "info_req_ok" then
- activity = "All answers recieved!"
- elseif specifier == "timeout" then
- activity = "Timed out. Recieved "..p1.." answers"
- elseif specifier == "counter" then
- activity = "Counted "..p1.." full, "..p2.." half-full and "..p3.." empty"
- elseif specifier == "evaluator" then
- activity = p1.."engines operating"
- end
- output=day.." @ "..time..": "..activity
- f.writeLine(output)
- f.close()
- end
- --Main
- --The program is intended to request information
- --from the slaves about energy cells. Then recieveis
- --the requested data. The recieved data contains
- --how many REC are full empty or has some energy in
- --them. After this the program counts how many full
- --or empty and then turns the engines (redstone outputs)
- --ON or OFF according to the values.
- modem=peripheral.wrap(modem_side)
- modem.open(master_rx_port)
- while true do
- print("Refinery program is Running")
- erc = 00 --Flag to see if all data has arrived
- retrycount = 1
- while true do --Loop for re-requesting data in case of error
- inforeq( ) --Information request
- data = recieve_info(number_o_slaves,wait_time) -- data table
- if erc == 00 then break end --error check
- end
- term.clear()
- counter(data) --counting and evaluating data
- end
Advertisement
Add Comment
Please, Sign In to add comment