Announcement

Collapse
No announcement yet.

Split a message

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

  • Split a message

    Hello everybody I've got a question about splitting a message. In fact, I'm reading on the serial port RS232 a message who contains some data (about weather). The relevant message is like this : 04/06/2010-10h09m26s,TEMP:°C,PRES:000,HYG:00%,LAT00,LONG:00,ALT:000,ETAT:000,ENT:000

    This message is displayed correctly on a little 32028-P1T screen.
    I want to know if I could (with 4DGL) split the message for each ",
    " and make a newline. To better explain, I wanna know if I can obtain a message like this one in 4DGL :

    04/06/2010-10h09m26s,

    TEMP:°C,
    PRES:000,
    HYG:00%,
    LAT00,
    LONG:0,
    ALT:000,
    ETAT:000,
    ENT:00

    I'm using this code for reading data on my serial port :

    Code:
    var ch;com_Init(combuf,200,0);gfx_Cls();txt_Set(FONT_SIZE, FONT4);txt_Set(TEXT_COLOUR, 0xFFFF00);setbaud(BAUD_115200);to(COM7); print("   Station D'Infos     Meteorologiques\n\n\n");txt_Set(FONT_SIZE, FONT3);repeatch := serin();if (ch != -1)print( [CHR] ch);   // Affichage sur l'ecran de la touche presseeendifforever
    Looking for some help please

    Regards
    Nicolas

  • #2
    I don't know much about 4DGL since I'm a newbie. I believe you can check the character before you print it using print(). If the character is ',' , print a new line character instead. (or move the position of printing)
    Code:
    if ( ch!=-1) if (ch== 44) print("\n"); // something like this end ifprint( [CHR] ch);end if

    Comment


    • #3


      Hello
      Thanks for your help but your solution doesn't works

      Comment


      • #4
        Hi Nicolas, there's a few ways of doing it.Simplest way would be:-

        func main()

        var ch, count;
        gfx_Cls();
        txt_Set(FONT_SIZE, FONT3);
        txt_Set(TEXT_COLOUR, 0xFFFF);
        setbaud(BAUD_115200);
        txt_Set(FONT_SIZE, FONT3);

        com_Init(combuf,200,0); // use buffered comms

        to(COM0); print(" Station D'Infos Meteorologiques\n\n\n");

        count := 0;
        while(count++ < MESSAGE_LENGTH)

        while((ch:=serin()) < 0); // wait for a character

        if(ch == ',') ch := '\n'; // change comma's to newlines

        putch(ch); // print the character

        //if(ch == ',') putch('\n'); // OR newline after comma


        wend

        print("\nDONE");
        repeat forever

        endfunc


        A better way would be:-

        #constant BUFSIZE 84
        // 84 character string
        // 04/06/2010-10h09m26s,TEMP:°C,PRES:000,HYG:00%,LAT00,LONG:00,ALT:000,ETAT:000,ENT:000

        var combuf[50]; // serial input buffer, up to 100 chars

        //=========================================================
        // reset the input buffer to the required count
        //=========================================================
        func resetRXbuffer(var count)
        com_Init(combuf,count,0);
        to(COM0); print(" Station D'Infos Meteorologiques\n\n\n"); // send request to host
        endfunc


        func main()

        var count, p;
        gfx_Cls();
        txt_Set(FONT_SIZE, FONT3);
        txt_Set(TEXT_COLOUR, 0xFFFF);
        setbaud(BAUD_115200);
        txt_Set(FONT_SIZE, FONT3);

        repeat

        // send message to host, set expected string length
        resetRXbuffer(BUFSIZE);

        // wait for buffer to fill to required level
        repeat
        txt_MoveCursor(3,3);
        count:=com_Count();
        print("Count:",[DEC2] count," ");

        // can do other things here while waiting for buffer to fill....

        until (com_Full());


        // scan the buffer, converting comma's to newlines
        p := str_Ptr(combuf);
        while(count--)
        if(str_GetByte(p) == ',') str_PutByte(p, '\n');
        p++;
        wend

        // print the buffer
        txt_MoveCursor(5,3);
        print([STR] combuf);

        forever

        endfunc





        Regards,
        Dave

        Comment

        Working...
        X