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

Rednet

Started by ebernerd, 16 February 2013 - 02:08 PM
ebernerd #1
Posted 16 February 2013 - 03:08 PM
I was wondering if someone could help me with programming them and with rednet. I want to set up a town that with computer automated databases. I was wondering if someone can help me program with tables and rednet. I've had trouble with both these things in the past and I'm looking for someone to help me.
(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
grand_mind1 #2
Posted 16 February 2013 - 03:46 PM
This would be a very large project. I do not know enough about computercraft to help you with this. I like the idea, however Ask a Pro is for help with programs, not to get someone to write a program for you. Try to code a bit of it and if you get stuck or need help then that's a perfect time to ask someone.
Please do not take this in an offensive way!
Sorry, I just wanted you to know. :D/>
Bubba #3
Posted 16 February 2013 - 04:50 PM
Yeah this topic would fit better in General methinks.

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.
ebernerd #4
Posted 18 February 2013 - 05:54 AM
Yeah this topic would fit better in General methinks.

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.
Thanks! But what does the "str" stand for? I would just like to know so I can understand it more
Bubba #5
Posted 18 February 2013 - 06:00 AM
In this case, I call the variable "strTable" because it is a string representation of a table. What textutils.serialize() does is convert a table into a string format so that you can easily do things like this. Textutils.unserialize() does the opposite and converts the string back into a table.
ebernerd #6
Posted 18 February 2013 - 06:05 AM
In this case, I call the variable "strTable" because it is a string representation of a table. What textutils.serialize() does is convert a table into a string format so that you can easily do things like this. Textutils.unserialize() does the opposite and converts the string back into a table.
Thanks! I understand it now! That was very helpful.

Another thing I would like to ask (and I appreciate you helping me on this :D/>) is:

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?
Bubba #7
Posted 18 February 2013 - 06:20 AM
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?

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()
ebernerd #8
Posted 18 February 2013 - 06:25 AM
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?

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!
Bubba #9
Posted 18 February 2013 - 06:27 AM
The former saves a serialized table to the file. The latter just opens a file on one computer, sends it over rednet, and the receiving computer re-saves it.

You're not bugging me. I like helping people out :)/>
ebernerd #10
Posted 18 February 2013 - 06:33 AM
The former saves a serialized table to the file. The latter just opens a file on one computer, sends it over rednet, and the receiving computer re-saves it.

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. :D/>

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… :D/>

Still confusing… :D/>

I will use the latter format, that is within my grasp… :D/>

Thanks,
-V
Shrooblord #11
Posted 18 February 2013 - 11:18 AM
What is a latter, and how does it differ from a former?
'Latter' and 'former' are expressions used in speech to indicate one of two things: either the thing that was said before (former) or the thing that was said last (latter). ;)/>

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?
Bubba #12
Posted 18 February 2013 - 12:03 PM
What is a latter, and how does it differ from a former?
'Latter' and 'former' are expressions used in speech to indicate one of two things: either the thing that was said before (former) or the thing that was said last (latter). ;)/>

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).