Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Netduino, server e file binari
Forum - C# / VB.NET - Netduino, server e file binari

Avatar
TheDarkJuster (Member)
Guru^2


Messaggi: 1620
Iscritto: 27/09/2013

Segnala al moderatore
Postato alle 17:48
Venerdì, 17/10/2014
Sto scrivendo un web server per netduino, una scheda simile ad arduino che si programma in C# e che fa target a una versione ristretta del .NET framework. Fino a qui nessun problema: funziona bene, il debug è gradevole visual studio 2013 fa il suo dovere ecc..... Quindi ho preso un esempio di un web server minimale e lo ho cambiato fino a farlo diventare un web server che possa rispondere con delle pagine HTML o script chunked: ovvero mando il contenuto "a pezzi" senza specificare la dimensione, perchè se caricassi una pagina nella ram finirei subito i circa 100KB di RAM. Finchè i file da invieare sono testi posso usare questa tecnica, quando sono pdf/immagini ecc.... devo specificare la lungheza ed inviare i byte al browser. Il problema è con i file binari. Non riesco a leggerli, nè con FileStream nè con StreamReader. Il problema è che la scheda legge i primi caratteri correttamente, ma quelli successivi sono tutti caratteri strani, non visualizzabili che dovrebbero essere altro. Il progetto lo potete trovare qui: https://code.google.com/p/httpnetduino/ e la parte incriminata è quella nel file BinaryFile (il metodo ReadBlock). Ho debuggato ore, ore, ore, ore, giorni e giorni, ma non c'è stato verso di far leggere tutto il file correttamente, e non so più cosa fare. Chiedo aiuto qui, visto che sul forum ufficiale non mi pubblicano la domanda. Ci tengo a precisare che scegliere questo forum come seconda opzione è dato dal semplice fatto che non credo qui qualcuno possegga un netduino con cui fare il debug, ma spero che possiate illuminarmi ugualmente.

P.S. riporto per i pigroni il sorgente (no, scherzo lo riporto perchè potrebbe tornare utile in futuro sapere come era il sorgente con il problema)

Codice sorgente - presumibilmente C#

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Microsoft.SPOT;
  5.  
  6. namespace HTTPDuino
  7. {
  8.     public class HTTPBinaryFile : IDisposable
  9.     {
  10.         private string filePath;
  11.         private System.IO.StreamReader fileBytes;
  12.         private int currentPosition;
  13.  
  14.         public HTTPBinaryFile(string path)
  15.         {
  16.             //check the file existance
  17.             if (!System.IO.File.Exists(path))
  18.                 throw new Exception("The file " + path + " doesn't exists");
  19.  
  20.             //store the file path
  21.             this.filePath = path;
  22.  
  23.             //initialize the reader
  24.             this.currentPosition = 0;
  25.             //this.fileBytes = System.IO.File.OpenWrite(this.filePath);
  26.             this.fileBytes = new StreamReader(this.filePath);
  27.         }
  28.  
  29.         public long getLength()
  30.         {
  31.             return this.fileBytes.BaseStream.Length;
  32.         }
  33.  
  34.         public string getBlock(ref int read)
  35.         {
  36.             //empty the byte container that will be filled by a portion of file
  37.             char[] buffer = new char[255];
  38.  
  39.             MemoryStream memory = new MemoryStream();
  40.  
  41.             read = 0;
  42.             while ((read < 255) && ((this.currentPosition + read) < this.fileBytes.BaseStream.Length)/*this.fileBytes.EndOfStream*/)
  43.             {
  44.                 buffer[read] = (char)this.fileBytes.Read();
  45.                 //memory.Write(buffer, 0, buffer.Length);
  46.                 read++;
  47.             }
  48.  
  49.             memory.Close();/*
  50.             bytes = memory.ToArray();
  51.  
  52.             char[] mychars = Encoding.UTF8.GetChars(bytes);
  53.             */
  54.             string dbg = new string(buffer);
  55.             Debug.Print(dbg);
  56.  
  57.             return dbg;
  58.         }
  59.  
  60.         public bool endOfBlocks()
  61.         {
  62.             return (this.fileBytes.BaseStream.Length == this.currentPosition);
  63.         }
  64.  
  65.         #region IDisposable Members
  66.         ~HTTPBinaryFile()
  67.         {
  68.             Dispose();
  69.         }
  70.  
  71.         public void Dispose()
  72.         {
  73.             //delete every file-related objects
  74.             this.fileBytes.Close();
  75.             this.fileBytes.Dispose();
  76.             this.filePath = string.Empty;
  77.             this.filePath = null;
  78.         }
  79.         #endregion
  80.     }
  81. }



Personalmente ho scelto netduino perchè sto costruendo un phmetro che possa essere consultato come una pagina web e che invii le letture (di ph e di temperatura, lasciamo stare il perchè della temmperatura) come json in maniera asincrona, quindi con del buon css posso fare bella la pagina senza immagini, ma visto che è un progetto che scrivo io per necessità ma che pubblico perchè altri (con altre necessità) ne possano usufruire mi piacerebbe che funzionasse anche l'invio di file binari

Ultima modifica effettuata da TheDarkJuster il 17/10/2014 alle 18:07
PM Quote