Include binary problem

Just starting out? Need help? Post your questions and find answers here.
dsw
User
User
Posts: 10
Joined: Thu Feb 01, 2007 4:26 pm
Location: UK

Include binary problem

Post by dsw »

I am including a data file into some code. When I compile the code the size of the exe file suggests that the data has been included. When I run the program it works as expected but when I try to run the exe on a different machine the program fails at the data read stage. A picture which is also included is fine.

DataSection
#file_1:
IncludeBinary "data.hex"
Image_Image_1:
IncludeBinary "picture.JPG"
EndDataSection

Any pointers would be appreciated this is driving me nuts!
Running PB4 and Windows

Thanks
David
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Include binary problem

Post by PB »

> the program fails at the data read stage

So please post this code so we can see why it's failing.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Code: Select all

DataSection 
#file_1: 
Get rid of the character in front of file_1: and your problem should disappear.
BERESHEIT
dsw
User
User
Posts: 10
Joined: Thu Feb 01, 2007 4:26 pm
Location: UK

Post by dsw »

Thanks netmaestro, I tried that but it didn't work. As PB suggests here is the main bit of code (experienced programmers please close your eyes now) :)
When running on a different machine (or a different location on the main PC) the program fails in Procedure send at the Eof "while" loop

DataSection ;>
file_1:
IncludeBinary "data.hex"
Image_Image_1:
IncludeBinary "picture.JPG"
EndDataSection ;<
;}
;{ Image Plugins
UseJPEGImageDecoder()
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}


OpenWindow_Window_0()
Global Conn$

Procedure Send()
serport()
ReadFile(file_1, "data.hex")
Hcom.l = ComOpen(Conn$,#HandShakeModeRtsCts,255,255)
ComSetDTR(Hcom,1)
If Hcom=0
StatusBarText(#StatusBar_Window_0,0,"Connection failed - Check serial port settings")
Else
StatusBarText(#StatusBar_Window_0,0,"Connected to "+Conn$)
Delay(300);to allow receiver to settle after switch on
Beep_(1500,250)
While Eof(file_1)=0 ; loop as long the 'end of file' isn't reached
MyBuffer1.s=ReadString(file_1)+#CR$
ComWrite(Hcom, @MyBuffer1, Len(MyBuffer1))
sizeleft.f=(Lof(file_1)-Loc(file_1))
size.f=Lof(file_1)
percent.f=(1-(sizeleft/size))*100
SetGadgetState(#ProgressBar_5, percent)
Wend ;until the whole file is sent
CloseFile(file_1)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

if you had put your code in codetags, it would have been readable.

use the Code-button above the posting area. it will produce these tags: [code][/code]


> Thanks netmaestro, I tried that but it didn't work.

then there is a bug elsewhere, too, but you have to remove the '#' in every case.
a leading '#' describes a constant. the compiler cant recocnize this as a label.
oh... and have a nice day.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

This line in your program

Code: Select all

ReadFile(file_1, "data.hex") 
is reading the file from disk even though you have it stored in your program which is why it works on your computer and not on someone elses or yours if you move the file to a different location.

Edit. In fact you are using several disk file commands when you should be reading from memory.
dsw
User
User
Posts: 10
Joined: Thu Feb 01, 2007 4:26 pm
Location: UK

Post by dsw »

Kaeru Gaman - OK I know how to use the Code button now, thanks for pointing that out.
I did remove the #'s while I was debugging before..

Derek, It sounds like you have it! Trouble is, how do I read a local file(i.e. a file contained in the exe) as opposed to one on the PC...

Thanks for your help
David
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Use Restore (with a label name) to set the pointer to the start of the file and then use Read a$ or Read a.s etc

You will have to use a different method instead of eof() to end the loop, something along the lines of counting the length of the strings until you reach the size of the file, which you can work out if you subtract a label at the start of the file from a label at the end.

Try this.

Code: Select all

DataSection
begin:
Data.s "one"
Data.s "two"
Data.s "three"
finish:
EndDataSection
Restore begin
l=0
fl=?finish-?begin
Repeat
Read b$
l+Len(b$)+1
Debug b$
Until l>=fl
dsw
User
User
Posts: 10
Joined: Thu Feb 01, 2007 4:26 pm
Location: UK

Post by dsw »

Thanks a lot for the tip. That was something I hadn't thought about at all.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

You're welcome. :)
User avatar
manu
User
User
Posts: 32
Joined: Tue May 13, 2003 12:40 pm
Location: Germany
Contact:

Should be simple

Post by manu »

Try this:

Code: Select all

file_1_begin:
IncludeBinary "data.hex"
file_1_end:
[...]

size=?file_1_end-?file_1_begin
ComWrite(Hcom, ?file_1_begin, size) 

or in smaller blocks to get the progressbar running:

written=0
size=?file_1_end-?file_1_begin
*ptr=?file_1_begin
While written<size
  bs=256
  If bs>(size-written):bs=size-written:Endif
  ComWrite(Hcom, *ptr+written, bs)
  written+bs
  SetGadgetState(#ProgressBar_5, (100*written)/size) 
Wend
--manu
dsw
User
User
Posts: 10
Joined: Thu Feb 01, 2007 4:26 pm
Location: UK

Post by dsw »

manu,
Thank you! Nice piece of code and it works well

David
Post Reply