21 posts
Posted 18 January 2013 - 04:10 PM
I've tried to write a simple program that lets the user enter a computers ID then a message and send it. It should be stupidly simple but I can't figure out variables so I need some help. This is the code I tried to use:
print("Target ID:")
local input = read()
print("Message:")
local input2 = read()
rednet.send(input, "input2") – I want the inputs to get stuck in here automaticaly but don't know how
Any ideas?
1111 posts
Location
Portland OR
Posted 18 January 2013 - 04:13 PM
The id needs to be a number read outputs a string so you need to tonumber() the read
print("Target ID:")
local input = tonumber(read())
print("Message:")
local input2 = read()
rednet.send(input, "input2")
7508 posts
Location
Australia
Posted 18 January 2013 - 04:15 PM
your so close.
rednet.send( tonumber( input ), input2 )
tonumber turns the string that the user has entered into a number ( which is what rednet.send needs )
NOTE: Also dont forget to open and close rednet with
rednet.open( "left" ) -- replacing left with the side the modem is on
rednet.close( "left" ) -- replacing left with the side the modem is on
2088 posts
Location
South Africa
Posted 18 January 2013 - 05:41 PM
rednet.send(input, "input2") – I want the inputs to get stuck in here automaticaly but don't know how
When sending a variable, exclude the quotation marks.
1619 posts
Posted 18 January 2013 - 06:11 PM
Off-Topic: Why do you need to close rednet? I never do it, and I can't see a reason to.
On-Topic = nil
1111 posts
Location
Portland OR
Posted 18 January 2013 - 06:15 PM
Off-Topic: Why do you need to close rednet? I never do it, and I can't see a reason to.
I never close mine either, in fact on SSP I have autorun program setup that opens rednet on any computer with a modem attached at startup.
7508 posts
Location
Australia
Posted 18 January 2013 - 06:15 PM
Off-Topic: Why do you need to close rednet? I never do it, and I can't see a reason to.
Its good practice to close anything you open. And if you get in the habit of opening stuff without closing it you may start forgetting to close things that NEED closing, like file handles.
2005 posts
Posted 18 January 2013 - 08:38 PM
There are performance issues that can arise for computers/turtles that have modems open in an area cluttered with rednet broadcast spam. If you don't have a lot of rednet broadcast spam, then this makes little difference, but if you have a lot of turtles using a GPS system, then it does help to have them close their modems when they aren't locating themselves.
It's also good practice, but not so good that I bother to do it for no real reason.
53 posts
Location
Adelaide, Australia.
Posted 19 January 2013 - 01:09 AM
I wrote a program called Redsend to run a program from a remote computer…
function cmod()
write("Modem Side: ")
local cmodi = read()
if rednet.isOpen(cmodi) == true then
rof = "ONLINE"
else
rednet.open(cmodi)
end
end
function receive()
local id, msg = rednet.receive()
rednet.send(tonumber(id), "Received")
shell.run(tostring(msg))
print("Running ", msg)
end
function send()
write("Computer ID: ")
local sendid = read()
write("Program: ")
local pinput = read()
rednet.send(tonumber(sendid), tostring(pinput), false)
sleep(1)
local id1 = rednet.receive()
if id1 == sendid then
print("Return: Sent")
else
print("Failed: No return")
end
end
cmod()
print(rof)
parallel.waitForAny(send, receive)
21 posts
Posted 19 January 2013 - 04:26 PM
Thanks. That explains why I kept getting the "expected positive number" message. I didn't know that you had to define what kind of variable it was.