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

need help sending tables across rednet

Started by margeobur, 17 May 2013 - 12:00 AM
margeobur #1
Posted 17 May 2013 - 02:00 AM
Hello Pros

I know you've probably answered lots of questions about sending tables but I really can't figure this out. I'm trying to make a program for wireless mining turtles. I want a computer to be able to send commands and receive messages to/from the turtle. One of the things I want the computer to do is ask for the turtle's status in the form of a table that it will print.

Here are the functions for sending the table from the turtle:

local function getStatus()
  local x, y, z = gps.locate(4)
  if not x then
	x, y, z = "Could not locate", "Could not locate", "Could not locate"
   end
  -- local fuel = turtle.getFuelLevel()
  stage = "Testing"					-- this was so that I was sure it had a value
  local inv_room = 0
  for i = 4, 9 do
	inv_room = inv_room + turtle.getItemSpace(i)
  end
  local tStatus = { blocks_mined, stage, inv_room, x, y, z }

return tStatus
end

local function receive()
  while true do
	local sender, message, distance = rednet.receive()
	if sender ~= monitorID then
	  rednet.send(sender, "You are not my monitor!")
   end

	if message == "CheckStatus" then
	 local Status = getStatus()
	 print("Serialising and sending")
	 local sStatus = textutils.serialize(Status)		   -- these seem to run fine, they don't come up
	 rednet.send(monitorID, sStatus, true)			  -- with any errors at all

	 for key, value in ipairs(Status) do print(key..": "..value) end  -- this does not even print, so the
                                             --problem is with getStatus() I'm sure...
  elseif message == "StopMining" then
	 rednet.send(monitorID, "Stopping")
	 command = 2
  break

  else rednet.send(monitorID, "Unknown command")
  end

  end
  return
end 

this is the computer's function for getting the status:

local function askForStatus()
  print("Asking for status...")

  rednet.send(turtleID, "CheckStatus", true)
  senderID, sStatus, distance = rednet.receive()

  if senderID ~= turtleID then
	rednet.send(senderID, "You are not my turtle!")
  end

  if not sStatus then
	print("Status could not be retrieved.")
	return false
  elseif sStatus then
	print("Status was retrieved. Unserialising...")
	tStatus = textutils.unserialize(sStatus)
  end

  print("tStatus: "..tStatus)

  if tStatus then
	print("A table was received")			 -- these two tests don't print...
  end

	-- for key, value in ipairs(tStatus) do print(key..": "..value) end  
end 

No errors come up, tStatus just seems to be nil so the monitor does not print the tests…
If someone could kindly tell me what I am doing wrong that would be greatly apreciated.
remiX #2
Posted 17 May 2013 - 02:04 AM
When doing Status = getStatus()
add this after:
print( type( Status ) )
tell us what it prints
margeobur #3
Posted 17 May 2013 - 04:44 PM
Well, I did this:
print(Status)
where you told me and it came up with "table: 1dc3fb2"…
darkrising #4
Posted 17 May 2013 - 06:16 PM
This works.

(Excuse the debugging stuff and stuff)



monitorID = 1

local function getStatus()
  local x, y, z = 1,1,1
  if not x then
    x, y, z = "Could not locate", "Could not locate", "Could not locate"
  end
  -- local fuel = turtle.getFuelLevel()
  stage = "Testing"                                     
  local inv_room = 0
  for i = 4, 9 do
    --inv_room = inv_room + turtle.getItemSpace(i)
  end
  local tStatus = {}
  tStatus.blocks_mined = blocks_mined
  tStatus.stage = stage
  tStatus.inv_room = inv_room
  tStatus.x = x
  tStatus.y = y
  tStatus.z = z

return tStatus
end

local function receive()
  while true do
    local sender, message, distance = rednet.receive()
    if sender ~= monitorID then
      rednet.send(sender, "You are not my monitor!")
    end

  if message == "CheckStatus" then
    local Status = getStatus()
    print("Serialising and sending")
    local sStatus = textutils.serialize(Status)              
    rednet.send(monitorID, sStatus, true)                    

    for key, value in ipairs(Status) do 
      print(key..": "..value) 
    end                               
  elseif message == "StopMining" then
         rednet.send(monitorID, "Stopping")
         command = 2
  break

  else rednet.send(monitorID, "Unknown command")
  end

  end
  return
end
rednet.open("right")
blocks_mined = 23
stage = 1
inv_room = 30
receive()


function askForStatus()
  print("Asking for status...")

  rednet.send(turtleID, "CheckStatus", true)
  senderID, sStatus, distance = rednet.receive()

  if senderID ~= turtleID then
    rednet.send(senderID, "You are not my turtle!")
    return false
  end

  if not sStatus then
    print("Status could not be retrieved.")
    return false
  end

  print("Status was retrieved. Unserialising...")
  status = textutils.unserialize(sStatus)

  print(status.blocks_mined) 
  print(status.stage) 
  print(status.inv_room) 
  print(status.x) 
  print(status.y) 
  print(status.z) 
end 

turtleID = 0
rednet.open("right")
askForStatus()
margeobur #5
Posted 17 May 2013 - 10:06 PM
Thanks!!! :D/> This worked, although I think there is a bug in ComputerCraft because if I log out then back in, I can't send a table across at all unless I re-place the turtle (as in the turtle seems to send it, but the computer just receives nil).