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

Cannot exactly send number through rednet

Started by thepowdertoy, 22 October 2012 - 12:16 PM
thepowdertoy #1
Posted 22 October 2012 - 02:16 PM
I'm currently using computercraft 1.33 in my 1.2.5 test world, and this is the program:

Sender:

rednet.open("back")
while true do
i = rs.getBundledInput("left")
y = tostring(i)
rednet.broadcast(y)
print(y)
sleep(0)
end

Receiver:

rednet.open("back")
while true do
y = rednet.receive()
i = tonumber(y)
rs.setBundledOutput("right", y)
print(i)
sleep(0)
end

Wireshark:

rednet.open("back")
while true do
message = rednet.receive()
print(message)
sleep(0)
end

The sender and receiver are used to send and receive bundled cable through wireless, and the wireshark are used to see the message inside the wireless. The problem is, every receiving computer, either it the receiver or the wireshark always receives number 6, either any colored wire that connected to the bundled cable are turned on or not. I don't know if this a bug, or this is my fault at programming, nor I can see this happening in 1.3.2 version, because redpower is not yet updated yet.
Ditto8353 #2
Posted 22 October 2012 - 02:45 PM
rednet.receive() returns the sender ID and then the message
_, y = rednet.receive()
ChunLing #3
Posted 22 October 2012 - 09:17 PM
And you usually want to use the ID to determine whether or not to act on the message, so don't just throw it away. You can use it in a line like:
repeat senderID,message = rednet.receive() until senderID == authorizedID
If you want to check for messages from more than one authorized source, make a table of the authorized ID's and check that.
authorizedID = {[34] = true,[23] = true,[45] = true,[78] = true,[2] = true,}
repeat senderID,message = rednet.receive() until authorizedID[senderID]
Now, only a message from one of the ID's used as keys for the true values in authorizedID will allow you to continue.