Announcement

Collapse
No announcement yet.

UART reveice string problem

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

  • UART reveice string problem

    Hello,

    I have the uOLED-160-G2 and want to display 3 strings, received from the uart.

    I thought about a format like this: 'qualifier','lenght',"string1",'#',"string2",'#',"string3".

    Is is possible to receive a message directly into an array?

    Is there a easier way to split a string into parts?

    Here is my forst try:
    Code:
    var i,j;
    var sign1;
    var sign2;
    var laenge;
    var com_buffer[100];
    var buffer[60];
    var word1[20];
    var word2[20];
    var word3[20];
    var test;
    
    func main()
    
        gfx_Set(SCREEN_MODE,PORTRAIT_R) ;
        gfx_BGcolour(BLACK);
        gfx_Cls();
        setbaud(25);
        com_Init(com_buffer,0,':');
    
        txt_BGcolour(BLACK);
        txt_FGcolour(WHITE);
    
        repeat
    
        if(com_Count())                              //receive data
            repeat
            until(com_Full());
    
            test := com_Count();
    
            laenge := 0;
    
            for(i:=0;laenge<test;i++)
                buffer[i] := serin();
                laenge++;
                if(laenge>=test) break;
                buffer[i] |= serin()*256;
                laenge++;
            next
    
            com_Init(com_buffer,0,':');              //re-init
    
            j := 0;
            i := 0;
    
            repeat                                   //Find sign 1
                if(buffer[i]&0x00FF == '#')
                    sign1:=j;
                    test := 1;
                    break;
                endif
                j++;
                if(buffer[i]/256 == '#')
                    sign1:=j;
                    test := 0;
                    break;
                endif
                i++;
                j++;
            until(j<laenge);
    
            j++;
    
            repeat                                   //Find sign 2
                if(test == 0)
                    if(buffer[i]&0x00FF == '#')
                        sign2:=j;
                        break;
                    endif
                endif
                test := 0;
                j++;
                if(buffer[i]/256 == '#')
                    sign2:=j;
                    break;
                endif
                i++;
                j++;
            until(j<laenge);
    
            *word1 := [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
            *word2 := [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
            *word3 := [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    
            for(i:=0;i<sign1;i++)                    //split string 1
                word1[i] := buffer[i];
            next
    
    
            for(i:=sign1+1;i<sign2;i++)              //split string 2
                word2[i-sign1-1] := buffer[i];
            next
    
    
            for(i:=sign2+1;i<laenge;i++)             //split string 3
                word3[i-sign2-1] := buffer[i];
            next
    
    
            gfx_Cls();
    
            txt_Width(2);
            txt_Height(5);
            txt_Xgap(0);
            txt_Bold(1);
    
            gfx_MoveTo(64-(strwidth(word1)/2), 20);  //vertical-align: middle
            putstr(word1);                           //draw string 1
    
            txt_Width(1);
            txt_Height(3);
            txt_Xgap(1);
    
            gfx_MoveTo(64-(strwidth(word2)/2), 80);  //vertical-align: middle
            putstr(word2);                           //draw string 2
    
            gfx_MoveTo(64-(strwidth(word3)/2), 110);  //vertical-align: middle
            putstr(word3);                           //draw string 3
    
        endif
    
        forever
    endfunc
    Can someone help me?

  • #2


    That looks pretty good for a first try, I can spot a few things wrong with it though

    Strings must 'add up to' only exactly comms buffer size.
    Length in stream format ignored/lost

    The stream format of 'qualifier','lenght',"string1",'#',"string2",'#',"string3" doesn't specify the terminator for the last string, by 'try' accepts # or 0x00 for all strings.

    So my take on your code is
    Code:
    #platform "GOLDELOX"
    
    #inherit "4DGL_16bitColours.fnc"
    
    var i,j,k ;
    var com_buffer[20];                 // probably doesdn't neet to be this big as we are processing bytes as they are received
    var *bp;
    var word1[20];
    var word2[20];
    var word3[20];
    
    func serinw()
        var i ;
        while ((i := serin()) < 0) ;
        if (i == '#') i := 0 ;          // because re have defined # as the inter string delimeter
        return i ;
    endfunc
    
    func main()
    
        gfx_Set(SCREEN_MODE,PORTRAIT_R) ;
        gfx_BGcolour(BLACK);
        gfx_Cls();
        setbaud(BAUD_115200);           // clearer than saying 25
        com_Init(com_buffer,40,':');    // need length specifier
    
        txt_BGcolour(BLACK);
        txt_FGcolour(WHITE);                                                                                                                                                           
    
        repeat
    
        if(com_Count())                 // anything received, we are on our three strings
            for (i := 0; i < 3; i++)    // three strings to fetch
                if (i == 0)             // select  string dest
                    bp := word1 ;
                else if (i == 1)
                    bp := word2 ;
                else
                    bp := word3 ;
                endif
                j := 0 ;
                repeat
                    k := serinw();
                    bp[j] := k;
                    if (k == 0)
                        break;
                    endif
                    k := serinw();
                    if (k != 0)
                        bp[j] += k << 8;
                    endif
                    j++;
                until (k == 0);
            next
            com_Init(com_buffer,40,':');    // reinit for next packet
    
            gfx_Cls();
    
            txt_Width(2);
            txt_Height(5);
            txt_Xgap(0);
            txt_Bold(1);
    
            gfx_MoveTo(64-(strwidth(word1)/2), 20);  //vertical-align: middle
            putstr(word1);                           //draw string 1
    
            txt_Width(1);
            txt_Height(3);
            txt_Xgap(1);
    
            gfx_MoveTo(64-(strwidth(word2)/2), 80);  //vertical-align: middle
            putstr(word2);                           //draw string 2
    
            gfx_MoveTo(64-(strwidth(word3)/2), 110);  //vertical-align: middle
            putstr(word3);                           //draw string 3
    
        endif
    
        forever
    endfunc
    Mark

    Comment


    • #3


      Thank you very much.

      That is exactly what I need.

      Normally I'm programming PICs in real C, there it is much easier to split a string


      Thanks for helping
      Stefan

      Comment

      Working...
      X