Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Reading binary resources piecemeal


  • Please log in to reply
10 replies to this topic

#1 Ward

Ward

    Member

  • Members
  • PipPip
  • 22 posts

Posted 07 November 2010 - 07:05 PM

Does anyone have tips for reading a binary resource from flash without copying the entire thing to RAM? I'm working on a Netduino Plus and have a 54K chunk of data that fits in flash with the 64K firmware, but (understandably) throws a memory exception when I try any of the following three approaches:

Byte [] data = Resources.GetBytes(Resources.BinaryResources.Sample);
Object data = Microsoft.SPOT.ResourceUtility.GetObject(Resources.ResourceManager, Resources.BinaryResources.Sample);
Stream data = (Stream)Microsoft.SPOT.ResourceUtility.GetObject(Resources.ResourceManager, Resources.BinaryResources.Sample);
The first isn't too surprising, the second maybe a little more so, but the third is a bit disappointing. It looks like there's a "GetManifestResourceStream" method in the Assembly class on most C# implementations that would be useful, but for some reason isn't available on this particular build of NETMF. Thoughts? Chris, was this intentionally omitted to save space? Thanks.

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 07 November 2010 - 07:24 PM

Hi Ward, Interesting. I've never tried to stream an embedded resource in .NET MF before. As a quick solution, have you tried to break the resource into a series of small chunks? I know it's not as pretty--but I'm not sure if .NET Micro Framework supports loading bits of an embedded resources (without trying to load the entire thing into memory first). Chris

#3 Ward

Ward

    Member

  • Members
  • PipPip
  • 22 posts

Posted 08 November 2010 - 08:53 AM

Chris, that'd certainly work, and I was thinking along the same lines. I'd like to avoid it if possible as the use case is "copy this file [the resource] to this directory, run the script". I could probably come up with a PC-side script that first divided the resource into three pieces that would fit in RAM if there's no other way. --Ward

#4 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 15 November 2011 - 06:33 PM

Has there been a solution on this problem? I also have a text file saved in my embedded ressources that I would like to load piece for piece using a Stream. Greets, Markus

NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 15 November 2011 - 07:36 PM

Has there been a solution on this problem?

.NET MF version 4.2 introduces a method for partial reading of binary resources, there is a new overload of Resources.GetObject(..., int offset, int length).

#6 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 15 November 2011 - 08:32 PM

Hey thanks, I think I really should sit down in the next days and update my device and IDE to 4.2 ...
StringBuilder and this piece of code would really help...

Do you know how to use it exactly?
What does it return? If Resources.GetObject(..., int offset, int length) is an object, can I just cast it to String without any problems? In case i have a .txt embedded ressource that has 10 kb, and I want to load it in 10 pieces a 1 kb, can i use string
part1 = (String)Resources.GetObject(...,0,1000)
?

Greets, Markus

NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#7 Igor Kondrasovas

Igor Kondrasovas

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts
  • LocationPorto, Portugal

Posted 19 December 2011 - 09:36 PM

.NET MF version 4.2 introduces a method for partial reading of binary resources, there is a new overload of Resources.GetObject(..., int offset, int length).


Hello,


What references and namespace I must use to have acess to Resources class? I have already upgraded my device and project o .net mf 4.2 and only ResourceManager class is available on Resources namespace.

Thank you,

Igor.

Please do not consider this message. I notice that my project still did not have any resources created... my bad.

Edited by Igor Kondrasovas, 19 December 2011 - 09:45 PM.

Igor Kondrasovas

www.inovativatec.com


#8 Igor Kondrasovas

Igor Kondrasovas

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts
  • LocationPorto, Portugal

Posted 20 December 2011 - 12:06 PM

.NET MF version 4.2 introduces a method for partial reading of binary resources, there is a new overload of Resources.GetObject(..., int offset, int length).



I am curently testing this GetObject overload, and I am experiencing an strange behaviour.


1 - I created a binary file called "sm.bin", witch is attached to this message (zipped in this message so I could not upload .bin files). It is a very small file with 8 bytes on it.

2 - Then I added this bin file to my resources in my net mf project. Build Action = embedded resource and always copy to output

3 - In the very beginning of main method, I used

byte[] data = (byte[])ResourceUtility.GetObject(Resources.ResourceManager, Resources.BinaryResources.sm, 0, 8);
When I run the code, data contains 0 bytes.

Now, when I change the second parameter from 8 to 20 for instance, I get the 8 bytes I am expecting (from position 0 to 7) and some more other bytes (12 in total).
Why this is hapenning?

Attached Files

  • Attached File  sm.zip   118bytes   11 downloads

Igor Kondrasovas

www.inovativatec.com


#9 Blake Ramsdell

Blake Ramsdell

    Member

  • Members
  • PipPip
  • 20 posts

Posted 20 December 2011 - 05:21 PM

If only there was a way to implement a test of code to see if it works. An "automated test" if you will.

http://netmf.codeplex.com/

client_v4_2/CLR/Libraries/CorLib/corlib_native_System_Resources_ResourceManager.cpp

if(offset + length >= size)
{
    length -= size - offset; 
}

Seems like this is wrong in at least a couple of ways. I think:

if((offset + length) > size)
{
    length = (size - offset);
}
Is both clearer and less buggy.

So I think in your first case you ended up with:

if(0 + 8 >= 8)
{
    length -= 8 - 0;
}
So your length ended up as 0 in this case.

if(0 + 20 >= 8)
{
    length -= 8 - 0;
}
So your length ended up as 12 in this case (8 bytes of your resource data and four bytes of god knows what).

Happy to submit a patch, but I need to spend an hour and figure out how to do it and get the code. I was able to figure this out by browsing the source on CodePlex.

I'd say in the interim just make your resource one byte larger than you need. So nine bytes and then just know that the first eight are the "real" resource.

#10 Igor Kondrasovas

Igor Kondrasovas

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts
  • LocationPorto, Portugal

Posted 05 January 2012 - 11:38 AM

Hello Blake, Sorry for the delayed answer. Don´t know why I cannot reproduce the problem anymore. This kind of stuff is really anoying, since you will never know when it will happen again. Anyway, I see your point and hopefully somebody else can consistently reproduce it and fix it. Thank you, Igor.

Igor Kondrasovas

www.inovativatec.com


#11 Martin Ayotte

Martin Ayotte

    Member

  • Members
  • PipPip
  • 15 posts

Posted 22 November 2012 - 11:12 PM

Hi everyone, I'm trying to embed some binary resources, everything compile, but at runtime, it crash with a System.NotImplementedException inside the method Microsoft.SPOT.ResourceUtility.GetObject(ResourceManager, id) (without chunk pos/size). So I've tried to use the new method explain above, Microsoft.SPOT.ResourceUtility.GetObject(ResourceManager, id, offset, len) and I get another kind of exception, System.InvalidOperationException ... I'm running netduino_pls_4.2.0.1, is those resource management code have been removed during past releases ? Thanks Martin. Update : I've figured out ... When I've inserted my binary resources, which are in fact jpegs, VS inserted them as Bitmaps. I've then switched them manually in Designer.cs, but I didn't edit the .resx where they were still as Bitmaps. I've recently edit the .resx and change it to "System.Byte[], mscorlib". It fixed the issue, my WebServer class is now able to stream those jpegs.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.