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

Stuck in rednet project

Started by martinineter, 24 August 2012 - 05:24 PM
martinineter #1
Posted 24 August 2012 - 07: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
OmegaVest #2
Posted 24 August 2012 - 07: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.
martinineter #3
Posted 24 August 2012 - 07:42 PM
Huh?, not really getting it….., this may be my lack of experience…
martinineter #4
Posted 24 August 2012 - 07:47 PM
An example would be nice…….
OmegaVest #5
Posted 24 August 2012 - 08: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.
martinineter #6
Posted 24 August 2012 - 08:08 PM
im getting cleared up here :D/>/>
Kolpa #7
Posted 24 August 2012 - 08:23 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.
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
martinineter #8
Posted 24 August 2012 - 08:27 PM
not sure how to implent this…
OmegaVest #9
Posted 24 August 2012 - 08: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).
martinineter #10
Posted 24 August 2012 - 08: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)
martinineter #11
Posted 24 August 2012 - 08:41 PM
put a = in it and no errors, but nothing returns… :D/>/>
Kolpa #12
Posted 24 August 2012 - 08: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")
OmegaVest #13
Posted 24 August 2012 - 08: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.
martinineter #14
Posted 24 August 2012 - 09:00 PM
kolpa: should i only add the line: sendFile = fs.open("test", "r") ?
Kolpa #15
Posted 24 August 2012 - 09:08 PM
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
martinineter #16
Posted 24 August 2012 - 09:14 PM
ok, waiting for it…..
martinineter #17
Posted 25 August 2012 - 07:19 AM
still cant get it to work, which string in the string that comes from my fs.open thingy code
martinineter #18
Posted 25 August 2012 - 07: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/>/>
ardera #19
Posted 25 August 2012 - 08: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)
FuzzyPurp #20
Posted 25 August 2012 - 08: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.
martinineter #21
Posted 25 August 2012 - 08:46 AM
sender works now, receiver doesnt…

something in line 5: iget:5: bad argument: string expected, got nil

what me do wrong?
used ardera`s code
martinineter #22
Posted 25 August 2012 - 08:48 AM
oops, double post, internet problems
martinineter #23
Posted 25 August 2012 - 09:51 AM
btw, if I just use the most simple receiver code possible it says that it receives 100, is that correct?

x = rednet.receive()
print(x)

Edit: OOOOOh, so stupid, it now prints the ID
it now prints : {[1]="print ("hello")",}
I believe that is correct
ardera #24
Posted 25 August 2012 - 10:00 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.

Ok OK… Ill make a smaller one…… EDIT: Now better?

Fixed receiver code:

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, Savepath)

in the last line there was recieveFile(text, path) instead of recieveFile(text, Savepath)
martinineter #25
Posted 25 August 2012 - 10:23 AM
nope, doesnt work either:
textutils:141: attempt to cencatenate string and nil
typo?
ardera #26
Posted 25 August 2012 - 10:33 AM
Ok now the code should be fixed:

rednet.open("right")
print ("Please enter filename: ")
Savepath = read()
function recieveFile(inString,Savepath)
  file =  fs.open(Savepath,"w")
  local data=textutils.unserialize(inString)
  for onlyalittlenumber=x,#data do
    file.writeLine(data[x])
  end
  file.close()
end
_,text = rednet.receive()
recieveFile(text, Savepath)
martinineter #27
Posted 25 August 2012 - 11:02 AM
…………. error:
iget:7: 'for' initial value must be a number

damn those stupid errors
ardera #28
Posted 25 August 2012 - 04:23 PM
Ok Ok now it should be fixed forever:

rednet.open("right")
print ("Please enter filename: ")
Savepath = read()
function recieveFile(inString,Savepath)
  file =  fs.open(Savepath,"w")
  local data=textutils.unserialize(inString)
  for onlyalittlenumber=1, #data do
    file.writeLine(data[x])
  end
  file.close()
end
_,text = rednet.receive()
recieveFile(text, Savepath)

Sry ^^
martinineter #29
Posted 25 August 2012 - 04:52 PM
ok, no errors now, but 1 tiny little malfunction……. the file the receiver writes is empty ….. :D/>/>
ardera #30
Posted 25 August 2012 - 04:57 PM
Next error found:

rednet.open("right")
print ("Please enter filename: ")
Savepath = read()
function recieveFile(inString,Savepath)
  file =  fs.open(Savepath,"w")
  local data=textutils.unserialize(inString)
  for onlyalittlenumber=1, #data do
	file.writeLine(data[onlyalittlenumber])
  end
  file.close()
end
_,text = rednet.receive()
recieveFile(text, Savepath)
Now all bugs should be fixed…


Now it should be fixed…


Huh … Who coded the base code?
Kolpa #31
Posted 25 August 2012 - 08:34 PM
Next error found:

rednet.open("right")
print ("Please enter filename: ")
Savepath = read()
function recieveFile(inString,Savepath)
  file =  fs.open(Savepath,"w")
  local data=textutils.unserialize(inString)
  for onlyalittlenumber=1, #data do
	file.writeLine(data[onlyalittlenumber])
  end
  file.close()
end
_,text = rednet.receive()
recieveFile(text, Savepath)
Now all bugs should be fixed…


Now it should be fixed…


Huh … Who coded the base code?

me ;/ was kinda tired that day fixed my post :D/>/>
martinineter #32
Posted 25 August 2012 - 09:39 PM
… still same problem…. :D/>/>
Kolpa #33
Posted 26 August 2012 - 10:08 AM
this code works for me its the same as i posted dunno why it doesn't work for you, it writes the file new into the file test

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

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

raw = sendFile("new")
recieveFile(raw,"test")
Kolpa #34
Posted 26 August 2012 - 10:30 AM
and these two programs send it over rednet:
Sender:
this sends the file "newTest"

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

function mod() --this finds the location of the modem
for n,sSide in pairs(rs.getSides()) do
  if peripheral.getType(sSide) == "modem" then
  side = sSide
  break
  end
end
return side
end

rednet.open(mod())
rednet.broadcast(sendFile("newTest"))

Reciever:
this saves it and runs it automatically

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

function mod() --this finds the location of the modem
for n,sSide in pairs(rs.getSides()) do
  if peripheral.getType(sSide) == "modem" then
  side = sSide
  break
  end
end
return side
end

rednet.open(mod())
_,txt = rednet.receive()
filename = "newTest"
recieveFile(txt,filename)
shell.run(filename)
martinineter #35
Posted 31 August 2012 - 08:08 AM
uuuuuhm, sorry for the late post, I probably fucked up, but it doesnt work either……….
Kolpa #36
Posted 31 August 2012 - 04:58 PM
uuuuuhm, sorry for the late post, I probably fucked up, but it doesnt work either……….
that's weird it works fine for me o.O please post the whole error
martinineter #37
Posted 03 September 2012 - 07:54 AM
no error, file is just empty

BTW, school started, so thats the reason for late posts
Kolpa #38
Posted 03 September 2012 - 06:46 PM
no error, file is just empty

BTW, school started, so thats the reason for late posts
are you playing on a server of some sort ? it would help me being able to look over the data (also try to start a new world this code try's to write the 1. message it gets to a file so if anything is broadcasting near you it will interfere)