147 posts
Location
My Computer
Posted 17 November 2013 - 08:43 AM
Hello pros! What I want to do is have a computer send a message to another computer and the other computer logs it inside of a file.
My current setup:
First computer:
rednet.open("top")
print("What would you like to send?")
local input = read()
rednet.send(13, input)
sleep(1)
print("Message sent!")
Receiving computer:
id, msg = rednet.receive()
local file = fs.open("log", "a")
file.writeLine(msg)
file.close()
212 posts
Location
Dallas, Tx
Posted 17 November 2013 - 09:21 AM
The rednet.receive() will yield the entire program until an actual message is recieved. Like if you wanted to do other things while you wait for the message to come in like monitor clicks, timers, redstone events i would recommend doing something like the following for your logging computer
while true do
local ev = { os.pullEvent() }
if ev[1] == "modem_message" or ev[1] == "rednet_message" then
local file = fs.open('log','a')
file.writeLine(textutils.serialize(ev))
file.close()
end
end
while true do:
keep it running in a loop to log every action recievedlocal ev = { os.pullEvent() }
declare a local var 'ev' which is a table that stores everything os.pullEvent returnsif ev[1] == "modem_message" or ev[1] == "rednet_message"
for backwards compatibility on the Rednet API or the Modem APIlocal file = fs.open('log','a')
declare a local var 'file' to open a file handle 'log' in append mode which will keep its current data in the filefile.writeLine(textutils.serialize(ev))
turn the table into a string and write to filefile.close()
close the file handle and save data
Edited on 17 November 2013 - 08:22 AM
521 posts
Location
Stockholm, Sweden
Posted 17 November 2013 - 09:24 AM
Is there an aqual problem here? Cause from the look of the code it should work just fine as is. Maybe put it in a loop so it continues to listen..
147 posts
Location
My Computer
Posted 17 November 2013 - 12:13 PM
I found the solution! Turns out I needed to put rednet.open("back") at the start of the receiving computers code!