Announcement

Collapse
No announcement yet.

Is there a better way to manage data from UART communication?

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

  • Is there a better way to manage data from UART communication?

    My screen freezes every time i dont receive any string, the string im receiving goes like this ;9.5,1:0 after that i save them into tokens . Any idea how to do it better? is there any uart.readable()??
    Code:
        com_Init(Id, 12, ';' );
            repeat
            until(com_Count() == 1);
    
            com_Init(Siluet, 12, '.' );
            repeat
            until(com_Count() == 1);
    
            com_Init(Led, 12, ',' );
            repeat
            until(com_Count() == 1);
    
            com_Init(Status, 12, ':' );
            repeat
            until(com_Count() == 1);
    Last edited by Elektrische; 29th March 2017, 05:24 AM.

  • #2
    why not use the initial comms init and just read the data if your input is always the same, what are you trying to do, receive ";9.5,1:0" ?
    if the input always starts with ";" you only need to initialize the comms the first time with the ";" qualifier. data will only be written to the buffer when the qualifier is received. then you can just read in the bytes if theyre pattern is always the same. You need to use serin() to receive the bytes

    Tony
    Last edited by tonton81; 26th March 2017, 06:18 AM.

    Comment


    • Elektrische
      Elektrische commented
      Editing a comment
      Can you show me an example? please.

  • #3
    im not fluent in 4dgl but you could do somthing like
    var data[8]; com_Init(Id, 12, ';' ); // this will ignore all data until the ';' qualifier is read data[0] := serin(); <-- read the byte into the buffer data[1] := serin(); <-- read the byte into the next buffer etc, or add them to a string. then i would presume you reinitialize the com init qualifier again to start over for waiting for next ';' qualifier

    Comment


    • #4
      Hi Elektrische,

      This forum post might also offer some help:

      Read Serial COM1 and Parse String
      http://forum.4dsystems.com.au/forum/...d-parse-string

      you can also try something like storing the received values like in post #14, then use/print specific array indexes/element for your application. This might prevent freezing and having to wait for a string to be sent for other parts of the code to run.

      Regards,

      Comment


      • #5
        Is working right now, would you add something else so it doesn´t loose any byte? . The screen is receiving the string from an xbee and on the other side 4 xbee´s will be sending the same structured string but with different values, for example a led will turn green if the module is active. the string goes like that:
        $;9.2,1:1\r
        ($;9.Xbee Number(1,2,3,4),Status LED(1,0),Action(1,0)\r


        Code:
        var combuf[40]; // buffer
        var dataArray[20]; // storage for desired received bytes
        var Data1[20];
        var Data2[20];
        var Data3[20];
        var Data4[20];
        
        var n, ch;
        
        
        func main()
        gfx_ScreenMode(PORTRAIT_R) ; // set screen mode in reverse landscape
        // Xb.printf("10;9.%s,%i:%i",Id,Lock,Position);
        setbaud(BAUD_1200);
        com_Init(combuf,30,'$');
        n := 0;
        
        while(1)
        
        ch := serin();
        
        if (ch > 0)
           dataArray[n] := ch;
           n++;
        endif
        
        
        if (ch=='\r')
          gfx_MoveTo(0,30);
          txt_FGcolour(BLACK);
          gfx_MoveTo(0,30); txt_FGcolour(WHITE);
        
          if (dataArray[0]==';')
            to (Data1); putch(dataArray[1]);
          endif
          if (dataArray[2]=='.')
            to (Data2); putch(dataArray[3]);
          endif
          if (dataArray[4]==',')
            to (Data3); putch(dataArray[5]);
          endif
          if (dataArray[6]==':')
            to (Data4); putch(dataArray[7]);
          endif
        
          n:=0; // resets the new array index to 0
        
          print("String1 ",[STR]combuf,"\n") ;
        
          print("Data1 ",[STR]Data1, "\n") ;
        
          print("Data2 ",[STR]Data2, "\n") ;
        
          print("Data3 ",[STR]Data3, "\n") ;
        
          print("Data4 ",[STR]Data4, "\n") ;
        
          com_Init(combuf,20,'$'); // requires another '$' character for serin1
          gfx_MoveTo(0,0);
        
        endif
        
        wend
        endfunc
        Last edited by Elektrische; 29th March 2017, 05:41 AM.

        Comment


        • #6
          Hi Elektrische,

          Maybe you could add a counter variable to check if you have received the correct number of bytes.

          You could also check the number of values inside your 'dataArray' once it has it has received a complete desired data from the transmitting side and clear it once you're done with a process.

          you could also use other serial functions like com_Count() to check if there are remaining characters in the communications buffer, etc..

          Serial functions starting on page 238: (for Diablo)
          http://www.4dsystems.com.au/productp...ons_R_1_16.pdf

          Best Regards,

          Comment


          • Elektrische
            Elektrische commented
            Editing a comment
            Would you mind helping me on this with my code?

        • #7
          Hi Elektrische,

          Yes, you could post your encountered problems here (if you have any) so we could help

          The suggestion in this forum post might help:

          Clearing an Array for re-use
          http://forum.4dsystems.com.au/forum/...ray-for-re-use

          Regards,

          Comment

          Working...
          X