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

Wireless Rednet with monitor issue.

Started by GsnShadow, 26 January 2013 - 06:01 PM
GsnShadow #1
Posted 26 January 2013 - 07:01 PM
Below are my server(which is hooked up to the monitor) and my client(MiningTurtle).

So I have tested the ability to send and parse the data and that works fine.

My setup is a big monitor hooked up to a computer(server) with a modem and I have two wireless turtles with the same program with minor numeric changes and obviously different labels to tell them apart. On the server I run. monitor top draw2

if I run the program on turtleA it shows up correctly at the top position. If I then run the program on turtleB it shows up in the correct lower position , but with the information from turtleA.

If I try the reverse. Starting with TurtleB it will show up correclty in the lower position with TurtleB info. If I then run Turtle A it is in the top position but with TurtleB info. So it knows where to put the message but doesnt seem to update the message. Been trying to figure this out for a couple hours and would like it if anyone could look at it with a fresh pair of eyes. Also not a pro programmer so any tips or advice is greatly appreciated.

The goal of this program is to be able to have multiple turtles running and a central location to see the progress and fuel levels.


Again thank you


========================================================

--[[
  File:	  draw2
  Version:   1.0
  Purpose:   A utiltiy for monitoring multiple turtles from a central location
  Author(s): GsnShadow
]]
(server) --draw2

local w,h = term.getSize() --term size
a= {}  --defining the array
n=1 --defining number for array
sh=0  --screen height offset
function netClose()
	rednet.close("right")
