Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Send file to a PC

dave1707dave1707 Mod
edited December 2012 in General Posts: 7,874

Here is a program to send a file to a PC for either backup or to process the information The first part of this program is just code to show examples of how to save the data from a table or string. Just comment/uncomment the different calls. The data can be either ASCII or Hex or both. If the file is all ASCII, the file will be terminated with hex zeroes. If the file is all hex or ASCII and hex, the first 3 bytes of the file will contain the number of bytes in the file excluding the 3 byte size. Both files will have hex zeroes at the end which were used to fill the remaining bytes of the .png image that wasn't actual data. The main part of the program is createPNG(strng) . After you create a table or string of the information you want to transfer, call createPNG passing the table or string name. If a table is sent, it will be converted to a string. If you want to take the information into excel, then create the information in the table with comma seperaters (see example function AsciiTable ). The information will be packed into a .png image. The image is saved in Dropbox with the name CodeaFile. If you have Dropbox running on your PC, just sync Dropbox to update the file on your PC. After syncing, load the file CodeaFile.png on your PC into a photo editor and save it as a .raw file. If you can't save the file as .raw, the .png file will be useless to you. After creating CodeaFile.raw, you can rename it to whatever name you want, changing the extension to .txt for an ASCII file, or .bin for a hex file. The name CodeaFile in this program doesn't need to be changed because you should always rename the file CodeaFile.raw on the PC to something else before using it. I checked the program for errors, but if you find any, let me know.

EDIT: to view the contents of the hex file on the PC, you'll have to use a hex editor.


-- start of code for showing examples function setup()             -- uncomment/comment calls for different test example. Call only one at a time.          AsciiTable()    -- test call for Ascii table     --AsciiString()    -- test call for Ascii string         --HexTable()    -- test call for Hex table     show()    -- show hex values stored in .png file end function AsciiTable()    -- create test data from Ascii table     local tab={}     for z= 1,32 do         tab[z]=z.."," -- create data with comma seperaters         --table.insert(tab,z..",")     end     str=table.concat(tab)    -- convert table to a string      print("Ascii file from a table.\n")      print(str.."\n")       createPNG(tab) end function AsciiString()    -- create test data from Ascii string     str="one,two,3,four,5,six,seven,8,nine"     print("Ascii file from a string.\n")     print(str.."\n")     createPNG(str) end function HexTable()    -- create test data from a hex table     local tab={}     for z=0,32 do         tab[z+1]=string.char(z)     end      str=table.concat(tab)    -- convert table to a string       print("Hex file created from a table\n1st 3 bytes (b1,b2,b3) are the size.")       print("File size = b1+(b2*256)+(b3*256*256)\n")         createPNG(tab) end function show()    -- show values stored in img1     img1=readImage("Dropbox:CodeaFile")     x1=img1.width     y1=img1.height     print("The Hex values stored in the PNG file.    R     G     B  values.\n")            for y=y1,1,-1 do         for x=1,x1 do             r,g,b,a=img1:get(x,y)             str=string.format("%02x    %02x    %02x",r,g,b)             print(str)         end     end     print("\nAscii files are terminated with hex zeroes. Hex files have the size in 1st 3 bytes.")     print("\nThe remaining image is filled with hex zeroes.") end -- end of code for showing examples -- this function is all that's needed to create the .png image file function createPNG(strng)    -- pass a string or table of information     -- written by: dave1707 12/10/12     local fileName="Dropbox:CodeaFile"    -- file name for .png file     local img1,size      local count=0     local x,y,v1,v2,v3,s1,s2,b1,b2,b3,z     local a1=256; local a2=256*256     local ascii=true          if type(strng)=="table" then         strng=table.concat(strng)    -- convert a table to a string     end          -- check for all ascii or a mix of ascii and hex     size=#strng        for z=1,size do         x=string.byte(strng,z,z)         if x<32 or x>126 then    -- non ascii value             ascii=false         end     end           -- calculate width and height for image size and create img1     x=math.ceil((size+6)/3)     s1=math.ceil(math.sqrt(x))     s2=math.ceil(x/s1)      img1=image(s1,s2)                  -- calculate 3 byte size (b1+b2*256+b3*256*256) for hex file     b1=size; b2=0; b3=0      if b1>=a2 then         b3=math.floor(size/a2)         b1=b1-b3*a2     end     if b1>=a1 then         b2=math.floor(size/a1)         b1=b1-b2*a1     end          -- move string data into r,g,b values and set x,y point of img1     for y=s2,1,-1 do            for x=1,s1 do             if x==1 and y==s2 and not ascii then                 img1:set(1,s2,b1,b2,b3,255)    -- put size in 1st rgb entry of a hex file                          else                 v1=0;v2=0;v3=0                 count = count + 1                 if count<=size then                     v1=string.byte(strng,count,count)                     count = count + 1                     if count<=size then                         v2=string.byte(strng,count,count)                         count = count + 1                         if count<=size then                             v3=string.byte(strng,count,count)                         end                      end                                       img1:set(x,y,v1,v2,v3,255)    --save rgb values per x,y point                 else                     img1:set(x,y,0,0,0,255)    -- fill remaining image with 0's                 end             end         end     end          saveImage(fileName,img1) end

Comments

  • Let me step right in.

    If you're on a local network I suggest using some kind of file server on the PC. I already have (privately) extended my minifileserver script (that I already use to run scripts on Codea from the PC) to accept POST queries and store data back in files.

    In Codea I do something like this:


    function post(data) url = "http://192.168.178.25/report.txt" para = {} para["method"] = "POST" para["data"] = data http.request(url, success, failure, para) end function success() end function failure() end data = table.concat(listGlobalData(), "\n") post(data)

    Will come up with a new version of my file server later this week.

  • dave1707dave1707 Mod
    Posts: 7,874

    Hi @Codeslinger My iPad, PC and printer are online thru a router. I use the PC just for things that I don't want or can't do on the iPad. All of my fun coding is done on the iPad, so I really don't need to send info back and forth to the PC. It looks like your code is for getting information from the PC while my code was to send a file from the iPad to the PC. I don't have a reason to send info to the PC right now, but I thought I would write that just for something to do. How useful it is I don't know, or if there's an easier way of sending a file (ascii or hex) from the iPad to the PC.

  • What exactly makes you think that I'm GETting data from the PC instead of POSTing to it? The above code will create a file "report.txt" on the PC containing the global data keys.

    At the moment I'm just waiting for Codea 1.5 to offer some data access functions, there's nothing in there yet that hasn't been there before or I haven't seen it, the os namespace also doesn't exist. All this makes it a bit difficult.

  • dave1707dave1707 Mod
    Posts: 7,874

    Hi @Codeslinger I'm not familiar with the http functions. I was just thinking that the http:request running on the iPad was just requesting (getting) information from the URL. I was looking at the Codea reference for http:request and it doesn't give me enough information to know how to use it. Can you give me more info on the function (an example of sending and getting a file) and an explanation of the parameters, or point me to a link that explains it in more detail. Others reading this post who don't know might also be interested. I think this is something that I'd like to play around with to see how it works.

  • edited December 2012 Posts: 666

    Http.get is a misnomer: it allows you to send http verbs such as POST, but you have to also send a data body. The web server will get the post request, but has to do something with it.

    His post function is nice and compact.

    No data access stuff. Is forthcoming @codeslinger. I'm using MOAI cloud to resolve that.

Sign In or Register to comment.