473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Multiple looping


Hello,

I am trying to send 2^19 data to USB2.0. I am reading the file
"all_sines" and loading it into
an array called "string1". Then, in a for loop I copied it into the USB
data array "Data_outBuffer". The USB can
carry only "32767" bytes at one time. The way I am doing right now
requires 16 for loops to send the
2^19 amount of data. ( 32767 * 16 = 2^19). My USB function SetSignals
can only carry "32767 bytes at one time".

I am looking for optimize code solution. like one while loop and the
other for loop.
Thanks
Regards,
John

#define COUNT_VAR 32767

int main(int argc, char* argv[])

{

FILE *infile;
unsigned char string1[COUNT_VAR+1];
unsigned char DataOutBuffer[COUNT_VAR -1];
int ReturnVal;
int x;
if ( (infile=fopen("C:\\C\\all_sines.bin","rb")) != NULL )

{
fread ( string1,sizeof( char), COUNT_VAR, infile );
}

else

printf( "Problem opening the file\n" );

for( x=0; x <= COUNT_VAR-1 ; x++ )

{

DataOutBuffer[x]=string1[x];
printf("value =%d %d\n",x,DataOutBuffer[x] );

}

SetSignals (PodNumber, 0xFF /* Don't Care */, COUNT_VAR /*Max.
Length=32767*/, DataOutBuffer); // USB Command

}/*main*/

Aug 3 '05 #1
4 2323
On Wed, 03 Aug 2005 14:24:18 -0700, john wrote:

Hello,

I am trying to send 2^19 data to USB2.0. I am reading the file
"all_sines" and loading it into
an array called "string1". Then, in a for loop I copied it into the USB
data array "Data_outBuffer". The USB can
carry only "32767" bytes at one time. The way I am doing right now
requires 16 for loops to send the
2^19 amount of data. ( 32767 * 16 = 2^19). My USB function SetSignals
can only carry "32767 bytes at one time".


How about:

#define MAX_SEND 32767

int main(int argc, char* argv[])
{
FILE *infile;
int x;

if ( (infile=fopen("C:\\C\\all_sines.bin","rb")) != NULL )
{
// Loop until all data has been sent.
for (;;)
{
unsigned char buffer[MAX_SEND];
// Read up to MAX_SEND bytes.
int bytes = fread(buffer, sizeof(unsigned char), MAX_SEND, infile);
// Make sure we got some data.
if (bytes == 0)
break; // error or eof. use ferror if you care.
// Send the data.
SetSignals(PodNumber, 0xFF, bytes, buffer); // USB Command
}
}
else
printf( "Problem opening the file\n" );
}

- Jay
Aug 3 '05 #2
Hello,

Thanks for the reply! But this will be infinite loop. I want my loops
to run ( 16 * 32767 ) times. The problem is that how can I keep the
track of my "string1" array. for example I tried to the following thing

y=0;
x=0;
while ( y<17)
{
for (; x< COUNT_VAR-1;x++)
{
DataOutBuffer[x]=string1[x];
}
y=y+1;
x=x+1;
Setsignals (PodNumber,0xFF;COUNT_VAR,DataOutBuffer);
}

Now, if 'x' gets to 32767 which is also the "COUNT_VAR" then the "for
loop" will never execute again , it will just run once. My USB function
can only carry "32767" bytes and I need to send 2^19 bytes out.
Please adivce!

Thanks
Regards,
John



Jay Nabonne wrote:
On Wed, 03 Aug 2005 14:24:18 -0700, john wrote:

Hello,

I am trying to send 2^19 data to USB2.0. I am reading the file
"all_sines" and loading it into
an array called "string1". Then, in a for loop I copied it into the USB
data array "Data_outBuffer". The USB can
carry only "32767" bytes at one time. The way I am doing right now
requires 16 for loops to send the
2^19 amount of data. ( 32767 * 16 = 2^19). My USB function SetSignals
can only carry "32767 bytes at one time".


How about:

#define MAX_SEND 32767