end
function netOpen()
netClose()
rednet.open("right")
end
function getData()
id, message, distance = rednet.receive()
end
function assignData()
netOpen()
getData()
--id, message = rednet.receive()
if id == 20 then
--T1m = message
sh=0
drawScreen()
elseif id == 30 then
--T2m = message
sh=5
drawScreen()
elseif id == 33 then
--T3m = message
sh=10
drawScreen()
end
end
function tClear()
term.clear()
end
function drawScreen()
local s = message
for token in string.gmatch(s,"[^%s]+") do
a[n] = token
n=n+1
end
name = a[1]
fuelLevel = a[2]
fuelNeeded = a[3]
collected = a[4]
blocks = a[5]
--print(name)
--print(fuel)
--print(fuelNeeded)
--print(collected)
--print(blocks)
iFuelLevel = tonumber(fuelLevel)
iBlocks = tonumber(blocks)
if iFuelLevel < iBlocks then
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
end
fuel = " Fuel: " .. fuelLevel
term.setCursorPos(1,1+sh)
term.write(name)
term.setCursorPos(w - #fuel+1, 1 + sh)
term.write(fuel)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
   -- term.setCursorPos(1,2+sh)
   -- term.write(" Size:   " .. length .. "L x " .. width .. "W x " .. height .. "H")
term.setCursorPos(1,3+sh)
term.write(" Volume: " .. collected .. " of ".. blocks .. " Blocks") --temp
iFuelLevel = tonumber(fuelLevel)
iBlocks = tonumber(blocks)
if iFuelLevel < 1 then
term.setCursorPos(1,5+sh)
term.write(" Oh Noes! I ran out of fuel!")
elseif iFuelLevel < iBlocks then
term.setCursorPos(1,5+sh)
term.write(" I may not be able to finish the task: Low Fuel ")
end
s = ""
end

while true do
assignData()
end


===========================================================================


--[[
  File:	  test
  Version:   1.0
  Purpose:   A test program for draw2
  Author(s): GsnShadow
]]
(client)  -- I just set up a test file with plugged in data for the most part
rednet.open("right")
local label = os.getComputerLabel()
local computerID = 32
local fuel = turtle.getFuelLevel()
local fuelNeeded = 3200
local collected = 10
local message = label .. " " .. fuel .. " " .. fuelNeeded .. " " .. collected .. " " .. 2500
rednet.send(computerID, message)
Lyqyd #2
Posted 27 January 2013 - 07:46 AM
Split into new topic.
GsnShadow #3
Posted 28 January 2013 - 06:18 AM
Thanks
GsnShadow #4
Posted 28 January 2013 - 02:50 PM
bump ^^ :unsure:/>
ChunLing #5
Posted 28 January 2013 - 05:13 PM
Does message ever update under any circumstances?

Your use of globals is somewhat uncontrolled, particularly given some of the oddities of function arrangement. If a function is only used in one place, it probably doesn't need to be a function (if it isn't used at all, it definitely doesn't need to be a function). Single use global functions can have a negative impact on readability and reliability, particularly if they aren't arranged by dependency.
GsnShadow #6
Posted 28 January 2013 - 05:52 PM
From my tests the message never does update. The tClear was a test to see if clearing the screen worked I never removed it. The rednet open and closing I will probably remove and just call them directly. What ever the 1st message was displays, but any call after that will display the 1st message in the spot where the 2nd message should be.

Is there a better way to arrange them. i am still learning on the coding side of things. Any help is greatly appreciated.
ChunLing #7
Posted 28 January 2013 - 06:11 PM
Okay, I rearranged the code to remove all the functions (since I couldn't see any cases where a function was used more than once). That helped me spot the problem.
Spoiler
--[[
  File:   draw2
  Version:   1.0
  Purpose:   A utiltiy for monitoring multiple turtles from a central location
  Author(s): GsnShadow
]]

local w,h = term.getSize() --term size
a= {}  --defining the array
n=1 --defining number for array
sh=0  --screen height offset
rednet.open("right")
while true do
    id, message, distance = rednet.receive()
    if id == 20 then
        sh=0
    elseif id == 30 then
        sh=5
    elseif id == 33 then
        sh=10
    end
n=1 --defining number for array
    for token in message:gmatch("[^%s]+") do
        a[n] = token
        n=n+1
    end
    name = a[1]
    fuelLevel = a[2]
    fuelNeeded = a[3]
    collected = a[4]
    blocks = a[5]
    iFuelLevel = tonumber(fuelLevel)
    iBlocks = tonumber(blocks)
    if iFuelLevel < iBlocks then
    term.setBackgroundColor(colors.white)
    term.setTextColor(colors.black)
    end
    fuel = " Fuel: " .. fuelLevel
    term.setCursorPos(1,1+sh)
    term.write(name)
    term.setCursorPos(w - #fuel+1, 1 + sh)
    term.write(fuel)
    term.setBackgroundColor(colors.black)
    term.setTextColor(colors.white)
       -- term.setCursorPos(1,2+sh)
       -- term.write(" Size:   " .. length .. "L x " .. width .. "W x " .. height .. "H")
    term.setCursorPos(1,3+sh)
    term.write(" Volume: " .. collected .. " of ".. blocks .. " Blocks") --temp
    iFuelLevel = tonumber(fuelLevel)
    iBlocks = tonumber(blocks)
    if iFuelLevel < 1 then
    term.setCursorPos(1,5+sh)
    term.write(" Oh Noes! I ran out of fuel!")
    elseif iFuelLevel < iBlocks then
    term.setCursorPos(1,5+sh)
    term.write(" I may not be able to finish the task: Low Fuel ")
    end
end
Basically, you weren't resetting n, the variable used to index the table where you were storing all the contents of message. So with each new message, the table was just getting bigger, but you were always only printing out the first set of elements. I moved the initialization of n to inside the loop, right before it is used in the gmatch loop.

I don't know if the code still works after being surgerized so crudely, but if you're original code was working then moving the initialization of n to be right before the gmatch loop should fix the problem.
GsnShadow #8
Posted 28 January 2013 - 06:31 PM
Awesome I will test it out and post back.. Thank you very much for taking the time and helping.
GsnShadow #9
Posted 28 January 2013 - 06:35 PM
Just tested and it works!!!!!!! Thank you so much