Announcement

Collapse
No announcement yet.

Problem: reading characters from USART

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

  • Problem: reading characters from USART

    Hello everyone,

    I need your coding expertise to figure out what I am doing wrong here.

    What I basically want to do is to send 16 characters through the terminal , and I want the module to break down those 16 characters into 4 groups and display each on separate lines according to the order.

    ex. if i send QWERTYUIOPLKJHGF through the terminal, I want the LCD module to display it like this:

    QWER
    TYUI
    OPLK
    JHGF
    .

    I wrote a coding for this, but each time the LCD output the characters, it outputs only the last 4 characters I entered, on all the 4 lines. For example if I enter the same characters as above. The LCD outputs:
    JHGF
    JHGF
    JHGF
    JHGF
    .

    Here is my coding, if you could identify the problem in it, I will be really grateful, because I've spent about 2 days now re-writing the code in different ways, but I got the same result. Maybe I am using the internal functions wrongly. Please let me know.

    My Coding:

    func main()

    setbaud(BAUD_9600);

    var combuf[10]; // a buffer for up to 20 characters
    var array[10];

    var n:=0;

    putstr("Messages");
    com_Init(combuf, 4, 0); // initialize circular queue of 7 bytes,


    repeat

    if (com_Full() &amp;&amp; n<5)

    array[n]:=combuf;
    n++;
    com_Init(combuf,4,0);
    endif

    if(n==4)
    txt_MoveCursor(1,0);
    putstr(array[0]);
    print(" 1");
    print("\n");
    putstr(array[1]);
    print(" 2");
    print("\n");
    putstr(array[2]);
    print(" 3");
    print("\n");
    putstr(array[3]);
    print(" 4");
    print("\n");

    n:=0;
    endif

    forever
    //=================================================

    endfunc



  • #2


    combuf is an internal buffer to store bufferred characters, there is nothing useful that can be achieved by trying to use it directly.

    use, say, array[n]:= serin() ;
    Mark

    Comment


    • #3


      hi, thank you for your quick reply.

      the problem with what u described is that I will be able to store only the first character of a particular line in that array[n].

      Is there a way to read combuf array and store its characters as a string ?

      thanks again.

      Comment


      • #4


        please also note that in the "GOLDELOX-GFX2-4DGL-Internal-Functions-rev2" on page 142, they mention that " If no characters are removed from the buffer until the COM_FULL flag (which can be read with the com_Full() function) becomes set, it is guaranteed that the bytes will be ordered in the buffer from the start position, therefore, the buffer can be treated as an array and can be read directly without using serin() at all."

        Comment


        • #5
          If you wish to read characters from the array without using serin() you will need to make a function to unpack the characters.
          All 4DGL variable and arrays are 16bit, so when you use the buffered comms function, the characters are packed 2 per location.

          // function to get a byte from a packed buffer
          func get_byte(var array, var idx)
          var b ;
          if (idx&amp;1)
          b := array[idx>>1] >> 8 ;
          else
          b := array[idx>>1] &amp; 0xff ;
          endif
          return b ;
          endfunc

          alterantively if you just want to place on 4 lines like your example

          var combuf[10]; // a buffer for up to 20 characters
          setbaud(BAUD_9600);

          com_Init(combuf, 16, 0); // initialize buffer,

          while (!com_Full() )
          // do something while waiting
          wend

          // buffer now has the 16 characters

          for (x:=0; x<16; x++)
          putch(serin());
          if((x&amp;3)==3) putch('\n'); // newline after every 4 chars
          next

          repeat forever // done





          Regards,
          Dave

          Comment


          • #6


            hi dave, thanks for your reply and suggestions.
            The problem is that I need to process the data as well later on, so I can't use your second method.

            Could you please explain to me why I can't assign combuf to seperate locations in an array.

            I do not have much experience with embedded application programming, so excuse me if that was a dumb question.

            Comment


            • #7


              I'm pretty sure either this will work

              [quote]
              while (!com_Full() )
              // do something while waiting
              wend

              // buffer now has the 4 characters

              for (x:=0; x
              Mark

              Comment


              • #8
                hi,
                Please take a look at this coding :

                repeat

                if (com_Full() &amp;&amp; n<7)
                txt_MoveCursor(n,0);
                putstr(combuf);
                // print("\n");
                n++;
                com_Init(combuf,4,0);
                endif

                forever

                With the above coding, I can easily display 16 characters divided into 4 groups. But what I want to do is instead of directly outputting the combuf to screen with the putstr(combuf); in the loop. I want to assign the current instance of the combuf to an array[n], with array[n]:=combuf; , then after the whole packet is received, I want to be able to print it like this:
                putstr(array[0]);
                putstr(array[1]);
                putstr(array[2]);
                putstr(array[3]);

                Is that possible ?

                Comment


                • #9
                  Of course it's possible, but you need to understand what's really going on

                  variables in 4DGL are currently 16 bits or 2 bytes or integers or words.

                  An array is just an array of the above variables.

                  So array[n]:=combuf; merely assigns the first 2 bytes of combuf into the 2 bytes of array element n.

                  You may, I haven't tested it, but I think it will be ok, do something like
                  var array1[3] ;
                  var array2[3] ;
                  var array3[3] ;
                  var array4[3] ;
                  array1[0] := combuf[0] ; // first two characters
                  array1[1] := combuf[1] ; // next two characters
                  array1[2] := 0 ;
                  array1[0] := combuf[2] ; // first two characters of second group
                  array1[1] := combuf[3] ; // next two characters
                  array1[2] := 0 ;
                  array1[0] := combuf[4] ; // first two characters of third group
                  array1[1] := combuf[5] ; // next two characters
                  array1[2] := 0 ;
                  array1[0] := combuf[6] ; // first two characters of fourth group
                  array1[1] := combuf[7] ; // next two characters
                  array1[2] := 0 ;

                  putstr(array1) ;
                  print("\n") ;
                  putstr(array2) ;
                  print("\n") ;
                  putstr(array3) ;
                  print("\n") ;
                  putstr(array4) ;
                  print("\n") ;

                  or in one line

                  print([STR] array1, "\n", [STR] array2, "\n", [STR] array3, "\n", [STR] array4, "\n",) ;

                  I hope that helps to clear things up, but really I don't like the code
                  Mark

                  Comment


                  • #10


                    so what you're saying is that if i assign array[0]:-combuf; and then putstr(array[0]); and input ABCD into that program. It will only display the first 2 characters? as in AB?

                    Comment


                    • #11


                      You'd have to input ABCD first.

                      The assignment will only assign the first two characters, so assuming array[1] contains the required null, then it will only display the first two characters.
                      Mark

                      Comment


                      • #12


                        but this is not the case. Because if I assign it like this: array[0]:="HELLO"; and putstr(array[0]); it will display HELLO on the display. How do you explain that?

                        Comment


                        • #13


                          That's because the compiler recognises what you are trying to do because of the statements you used.
                          Mark

                          Comment


                          • #14


                            So where does it actually store the other letters? as in CD?

                            Comment


                            • #15
                              What you need to do if you wish to retain the data is to keep a buffer for each entry,

                              try this approach

                              var buf1[3];
                              var buf2[3];
                              var buf3[3];
                              var buf4[3];

                              var bufptrs[4];

                              func main()
                              var n, p;

                              bufptrs[0] := buf1; // initialize the buffer pointer list
                              bufptrs[1] := buf2;
                              bufptrs[2] := buf3;
                              bufptrs[3] := buf4;

                              print("Enter 4 x 4 character entries\n");

                              while(n<4)

                              com_Init(bufptrs[n],4,0);

                              while(!com_Full())
                              // do something
                              wend

                              n++;
                              print("Entry ",n, " received\n");

                              wend

                              for(n:=0; n<4; n++)
                              putstr(bufptrs[n]);
                              putch('\n');
                              next

                              repeat forever



                              endfunc

                              Regards,
                              Dave

                              Comment

                              Working...
                              X