Rednet
#1
Posted 16 February 2013 - 03:08 PM
(PS is there a any mistakes in this I'm using speech recognition as one of my hand is broken and I have a splint.)
Here is a more in depth description of my idea:
I want to automate a city. This will all be put on a server with a computercraft server mod files. I want to know how to program was rednet and tables so I can put files into a database. This would be helpful for hospitals fire departments and police departments for medical records fire records and crime records. (again sorry for the bad grammar)
This is helpful for anyone working in a police station to see instantly who has what records. I suppose I could use the book and quill but this is easier and more fun.
Thank you in advance to anyone who helps me. I would very much appreciate this help.
All the best,
viextra
#2
Posted 16 February 2013 - 03:46 PM
Please do not take this in an offensive way!
Sorry, I just wanted you to know.
#3
Posted 16 February 2013 - 04:50 PM
But as to help with tables and rednet, what do you need to know? The easiest way to send tables over rednet would be using textutils like so:
local example = {
"This", "is", "an", "example", "table"
}
local strTable = textutils.serialize(example)
rednet.open("back") --Assuming that you have a modem on the back
rednet.send(1, strTable) --Assuming that you have a computer with ID 1 running the following code
And the listening code
rednet.open("back")
local strTable = rednet.receive()
local example = textutils.unserialize(strTable)
for i=1,#example do
print(example[i])
end
Not too difficult really. There's more info about textutils serialize functions on the wiki.
#4
Posted 18 February 2013 - 05:54 AM
Bubba, on 16 February 2013 - 04:50 PM, said:
But as to help with tables and rednet, what do you need to know? The easiest way to send tables over rednet would be using textutils like so:
local example = {
"This", "is", "an", "example", "table"
}
local strTable = textutils.serialize(example)
rednet.open("back") --Assuming that you have a modem on the back
rednet.send(1, strTable) --Assuming that you have a computer with ID 1 running the following code
And the listening code
rednet.open("back")
local strTable = rednet.receive()
local example = textutils.unserialize(strTable)
for i=1,#example do
print(example[i])
end
Not too difficult really. There's more info about textutils serialize functions on the wiki.
#5
Posted 18 February 2013 - 06:00 AM
#6
Posted 18 February 2013 - 06:05 AM
Bubba, on 18 February 2013 - 06:00 AM, said:
Another thing I would like to ask (and I appreciate you helping me on this
I want to make it so I could send a message, and then if I wanted to, I could save the contents of the file to ANOTHER file (sorry if that is confuzzling) So I could receive a message on what people have killed someone, and save it to a police computer file called "killers" or something.
How would I do that?
#7
Posted 18 February 2013 - 06:20 AM
ViextraProgramming, on 18 February 2013 - 06:05 AM, said:
How would I do that?
So you want to save the contents of the rednet message to a file? Or do want one computer to send a file over rednet to be saved on the second computer?
If the former, then this:
rednet.open("back")
local id, strTable = rednet.receive()
local killers = textutils.unserialize(strTable)
local file = fs.open("killers", "a") --Open the killers file in append mode
for i,v in pairs(killers) do
file.writeLine(v)
end
file.close()
If the latter, that's a bit simpler.
Sending Computer:
rednet.open("back")
local f = fs.open("desired file", "r")
local content = f:readAll() --Reads the entire content of the file into the string
f.close()
rednet.send(1, content)
Receiving computer:rednet.open("back")
local f = fs.open("desired file", "w")
local content = rednet.receive()
f:write(content)
f.close()
#8
Posted 18 February 2013 - 06:25 AM
Bubba, on 18 February 2013 - 06:20 AM, said:
ViextraProgramming, on 18 February 2013 - 06:05 AM, said:
How would I do that?
So you want to save the contents of the rednet message to a file? Or do want one computer to send a file over rednet to be saved on the second computer?
If the former, then this:
rednet.open("back")
local id, strTable = rednet.receive()
local killers = textutils.unserialize(strTable)
local file = fs.open("killers", "a") --Open the killers file in append mode
for i,v in pairs(killers) do
file.writeLine(v)
end
file.close()
If the latter, that's a bit simpler.
Sending Computer:
rednet.open("back")
local f = fs.open("desired file", "r")
local content = f:readAll() --Reads the entire content of the file into the string
f.close()
rednet.send(1, content)
Receiving computer:rednet.open("back")
local f = fs.open("desired file", "w")
local content = rednet.receive()
f:write(content)
f.close()
Again, I am a newbie at this..
What is a latter, and how does it differ from a former?
Anyways, thanks for the help! I hope to get more in the future.
I hope I am not bugging you, bubba!
#9
Posted 18 February 2013 - 06:27 AM
You're not bugging me. I like helping people out
#10
Posted 18 February 2013 - 06:33 AM
Bubba, on 18 February 2013 - 06:27 AM, said:
You're not bugging me. I like helping people out
Thanks... I sometimes annoy people by asking them questions.
I am thinking of using this concept to make a simple email system. I will post the code here, and I think it will work.
I have an operating system that copies and makes directories from a Floppy, so I will put an "Archive" directory, "Inbox" and "Sent" directories, under a "Email" directory...
Still confusing...
I will use the latter format, that is within my grasp...
Thanks,
-V
#11
Posted 18 February 2013 - 11:18 AM
ViextraProgramming, on 18 February 2013 - 06:25 AM, said:
I have a question for you too, Bubba: how does textutils.unserialize exactly create a table? Does it recognise all spaces in a string and put all separate 'words' into a different entry in a table?
#12
Posted 18 February 2013 - 12:03 PM
Shrooblord, on 18 February 2013 - 11:18 AM, said:
ViextraProgramming, on 18 February 2013 - 06:25 AM, said:
I have a question for you too, Bubba: how does textutils.unserialize exactly create a table? Does it recognise all spaces in a string and put all separate 'words' into a different entry in a table?
You can open up mods/ComputerCraft/lua/rom/apis/textutils to see the full workings of serialize and unserialize, but essentially all unserialize does is return a loadstring(str) on the table in string form. If you like at the string form of a serialized table, you'll see something like this:
{[1]="this is a test",[2]=true,["test"]=1,}
That's a perfectly valid lua table in string form, so all that unserialize has to do is turn that into real code. Loadstring is the method textutils uses in order to do this. Serialize is a bit more complicated, but really all that it does is take each value using a for k,v in pairs(t) loop and put k,v into the string.
The downside to textutils is that it cannot serialize all tables (for example, tables with functions).
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











