phazeonphoenix Posted September 24, 2013 Posted September 24, 2013 Oh, my mistake. When I wrote that code snippit I failed to add event to the beginning of the os.pullevent line. Try this: event, id, message = os.pullEvent("rednet_message") Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 HURRAH! It works like a charm! So, now if I want to add more computers, and have there stats displayed, all I have to do is copy paste this code again, and change the "cursorPos", id, and the message. Lets say I added the buffer state. the program would look something like this? rednet.open("top") local engine_state = false mon = peripheral.wrap("back") while true do event, id, message = os.pullEvent("rednet_message") if id == 17 then if message == "engines on" then engine_state = true elseif message == "engines off" then engine_state = false end end mon.clear() mon.setCursorPos(5,5) if engine_state == true then mon.write("Engines: On") else mon.write("Engines: Off") end local buffer_state = false while true do event, id, message = os.pullEvent("rednet_message") if id == 10 then if message == "buffer drain" then buffer_state = true elseif message == "buffer retain" then buffer_state = false end end mon.clear() mon.setCursorPos(5,7) if buffer_state == true then mon.write("Buffer: Drain") else mon.write("Buffer: Retain") end end end Quote
phazeonphoenix Posted September 24, 2013 Posted September 24, 2013 rednet.open("top") local engine_state = false local buffer_state = false mon = peripheral.wrap("back") while true do event, id, message = os.pullEvent("rednet_message") if id == 17 then if message == "engines on" then engine_state = true elseif message == "engines off" then engine_state = false elseif message == "Buffer: Drain" then buffer_state = true elseif message == "Buffer: Retain" then buffer_state = false end end mon.clear() mon.setCursorPos(5,5) if engine_state == true then mon.write("Engines: On") else mon.write("Engines: Off") end mon.setCursorPos(5,7) if buffer_state == true then mon.write("Buffer: Drain") else mon.write("Buffer: Retain") end end The issue with your example is that you created two endless loops and only one could be active at a time. All of your computers sending and receiving messages via rednet should use the same rednet id. Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 Ah, I see your point. How do I change the rednet id's of my computers? Every computer has a different id, the only way I can think of to make the id's the same is to use one computer, which is not possible in this case. Quote
phazeonphoenix Posted September 24, 2013 Posted September 24, 2013 in your sender's code: rednet.send(21,"engines on") That first argument is what id it's being sent by. Pick a number and just use it. The idea is one... implementation of rednet communication would use one rednet id, another (e.g. a completely separate and independent from this status monitor project) implementation would use another. Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 Oh, I thought that "21" was the id of the computer to send it to. Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 Because what is happening now is that the main computer is only recognizing the message being sent to it from computer 17. And all the other computers are sending to computer 21 (the main computer). So we need the main computer to recognize messages from multiple ids (computer numbers) Quote
phazeonphoenix Posted September 24, 2013 Posted September 24, 2013 I think since there isn't a straight forward way to get the computer ID and that the computer ID can change if the computer is dismantled and replaced they opted for a different system of identifying the intended recipient. Rednet is a message bus meaning that it doesn't send to specific PCs it sends to any computer connected to the rednet network and each computer either reacts to it or doesn't depending on the code running on it. Quote
phazeonphoenix Posted September 24, 2013 Posted September 24, 2013 Because what is happening now is that the main computer is only recognizing the message being sent to it from computer 17. And all the other computers are sending to computer 21 (the main computer). So we need the main computer to recognize messages from multiple ids (computer numbers) What's happening is that only the one computer is sending it's update messages on rednet id (aka channel) 17 and that's the only one the monitor computer is listening to. You could make it listen to multiple but that's not necessary here. Just have all of your sending pcs use the same rednet id and you'll be fine. Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 My engine computer sends like this rednet.send(21,"engines on") and that computer's id is 17 Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 here is the wiki page http://computercraft.info/wiki/Rednet.send Quote
phazeonphoenix Posted September 24, 2013 Posted September 24, 2013 Ok my mistake yet again. Contrary to what it may seem I'm not a CC programming expert. I hadn't done all my research on rednet. See This. You are correct that is the ID for the destined PC. There's two ways to fix this. event, id, message = os.pullEvent("rednet_message") if id == 17 then -- do things elseif id == 21 then -- do other things end or if you use rednet.broadcast instead of rednet.send in your sending computers that WILL send to all attached computers. If you go this route remove the if id == whatever then line and one of the ends so that you're no longer checking what id contains. Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 I am using the broadcast method. The engine status seems to be working fine, but the buffer state does not. It is recognizing the signal, but it does not change what is displayed. Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 Here are my current codes main code rednet.open("top") local engine_state = false local buffer_state = false mon = peripheral.wrap("back") while true do event, id, message = os.pullEvent("rednet_message") if message == "engines on" then engine_state = true elseif message == "engines off" then engine_state = false elseif message == "Buffer: Drain" then buffer_state = true elseif message == "Buffer: Retain" then buffer_state = false end mon.clear() mon.setCursorPos(5,5) if engine_state == true then mon.write("Engines: On") else mon.write("Engines: Off") end mon.setCursorPos(60,5) if buffer_state == true then mon.write("Buffer: Drain") else mon.write("Buffer: Retain") end end Engine send code rednet.open("left") while true do shell.run("clear") if rs.getInput ("back") then rednet.broadcast("engines on") else rednet.broadcast("engines off") end os.pullEvent("redstone") end Buffer send code rednet.open("top") while true do shell.run("clear") if rs.getInput ("front") then rednet.broadcast(21,"buffer drain") else rednet.broadcast(21,"buffer retain") end os.pullEvent("redstone") end end Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 oh my bad, the buffer broadcast is in send format. My bad. :/ Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 but, it still doesn't work after I fixed it. Quote
phazeonphoenix Posted September 24, 2013 Posted September 24, 2013 Remember that what a sender sends and what the monitor is testing for should be the same. Your buffer sender sends "buffer drain" and your monitor is looking for "Buffer: Drain". It's case sensitive. You could send anything really just as long as it matches the other side. Remove the 21, from your broadcast calls in the buffer sender's code. Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 And presto, it works! Thanks. So now I think I can use this method to add as many computers as I wish. Thank you! Quote
Reece C. Posted September 24, 2013 Author Posted September 24, 2013 Now all I have to figure out is how to make it look nice, you know, with lines ------------------ and stuff. Hope that won't be too difficult. Quote
phazeonphoenix Posted September 24, 2013 Posted September 24, 2013 Glad to help. CC is one of those mods that people either love or stay away from like the plague. You have to at least be able to make yourself think like a programmer for a short period of time. Quote
phazeonphoenix Posted September 25, 2013 Posted September 25, 2013 Could you post the solution that worked so we can say we contributed to the forum? Quote
Viktor_Berg Posted September 25, 2013 Posted September 25, 2013 As I understand it, the ComputerCraft Wireless RedNet allows computers to broadcast messages to eachother, and the Wired modems/cables function in the same manner. The MineFactory Reloaded RedNet cables function like RedPower 2 Bundled cables, and ComputerCraft includes an API to access all 16 channels of the cables through 1 connection. So essentially I'm saying it would be easiest to use analog wired redstone cables as opposed to Computercraft networking. That's how I'd do it. I have to add: easiest, but not best. With rednet cabling, you have 16 channels of data, but with CC rednet, you can send and receive ANY message, which is far more flexible. Just sayin'. Quote
Reece C. Posted September 25, 2013 Author Posted September 25, 2013 That's what I suspected. So I can easily just use wireless without having to change any code right? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.