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

rednet.send twice?

Started by Imprivate, 04 January 2014 - 08:31 AM
Imprivate #1
Posted 04 January 2014 - 09:31 AM
Hello all,
So I have a system with 2 computers where one sends a rednet message containing a variable to the other, which then does some stuff, then sends a message back containing a variable. The first computer is meant to print this variable, yet doesn't. I've figured out that the message is sent, read and sent back by the second computer, but not received by the first computer. So basically it works up until the first computer uses rednet.receive(), which is directly after sending the username at the beginning.

Here is the code for the FIRST computer:

for r,s in pairs(rs.getSides()) do
  if peripheral.getType(s) == "monitor" then
	m = peripheral.wrap(s)
	break
  end
end
mside = nil
for r,s in pairs(rs.getSides()) do
  if peripheral.getType(s) == "modem" then
	mside = s
  end
end
print("Modem detected on "..mside.." side.")
--------------------------------------------------
-- Asking for information
rednet.open(mside)
print("Please click a player detector")
event, username = os.pullEvent("player")
rednet.send(3, username)
sleep(1.2)
print("Awaiting response from the server...")
rednet.close("right")
-- Printing response:
rednet.open(mside)
id, spins = rednet.receive()
print("")
print("You have "..spins.." spins left!")
error()
Here is the code on the SECOND computer:


for r,s in pairs(rs.getSides()) do
  if peripheral.getType(s) == "modem" then
	mside = s
	break
  end
end
print("Modem detected on "..mside.." side")

while true do
  rednet.open(mside)
  id, username = rednet.receive()
	if id == 2 then
	  if fs.exists(username) then
		print("File found. Sending Data...")
		f = fs.open(username, "r")
		data = f.readLine()
		f.close()
		rednet.send(2, data)
		sleep(0.5)
		print("Data sent.")
	  else
		print("File not found. Creating new file to send...")
		f = fs.open(username, "w")
		f.write("0")
		f.close()

		f = fs.open(username, "r")
		data = f.readLine()
		f.close()
		rednet.send(2, data)
		print(data.." Sent.")
	  end
	  --elseif id == 3?
	end


  sleep(0.01)
end
Note: Computer 2 must run the program first, then computer 1 must run, then the player clicks a player detector adjacent to computer 1 (MiscPeripherals Mod for CC) which sends a message to computer 2, who will send a variable back to Computer 1 (which is where the problem occurs, the message doesn't ever seem to be received). Ask if you still don't understand what's going on!

Thanks in advance, Imprivate
Edited on 04 January 2014 - 09:21 AM
wieselkatze #2
Posted 04 January 2014 - 10:44 AM
Your problem is the "sleep(1.2)" in line 20. That's all.
The server has no sleep in the loop and sends the answer immediately, but the computer 1 is still in the sleep(1.2).
Edited on 04 January 2014 - 09:44 AM
CometWolf #3
Posted 04 January 2014 - 10:47 AM
After a quick look, i'd say the most likely explaination is the sleep(1.2) between when the first computer sends the message, before receiving. If the message is sent during this sleep, it will be missed.

Edit: lol ninja
Edited on 04 January 2014 - 09:48 AM
Imprivate #4
Posted 04 January 2014 - 12:09 PM
Thank you all! This worked, silly error ;)/>