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

rednet error 87

Started by shiskebab, 24 July 2015 - 04:29 PM
shiskebab #1
Posted 24 July 2015 - 06:29 PM
Been working too long on fixing this, so now i would wery much appreciate that someone more experienced take a look at this.

The goals of this was a proof of concept type of thing:

- send a message from a client computer to an idle server
- make server save the message, and rebroadcast the contents of the file back to the client

Client:


local function Startup()
  term.clear()
  term.setCursorPos (1,1)
  print ("   this Computer's id: ", os.getComputerID())
  print ("   Set receiving computer's id:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  print ()
  print ("   message:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  term.setCursorPos(5,4)
  repeat
	local compid = tonumber(read())
  until compid
  term.setCursorPos(5,4)
  print ("id saved")
  term.setCursorPos(5,9)
  local alv3 = read()
  term.setCursorPos(5,9)
  print ("message saved")
  sleep(0.5)
  Sender()
  sleep(2)
end
function Sender()
  term.clear()
  term.setCursorPos (1,1)
  print ("sending message in: ")
  print ("3")
  sleep(1)
  print ("2")
  sleep(1)
  print ("1")
  rednet.open("right")
  sleep(1)
  rednet.send(tonumber(compid), alv3)
  sleep(1)
  rednet.close("right")
  print ("message sent")
  sleep(1)
  Startup()
end
function Receive()
  term.clear()
  term.setCursorPos (1,1)
  print ("   this Computer's id: ", os.getComputerID())
  print ("   waiting for message:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  print ()
  print ("   Terminal:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")

  while true do
   rednet.open("right")
   senderID, message = rednet.receive()
	 term.setCursorPos (5,4)
  print (message)
  sleep(0.5)
  term.setCursorPos (5,9)
  print (senderID)
  local event = os.pullEvent("key")
  Startup()
  break
  end
  end
Startup()

Server Code:


local function Startup()
  term.clear()
  term.setCursorPos (1,1)
  print ("   this Computer's id: ", os.getComputerID())
  print ("   Set this Computer's label:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  print ()
  print ("   Set The Computer Terminal's ID")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  term.setCursorPos(5,4)
  local IgnLabel = read()
  os.setComputerLabel(IgnLabel)
  term.setCursorPos(5,4)
  print ("Label successful")
  term.setCursorPos(5,9)
  local compid = tonumber(read())
  term.setCursorPos(5,9)
  print ("ID saved")
  sleep(2)
  print ("storing message")
  WriteFile()
  sleep(1)
  print ("sending stored messages")
  SendFile()
  print ("message sent!")
  sleep(5)
  Startup()
end
function WriteFile()
  rednet.open("right")
  repeat
  local id, msg = rednet.receive()
  until id == compid
  rednet.close()
  if fs.exists("messages") == false then
  fs.makeDir("messages")
  local msgs = fs.open("messages", "a")
  msgs.write(msg)
  msgs.close()
   elseif fs.exists("messages") == true then
   local msgs = fs.open("messages", "a")
   msgs.write(msg)
   msgs.close()
  else
  term.clear()
  term.setCursorPos(1,1)
  print ("failed to write file, or receive message")
  sleep(3)
  Startup()
  end
end
function SendFile()
  local SendIt = fs.open("messages", "r")
  local shipment = SendIt.readAll()
  SendIt.close()
  rednet.open("right")
  rednet.send(compid, shipment)
  rednet.close("right")
end
Startup()

There might be some unforseen bugs on both programs. I haven't been able to test it any farther than the client failing to send the message. Any help and tips are appreciated, also if you want to elaborate or go into depth on tips&tricks when coding that would make my code better/easier please keep it ELI5. I'm not a good programmer :P/>
Edited on 24 July 2015 - 04:30 PM
KingofGamesYami #2
Posted 24 July 2015 - 06:35 PM
I'd say don't close rednet. Once it's open, leave it open.
shiskebab #3
Posted 24 July 2015 - 06:49 PM
hmm I always thought you had to close it after, like the fs.close() function after you edit a file :P/>

anyway, tried moving rednet.open to the Startup() function, and the rednet.close() to the last part of the Receive() function. It didn't work though, i still get the expected number 87 error

** should be noted that the tonumber() function is a little all over the place. I have tried adding and removing it to the value, and the rednet.send() back an forth. There was no difference.
** holy shit just realised my rednet.send() ** might be calling a value that doesnt exist
** oh god, i have no idea what im looking for :rolleyes:/>
Edited on 24 July 2015 - 05:04 PM
KingofGamesYami #4
Posted 24 July 2015 - 07:04 PM

  repeat
        local compid = tonumber(read())
  until compid

…makes compid local to the repeat loop. Instead, do something like this:

  local compid
  repeat
        compid = tonumber(read())
  until compid

You may want to read up on Scope (BB I know you have the link for this somewhere)
shiskebab #5
Posted 24 July 2015 - 07:17 PM
hmm didn't know it did that.

Added the changes, still same error. Have no idea what scope is, but if you link i'll read up on it
shiskebab #6
Posted 24 July 2015 - 08:23 PM
Is this the tutorial ? http://www.computercraft.info/forums2/index.php?/topic/16700-lua-basics-variable-scope-code-blocks-and-control-structures/

atleast it might answer some questions as to why i cant assign any proper values :P/>
Balthamel #7
Posted 24 July 2015 - 08:24 PM
Is this the full code you're using? the number in an error is usually the line that the error is on, since this code is apparently 63 lines long…
Can you pastebin the code?
Is the error in the server or client code?
KingofGamesYami #8
Posted 24 July 2015 - 08:39 PM
@Balthamel - rednet is one of those APIs that can error. Line 87 is a peripheral call to a modem, inside the 'send' function. Really, the send function should check the input before attempting to send the message, but it doesn't, hence the ambiguous error.
Balthamel #9
Posted 24 July 2015 - 08:54 PM
Ah, damn random functions. but good to know :)/>
shiskebab #10
Posted 24 July 2015 - 09:24 PM
Ok got it to work, followed the tutorial i posted earlier and called the local values at the first two line of code in the program, instead of them being called locally inside the code block and then magically being scooped up by the garbage collector :P/>

with that fix I dont get the error 87 anymore thx guys !
shiskebab #11
Posted 24 July 2015 - 09:43 PM
If anyone caught my victory celebration dont run away yet!

found another issue, this time with the server portion. The foolish whench claims I'm attempting to index a nil value!? (line 46) ((the writefile function, and fs portion))


local compid
local function Startup()
  term.clear()
  term.setCursorPos (1,1)
  print ("   this Computer's id: ", os.getComputerID())
  print ("   Set this Computer's label:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  print ()
  print ("   Set The Computer Terminal's ID")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  term.setCursorPos(5,4)
  local IgnLabel = read()
  os.setComputerLabel(IgnLabel)
  term.setCursorPos(5,4)
  print ("Label successful")
  term.setCursorPos(5,9)
  repeat
		compid = tonumber(read())
  until compid
  term.setCursorPos(5,9)
  print ("ID saved")
  sleep(2)
  print ("storing message")
  WriteFile()
  sleep(1)
  print ("sending stored messages")
  SendFile()
  print ("message sent!")
  sleep(5)
  Startup()
end
function WriteFile()
  rednet.open("right")
  id, message = rednet.receive()
  if id == compid then
   rednet.close()
   if fs.exists("messages") == false then
	fs.makeDir("messages")
	  local h = fs.open("messages", "a")
		  h.write(message) -- claims i am attempting to add a nil value here ?
		  h.close()
	 elseif fs.exists("messages") == true then
	  local h = fs.open("messages", "a")
		  h.write(message)
		  h.close()
	 else
	  term.clear()
	  term.setCursorPos(1,1)
	  print ("failed to write file, or receive message")
	  sleep(3)
	  Startup()
  end
  else
  end
end
function SendFile()
  local SendIt = fs.open("messages", "r")
  local shipment = SendIt.readAll()
  SendIt.close()
  rednet.open("right")
  rednet.send(compid, shipment)
  rednet.close("right")
end
Startup()

** i should probably add this stuff to pastebin instead
Edited on 24 July 2015 - 07:44 PM
KingofGamesYami #12
Posted 24 July 2015 - 09:54 PM
Note: Attempting to open a folder as if it was a file won't work. You literally make sure it's a directory before opening it.
shiskebab #13
Posted 24 July 2015 - 10:03 PM
So whats wrong is that i am making a directory, and then trying to open a file thats not there ?

If so how do i create a file, and how do i make it save the message in "id, message = rednet.receive()" ?

** ok hold on I'm not as stupid as i sound, rewrote some stuff checking now
** omg it actually works now! ALL BUGS HAVE BEEN PURGED

link to the finished code: http://pastebin.com/vvmMs6b7

still needs tweaking, but atleast it works now
Edited on 24 July 2015 - 08:21 PM
KingofGamesYami #14
Posted 24 July 2015 - 10:59 PM
So whats wrong is that i am making a directory, and then trying to open a file thats not there ?

No. If you were to open a non-existent file in append mode, it would work. You are trying to open a directory, which won't work.

Glad to hear you fixed it though.
shiskebab #15
Posted 25 July 2015 - 12:38 AM
yeah actually i was so extatic the client was working that i didn't realise all the other errors my program had (oh man getting into computercraft after a year is rough).

so here I am several hours and hundreds of bugtests later:

http://pastebin.com/r7PcfDHu

finished code, working as intended. It sets up a client and server program. Configure the server first as it will wait for the client to send a message, the server will then store the message and send it back. The message and id of the server should be visible in the Client program when the proccess is done. All this might seem like a big hassle, but since all of it works i can paste parts of the program that i need later and know that it works. :D/>

** grammar
Edited on 24 July 2015 - 10:39 PM