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

From Capacitor bank to ComputerX to ComputerY (OpenP)

Started by YoShIWoZ, 22 April 2014 - 12:54 PM
YoShIWoZ #1
Posted 22 April 2014 - 02:54 PM
Hello guys, i'm back with one more problem that i can not find a solution to (Searched the webs forever)
It's about wireless modem and the use of Rednet/pullEVent
So i've set up a test system where i'm trying to get this to work:
1 capacitor bank with energy in it. - 1 computer(X) besides it with wirless modem - 1 computer(Y) with wireless modem and a monitor.
So computer(X) takes the data from the bank, then sends it to computer(Y) which then sends the data to the monitor.
Now what i can't figure out, is how to cut the diffrent variables out when on the second computer, since it all comes as text.
So i've set up the "layout" of the data on computer(X) and then just using computer(Y) mon.write(message) which is not very flexible. as i'd have to do all the layouting on the computer by the bank, instead of the computer in my house.
This is the code i've used:
Computer(X)

per = peripheral.wrap("left")
local function round( num, places )
  places = 10 ^ places
  return math.floor((num * places) + 0.5 ) / places
end
while true do
  energy = per.getEnergyStored("left")
  energyMax = per.getMaxEnergyStored("left")
  sleep(1)
  rednet.open("right")
  rednet.send(7, round(energy/1000000, 1).."M".." of "..round(energyMax/1000000, 1).."M".." = "..round(energy/energyMax*100, 1).."%")
  print(round(energy/1000000, 1),"M")
end
Computer(Y)

mon = peripheral.wrap("top")
function listen()
while true do
		rednet.open("right")
		id,message = rednet.receive()
		if id == 6 then --3 is the Id of the main pc
		 mon.setCursorPos(1,1)
		 mon.write("------Energy Bank 1------")
		   mon.setCursorPos(1,2)
		   mon.clearLine()
		   mon.write("Stored Energy: "..message)
		end
	end
end
while true do
listen()
end
CometWolf #2
Posted 22 April 2014 - 04:00 PM
Normally you'd use string.match or string.sub to get specific bits of a string, but in this case it'd be way easier to just drop the string altogether. You can send tables via rednet, so just store the info under corresponding indexes and send the whole table. Then extract them from their respective indexes on the other side.
YoShIWoZ #3
Posted 22 April 2014 - 05:03 PM
Ah, thanks for the feedback, i tried using tables, must admit it's the first time i've ever used tables before.
So i can't get it to work. I've tried a fewthings to fix this.
This is what i've tried, without luck: (It might be entirely wrong.)
So i as a starter just wanted to try to extract the info out of the tables and tested with mon.write. :)/>
Computer(X)
per = peripheral.wrap("left")
while true do
	 energy = per.getEnergyStored("left")
	  energyMax = per.getMaxEnergyStored("left")
	  function energyTable()
		  a[energySend] = energy
		  a[energyMaxSend] = energyMax
	  end
	 sleep(1)
	 rednet.open("right")
	 rednet.send(7, EnergyTable())
end
Computer(Y)
mon = peripheral.wrap("top")
-- saving this function for when i have gotten the tables fixed.
local function round( num, places )
  places = 10 ^ places
  return math.floor((num * places) + 0.5 ) / places
end

while true do
	rednet.open("right")
	id,message = rednet.receive()
	if id == 6 then --3 is the Id of the main pc
		function energyTable(message)
			print(a[energySend])
			print(a[energyMaxSend])
		end
		mon.setCursorPos(1,1)
		mon.write("------Energy Bank 1------")
		mon.setCursorPos(1,2)
		mon.clearLine()
		mon.write("Stored Energy:"..energyTable())
	end
end
Edited on 22 April 2014 - 03:04 PM
CometWolf #4
Posted 22 April 2014 - 05:30 PM
Yeah, that's not really how you use tables :P/>

t = {} --defines the table
t[index] = value --store a value
There's a couple of ways of doing the above, but this is the most important bits, so we'll stick to that for now :P/>


t = {}
t["energyMax"] = getEnergyMax() --or whatever, you get the idea
rednet.send(id,t)

local id,message = rednet.receive()
print(message["energyMax"])
YoShIWoZ #5
Posted 22 April 2014 - 06:23 PM
Yeah, that's not really how you use tables :P/>

t = {} --defines the table
t[index] = value --store a value
There's a couple of ways of doing the above, but this is the most important bits, so we'll stick to that for now :P/>


t = {}
t["energyMax"] = getEnergyMax() --or whatever, you get the idea
rednet.send(id,t)

local id,message = rednet.receive()
print(message["energyMax"])
Thanks a ton CometWolf!
I will just reply with the code i ended up with (working) so others see what i did if they get in the same situation. :)/>
Computer(X) <– The one besides the EnderIO Capacitor Bank

per = peripheral.wrap("left")
while true do
  energy = per.getEnergyStored("left")
   energyMax = per.getMaxEnergyStored("left")
   t = {}
   t["energyMax"] = per.getMaxEnergyStored("left")
   t["energy"] = per.getEnergyStored("left")
sleep(1)
rednet.open("right")
rednet.send(7, t)
end
Computer(Y) <– The one that has monitor(s) on top of it.

mon = peripheral.wrap("top")
local function round( num, places )
  places = 10 ^ places
  return math.floor((num * places) + 0.5 ) / places
end
while true do
	rednet.open("right")
	id,message = rednet.receive()
	if id == 6 then --3 is the Id of the main pc
		mon.setCursorPos(1,1)
		mon.write("------Energy Bank 1------")
		mon.setCursorPos(1,2)
		mon.clearLine()
		mon.write("Stored Energy: "..round(message["energy"]/1000000, 1).."M".." of "..round(message["energyMax"]/1000000, 1).."M".." = "..round(message["energy"]/message["energyMax"]*100, 1).."%")
	end
end
Edited on 22 April 2014 - 04:24 PM