This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mrpoopy345's profile picture

Rednet Logging

Started by mrpoopy345, 17 November 2013 - 07:43 AM
mrpoopy345 #1
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()
Cozzimoto #2
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 recieved

local ev = { os.pullEvent() } declare a local var 'ev' which is a table that stores everything os.pullEvent returns

if ev[1] == "modem_message" or ev[1] == "rednet_message" for backwards compatibility on the Rednet API or the Modem API

local 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 file

file.writeLine(textutils.serialize(ev)) turn the table into a string and write to file

file.close() close the file handle and save data
Edited on 17 November 2013 - 08:22 AM
jag #3
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..
mrpoopy345 #4
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!