Multiple looping

Discussion in 'C++' started by john, Aug 3, 2005.

  1. john

    john Guest

    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*/
     
    john, Aug 3, 2005
    #1
    1. Advertising

  2. john

    Jay Nabonne Guest

    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
     
    Jay Nabonne, Aug 4, 2005
    #2
    1. Advertising

  3. john

    john Guest

    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
     
    john, Aug 4, 2005
    #3
  4. john

    john Guest

    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
     
    john, Aug 4, 2005
    #4
  5. john

    Jay Nabonne Guest

    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);
    }
     
    Jay Nabonne, Aug 4, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mothra
    Replies:
    3
    Views:
    436
  2. CJ
    Replies:
    0
    Views:
    1,834
  3. Lance Hoffmeyer
    Replies:
    1
    Views:
    294
    Peter Otten
    Feb 16, 2007
  4. john

    Multiple looping

    john, Aug 3, 2005, in forum: C Programming
    Replies:
    5
    Views:
    331
    Nick Keighley
    Aug 5, 2005
  5. Replies:
    5
    Views:
    283
Loading...

Share This Page