Announcement

Collapse
No announcement yet.

to(outstream) not working as it should

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • to(outstream) not working as it should

    Hi All,

    This is my first post here so please forgive any inadvertent transgressions.

    I'm using a uLCD-43PT_GFX2 and the sample program FAT16append as I'm trying to get to grips with writing data to the uSD card. It's not going well.

    The following code should write a number, zero padded to five characters, followed by a string to a file count number of times. What it actually does is write 5 null characters to the file followed by " Hello XY " count number of times.


    var b, count, mybuf, mybuf2;
    var handle1;


    mybuf := mem_Alloc(100);
    mybuf2 := mem_Alloc(20);


    while (count--)
    handle1 := file_Open(filename, 'a');
    report(2);

    to(mybuf2); print([DEC5Z] count);
    b := file_Write(mybuf2, 5, handle1 ); // write the 5 bytes, return byte count written
    report(4);
    if (b != 5) goto error1; // error if not same as written

    mybuf := " Hello XY "; // this is the string we will be appending
    b := file_Write( mybuf, 11, handle1 ); // write the 12 bytes, return byte count written
    report(3);
    if (b != 11) goto error1;

    putch('.');

    file_Close(handle1);
    report(5);
    wend

    I have tried many work arounds but nothing does the trick. Strangely, the sample program fat16rw works just fine but it doesn't have the number business.

    Also, am I missing something, is there a simple IntToStr type function anywhere?

    I must be blunt and say that I'm finding this programming language a bit clumsy and flaky, neither C nor Pascal and a bit light on the standard functions.

    But I've not given up yet!

    Sharpie

  • #2
    Just to follow up my own post, this code, from fat16rw with a couple of changes by me, does work :-

    Cnt := 10; // iterations

    buff := mem_Alloc(1000);

    Cnt := 10; // iterations

    str := "aardvarks "; // our test string
    chr := 'JK'; // our test character

    //i := -32768;
    i := 0;
    while (i < Cnt)
    // test the writing of a string
    len := str_Length(str);
    if (len != file_Write(str, len, h)) DEBUG("Error writing ", len ," bytes\n");
    report(2);

    if (!file_PutC(chr, h)) DEBUG("Error writing 1 char\n");
    report(3);

    if (!file_PutW('XY', h)) DEBUG("Error writing 1 word\n");
    report(4);

    if (!file_PutW(0x3231, h)) DEBUG("Error writing 1 word\n");
    report(5);

    if (!file_PutW(chr, h)) DEBUG("Error writing 1 word\n");
    report(6);

    to(buff); print(" ",[DEC5Z] i," ");
    if (!file_PutS(buff, h)) DEBUG("Error writing string\n");
    report(7);

    if (!file_PutS("artful ", h)) DEBUG("Error writing string\n");
    report(8);

    i++;
    DEBUG("Count: ", i, "\n");

    wend // while iterations

    Any ideas why one eorks but the other doesn't?

    Sharpie

    Comment


    • #3


      Yes there are some subtle differences between the example code and your sample.

      In 4DGL, RAM is word aligned, and certain functions need to raise a string pointer for the argument.

      In the case of....
      to(buff); print(" ",[DEC5Z] i," ");
      file_PutS(buff, h);

      The string 'buff' will be stored correctly, as the file_Puts function does a byte pointer conversion automatically,

      however,
      to(mybuf2); print([DEC5Z] count);
      file_Write(mybuf2, 5, handle1 );

      will not work, as file_Write expects a byte pointer.


      Raising a string pointer for file_Write however, will work.
      to(mybuf2); print([DEC5Z] count);
      file_Write(str_Ptr(mybuf2), 5, handle1 );

      Note that in the case of:-
      str := "aardvarks ";

      str is a pointer to a static string, and static strings are already byte aligned
      (the string is actually stored in code segment, which is byte aligned)

      so,
      file_Write(str, 9, handle1 );

      will work as expected.
      Regards,
      Dave

      Comment


      • #4


        Thanks for the reply Dave. I haven't a chance to try your suggestions until Monday but I expect them to work as they make sense.

        What doesn't make sense is that it was one of your sample programs, FAT16append, that didn't work in the first place. Any ideas why that may be?

        Regards,

        Paul

        Comment


        • #5


          Yes you are quite right, FAT16append.4DG does not work correctly, but FAT16rw.4DG is aok.

          The problem with FAT16append.4DG has 2 lines that need changing:-


          b := file_Write( str_Ptr(mybuf2), 5, handle1 ); // write the 5 bytes, return byte count written
          and
          b := file_Write( str_Ptr(mybuf), 11, handle1 ); // write the 12 bytes, return byte count written

          (note the addition of the str_Ptr functions)

          The str_Ptr function basically 'doubles' the address of the pointer to get the byte aligned address.
          Regards,
          Dave

          Comment


          • #6


            Hi Dave,

            It's all working now on that front thanks and I have enough to enable me to continue with my project.

            Kind regards,

            Sharpie

            Comment

            Working...
            X