int main(int argc, char* argv[])
{
FILE *infile;
int x;

if ( (infile=fopen("C:\\C\\all_sines.bin","rb")) != NULL )
{
// Loop until all data has been sent.
for (;;)
{
unsigned char buffer[MAX_SEND];
// Read up to MAX_SEND bytes.
int bytes = fread(buffer, sizeof(unsigned char), MAX_SEND, infile);
// Make sure we got some data.
if (bytes == 0)
break; // error or eof. use ferror if you care.
// Send the data.
SetSignals(PodNumber, 0xFF, bytes, buffer); // USB Command
}
}
else
printf( "Problem opening the file\n" );
}

- Jay


Aug 4 '05 #3
Hello,

Thanks for the reply! But this will be infinite loop. I want my loops
to run ( 16 * 32767 ) times. The problem is that how can I keep the
track of my "string1" array. for example I tried to the following thing

y=0;
x=0;
while ( y<17)
{
for (; x< COUNT_VAR-1;x++)
{
DataOutBuffer[x]=string1[x];
}
y=y+1;
x=x+1;
Setsignals (PodNumber,0xFF;COUNT_VAR,DataOutBuffer);
}

Now, if 'x' gets to 32767 which is also the "COUNT_VAR" then the "for
loop" will never execute again , it will just run once. My USB function
can only carry "32767" bytes and I need to send 2^19 bytes out.
Please adivce!

Thanks
Regards,
John

Aug 4 '05 #4
On Thu, 04 Aug 2005 09:05:29 -0700, john wrote:
Hello,

Thanks for the reply! But this will be infinite loop.
Only if the file is infinitely long. When the fread reaches end-of-file,
it will break out of the loop. By using the eof condition, you don't need
to hard-code it in your code. The code I provided also doesn't require the
source data to be a multiple of 32767.

I also didn't understand the need to copy the data to a separate buffer in
order to send it, but I assume it's needed for some reason, given your
tenacity in keeping it.
I want my loops
to run ( 16 * 32767 ) times. The problem is that how can I keep the
track of my "string1" array. for example I tried to the following thing

y=0;
x=0;
while ( y<17)
{
for (; x< COUNT_VAR-1;x++)
{
DataOutBuffer[x]=string1[x];
}
y=y+1;
x=x+1;
Setsignals (PodNumber,0xFF;COUNT_VAR,DataOutBuffer);
}

Now, if 'x' gets to 32767 which is also the "COUNT_VAR" then the "for
loop" will never execute again , it will just run once. My USB function
can only carry "32767" bytes and I need to send 2^19 bytes out.
Keep in mind: the buffer you're reading into is only COUNT_VAR bytes long.
You're not reading the entire file into it. So you're going to need to do
multiple reads in order to copy all the data.
Please adivce!


Following your loop above:

for (int x = 0; x < 17; ++x)
{
// Read COUNT_VAR bytes into the source string.
fread(string1, sizeof(unsigned char), COUNT_VAR, infile);
// Copy to the data output buffer.
memcpy(DataOutBuffer, string1, COUNT_VAR);
// Send the data.
Setsignals (PodNumber,0xFF;COUNT_VAR,DataOutBuffer);
}

Aug 4 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Swarup | last post by:
<% Response.buffer = true Dim a a = 0 while a < 1000 Response.write "Im Looping" Response.flush a = a+1 wend
10
by: shank | last post by:
I have a recordset that contains multiple records of product a user is purchasing. For clarity, I converted the recordset fields to variables. I need to take that entire recordset and insert it...
3
by: Deano | last post by:
Here's a good one that keeps defeating me... I have a continuous form that has a date textbox as one of the controls. The user can choose any date they like including one they have already...
3
by: Colleyville Alan | last post by:
I am constructing a SQL command from a function. Some code builds the WHERE clause in a looping structure and passes that as an argument to the SQL-building function. But the results do not...
4
by: Gregory Gadow | last post by:
I've cobbled together a PrinterClass that takes a text file and dumps it to a printer. The app using is has multiple threads, all of which need access to a shared instance. Can someone point me to...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
1
by: projectjakecs | last post by:
Hi, I am working on a ms access database and I am having trouble using a form to create new records in an associative table. Here is the breakdown of my database: Main Table - Computer...
8
by: hunanwarrior | last post by:
I added textbox controls to a form when user selects amount to create from a combobox as follows: 'Load up the combobox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As...
0
by: chitta | last post by:
Hi I have a problem when i am looping on multiple lines. the question as follows If OrderProductLineItem (DocType='OrderCreate'/'OrderChange') < OrderProductLineItem...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.