Jump to content




Stuck in rednet project


37 replies to this topic

#1 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 06:24 PM

hi, i have been messing around with computercraft and thought: is it possible to send whole programs with rednet
I have tried some things, but cant make it work except if I put everything in the file to send on 1 line!
please help...... thanx in advance

#2 OmegaVest

  • Members
  • 436 posts

Posted 24 August 2012 - 06:29 PM

I think sending it on one line is possibly the best way to do so. Just, put linebreaks in it (n). On the other end, have the line put into a file, and the os will do the rest.

On the other hand, you can send individual lines across, and have a program designed to catch all these lines and put them into a file for you.

#3 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 06:42 PM

Huh?, not really getting it....., this may be my lack of experience...

#4 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 06:47 PM

An example would be nice.......

#5 OmegaVest

  • Members
  • 436 posts

Posted 24 August 2012 - 07:08 PM

Mmm.

Okay so first: how are you parsing the programs to a line presently? Because, it might look something like this.


function sendFile(inFile)   -- Needs to be a table/file from fs.open or io.open,and in read mode.
   local outtro
   while true do
      testro = inFile.readLine()
      if testro then
         outtro = outtro .. "n" .. testro
      else break end
   end
   return outtro
end


That would dump a file into a line with line breaks, ready for sending. You might even be able to call the function in the send call.

The other way would be



function sendFileSep(target, inFile)   -- target need to be a vaild computer. inFile needs to be a table/file from fs.open or io.open,and in read mode.
   while true do
      testro = inFile.readLine()
      if testro then
         rednet.send(target)
      else break end
   end
end




I don't know that either way will work great, or at all, but they should.

#6 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 07:08 PM

im getting cleared up here :D/>

#7 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 24 August 2012 - 07:23 PM

View PostOmegaVest, on 24 August 2012 - 07:08 PM, said:

Mmm.

Okay so first: how are you parsing the programs to a line presently? Because, it might look something like this.


function sendFile(inFile)   -- Needs to be a table/file from fs.open or io.open,and in read mode.
   local outtro
   while true do
	  testro = inFile.readLine()
	  if testro then
		 outtro = outtro .. "n" .. testro
	  else break end
   end
   return outtro
end


That would dump a file into a line with line breaks, ready for sending. You might even be able to call the function in the send call.

The other way would be



function sendFileSep(target, inFile)   -- target need to be a vaild computer. inFile needs to be a table/file from fs.open or io.open,and in read mode.
   while true do
	  testro = inFile.readLine()
	  if testro then
		 rednet.send(target)
	  else break end
   end
end




I don't know that either way will work great, or at all, but they should.
or do this:
sender:
function sendFile(inFile)   -- is only the direction i changed that
   local inFile = fs.open(inFile,"r")
   local outtro ={}
   while true do
     local testro = inFile.readLine()
	 if testro then
	   table.insert(outtro,testro)
	 else break end
   end
   return textutils.serialize(outtro)
end
reciever:
function recieveFile(inString,Savepath)   -- Serialized string and Savepath
  local file =  fs.open(Savepath,"w")
  local data=textutils.unserialize(inString)
  for x=1,#data do
	file.writeLine(data[x])
  end
  file.close()
end


#8 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 07:27 PM

not sure how to implent this...

#9 OmegaVest

  • Members
  • 436 posts

Posted 24 August 2012 - 07:32 PM

toOut = sendFile(file)
rednet.send(target, toOut)

flipped = rednet.receive()

Either Kolpa or my functions will work out


Both sendFile functions (basically) send over a long line of "code". This just needs to be ported to a file (in my case), or decomplied and put to a file (in Kolpa's, who has graciously included a decompiler as well).

#10 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 07:37 PM

created a new file and only added 'inFile = fs.open("test", "r")' above it and 'inFile.close() beneath it
bios:206"[string "isend"] : 6: '='expected

(did this with kolpa`s sender code)

#11 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 07:41 PM

put a = in it and no errors, but nothing returns... :D/>

#12 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 24 August 2012 - 07:51 PM

you should be able to run it like this:
sender:
function sendFile(inFile)   -- is only the direction i changed that
   local inFile = fs.open(inFile,"r")
   local outtro ={}
   while true do
	 local testro = inFile.readLine()
	   if testro then
		 table.insert(outtro,testro)
	   else break end
   end
   inFile.close()
   return textutils.serialize(outtro)
end

data = sendFile("filename")

rednet.send(pcid,data)
reciever:
function recieveFile(inString,Savepath)   -- Serialized string and Savepath
  local file =  fs.open(Savepath,"w")
  local data=textutils.unserialize(inString)
	  for x=1,#data do
		file.writeLine(data[x])
	  end
  file.close()
end

_,text = rednet.receive()
recieveFile(text,"path")


#13 OmegaVest

  • Members
  • 436 posts

Posted 24 August 2012 - 07:52 PM

Ehn. Okay, so our functions are meant to be called as functions (like print, actually)

It would actually be better used thusly:

file = fs.open("test", "r")

toOut = sendFile(file)
file.close()

rednet.send(target, toOut)


I'm not sure this is or isn't what you did. But, on the other computer, put Kolpa's code into a program and have it do this.


inFile = rednet.receive()

file = fs.open("test2", "w")
file.close()   -- Should make the file for you

receiveFile(inFile, "test2")


And there ya go. If I understand everything right this should work



EDIT: Crap. Ninja'd. Again. And better, no less.

#14 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 08:00 PM

kolpa: should i only add the line: sendFile = fs.open("test", "r") ?

#15 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 24 August 2012 - 08:08 PM

View Postmartinineter, on 24 August 2012 - 08:00 PM, said:

kolpa: should i only add the line: sendFile = fs.open("test", "r") ?
nah do the close too i forgot that let me edit it 1 sec

#16 martinineter

  • New Members
  • 20 posts

Posted 24 August 2012 - 08:14 PM

ok, waiting for it.....

#17 martinineter

  • New Members
  • 20 posts

Posted 25 August 2012 - 06:19 AM

still cant get it to work, which string in the string that comes from my fs.open thingy code

#18 martinineter

  • New Members
  • 20 posts

Posted 25 August 2012 - 06:44 AM

now I have this:


sender: rednet:347: positive number expected
rednet.open("right")
print ("Please enter the receiver ID: ")
ID = read()
print ("Please enter the file directory: ")
filename = io.read()
inFile = fs.open(filename, "r")
function sendFile (inFile)
local outtro={}
while true do
  testro = inFile.readLine()

  if testro then
   table.insert(outtro,testro)
  else break end
end
return textutils.serialize(outtro)
end
file = fs.open(filename, "r")data = sendFile(file)
file.close()
rednet.send(ID,data)
print ("File send!")
</name>

receiver: bios:206: [string "iget"]:8:8 '<name>' expected
rednet.open("right")
print ("Please enter filename: ")
Savepath = read()
function recieveFile(inString,Savepath)  
file =  fs.open(Savepath,"w")
  local data=textutils.unserialize(inFile)
	    for x,#data do
		  file.writeLine(data[x])
	    end
file.close()
end
_,text = rednet.receive()
recieveFile(text,path)

I now some stuff about computercraft, but what there errors are about...... :D/>

#19 ardera

  • Members
  • 503 posts
  • LocationGermany

Posted 25 August 2012 - 07:15 AM

Use (sender)
rednet.open("right")
print ("Please enter the receiver ID: ")
ID = assert(tonumber(read()), "Not a number")
print ("Please enter the file directory: ")
filename = io.read()
inFile = fs.open(filename, "r")
function sendFile (inFile)
local outtro={}
while true do
  testro = inFile.readLine()
  if testro then
   table.insert(outtro,testro)
  else break end
end
return textutils.serialize(outtro)
end
file = fs.open(filename, "r")data = sendFile(file)
file.close()
rednet.send(ID,data)
print ("File send!")
and (receiver):
rednet.open("right")
print ("Please enter filename: ")
Savepath = read()
function recieveFile(inString,Savepath) 
file =  fs.open(Savepath,"w")
  local data=textutils.unserialize(inFile)
		    for onlyalittlenumber=x,#data do
				  file.writeLine(data[x])
		    end
file.close()
end
_,text = rednet.receive()
recieveFile(text,path)


#20 FuzzyPurp

    Part-Time Ninja

  • Members
  • 510 posts
  • LocationHarlem, NY

Posted 25 August 2012 - 07:45 AM

You can send whole programs thru rednet and wifi - (i did this with the adventure program which is 1500+ lines when modems first came out in beta)

To the person above me, your signature is too big.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users