Page pageSerialTest
    Attributes
        ID                                  : 0
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        Width                               : 480
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Locked                              : no
        Swide up page ID                    : disabled
        Swide down page ID                  : disabled
        Swide left page ID                  : disabled
        Swide right page ID                 : disabled
        Fill                                : solid color
        Back. Color                         : 65535
    
    Events
        Preinitialize Event
            //vis p0,0
            //recmod=1
            //bauds=115200
        
Number nCrcRecv
    Attributes
        ID                                  : 5
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 160
        y coordinate                        : 0
        Width                               : 88
        Height                              : 29
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : flat
        Associated Keyboard                 : none
        Font ID                             : 5
        Back. Color                         : 65535
        Font Color                          : 0
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        Value                               : 0
        Significant digits shown            : all
        Format                              : decimal with digit grouping
        Word wrap                           : enabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
Number nCrcCalc
    Attributes
        ID                                  : 6
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 159
        y coordinate                        : 31
        Width                               : 90
        Height                              : 30
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : flat
        Associated Keyboard                 : none
        Font ID                             : 5
        Back. Color                         : 65535
        Font Color                          : 0
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        Value                               : 0
        Significant digits shown            : all
        Format                              : decimal with digit grouping
        Word wrap                           : enabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
Number nIt
    Attributes
        ID                                  : 11
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 378
        y coordinate                        : 234
        Width                               : 52
        Height                              : 30
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : flat
        Associated Keyboard                 : none
        Font ID                             : 5
        Back. Color                         : 65535
        Font Color                          : 0
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        Value                               : 0
        Significant digits shown            : all
        Format                              : decimal
        Word wrap                           : enabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
Number nLength
    Attributes
        ID                                  : 12
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 15
        y coordinate                        : 215
        Width                               : 100
        Height                              : 30
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : flat
        Associated Keyboard                 : none
        Font ID                             : 0
        Back. Color                         : 65535
        Font Color                          : 0
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        Value                               : 0
        Significant digits shown            : all
        Format                              : decimal
        Word wrap                           : enabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
Text tStatus
    Attributes
        ID                                  : 3
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 160
        y coordinate                        : 60
        Width                               : 271
        Height                              : 90
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : flat
        Associated Keyboard                 : none
        Font ID                             : 5
        Back. Color                         : 65535
        Font Color                          : 0
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        Input Type                          : character
        Text                                : newtxt
        Max. Text Size                      : 100
        Word wrap                           : enabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
Text tCmd
    Attributes
        ID                                  : 4
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 160
        y coordinate                        : 151
        Width                               : 272
        Height                              : 80
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : flat
        Associated Keyboard                 : none
        Font ID                             : 5
        Back. Color                         : 65535
        Font Color                          : 0
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        Input Type                          : character
        Text                                : newtxt
        Max. Text Size                      : 500
        Word wrap                           : enabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
Text tBuffer
    Attributes
        ID                                  : 7
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 7
        y coordinate                        : 270
        Width                               : 435
        Height                              : 30
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : flat
        Associated Keyboard                 : none
        Font ID                             : 5
        Back. Color                         : 65535
        Font Color                          : 0
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        Input Type                          : character
        Text                                : newtxt
        Max. Text Size                      : 100
        Word wrap                           : disabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
Text tDesc
    Attributes
        ID                                  : 8
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 0
        y coordinate                        : 0
        Width                               : 159
        Height                              : 204
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : flat
        Associated Keyboard                 : none
        Font ID                             : 5
        Back. Color                         : 65535
        Font Color                          : 0
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        Input Type                          : character
        Text                                : CRC Recv:\r\nCRC Calc:\r\n\r\nStatus\r\n\r\nCommand
        Max. Text Size                      : 60
        Word wrap                           : disabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
Picture p0
    Attributes
        ID                                  : 1
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 0
        y coordinate                        : 0
        Width                               : 479
        Height                              : 319
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Picture ID                          : 0
    
Button b0
    Attributes
        ID                                  : 9
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 334
        y coordinate                        : 9
        Width                               : 46
        Height                              : 46
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : 3D auto
        Font ID                             : 5
        Back. Color                         : 50712
        Back. Picture ID (Pressed)          : 0
        Back. Color (Pressed)               : 1024
        Font Color (Unpressed)              : 0
        Font Color (Pressed)                : 65535
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        State                               : unpressed
        Text                                : bck
        Max. Text Size                      : 10
        Word wrap                           : disabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
    Events
        Touch Press Event
            recmod=0
            page pageTest
        
Button b1
    Attributes
        ID                                  : 10
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Opacity                             : 127
        x coordinate                        : 392
        y coordinate                        : 10
        Width                               : 48
        Height                              : 48
        Effect                              : load
        Effect Priority                     : 0
        Effect Time                         : 300
        Fill                                : solid color
        Style                               : 3D auto
        Font ID                             : 5
        Back. Color                         : 50712
        Back. Picture ID (Pressed)          : 0
        Back. Color (Pressed)               : 1024
        Font Color (Unpressed)              : 0
        Font Color (Pressed)                : 65535
        Horizontal Alignment                : center
        Vertical Alignment                  : center
        State                               : unpressed
        Text                                : ON
        Max. Text Size                      : 10
        Word wrap                           : disabled
        Horizontal Spacing                  : 0
        Vertical Spacing                    : 0
    
    Events
        Touch Press Event
            recmod=1
            bauds=115200
        
Timer tmSerial
    Attributes
        ID         : 2
        Scope      : local
        Period (ms): 400
        Enabled    : yes
    
    Events
        Timer Event
            // data available
            if(usize>1)
            {
                bufferPos=0
                while(bufferPos<usize)
                {
                    // check for 0x55 0xBB - Command Init Secuence
                    if(u[bufferPos]==187&&u[bufferPos-1]==85)
                    {
                        //remove garbage at the start of the buffer if there's any to free buffer for command
                        if(u[bufferPos]!=1)
                        {
                            udelete bufferPos-1
                        }
                        tStatus.txt="init command detected"
                        //instruction is now aligned with buffer, because we deleted garbage before instrcution
                        //get length after init sequence (check if there are more than to bytes in buffer)
                        if(3<usize)
                        {
                            // check if serial buffer has reached the announced length
                            //payloadLength=u[2]
                            ucopy payloadLength,2,2,0
                            nLength.val=payloadLength
                            // we are only checking payload length so we have to skip first 3 bytes (init+payload length) (-1 because of < instead of <=)
                            payloadLength+=3
                            // payload length does also not contain crc, so we are adding another 2 bytes for crc
                            payloadLength+=2
                            if(payloadLength<usize)
                            {
                                tStatus.txt="reached"
                                // calculate crc
                                crcrest 1,0xFFFF
                                // payload length -1 to exclude crc
                                crcputu 0,payloadLength-1 //u[2]+3
                                // get recived crc to be able to compare it
                                ucopy recvCrc,payloadLength-1,2,0
                                nCrcRecv.val=recvCrc
                                // compare crc with recived value
                                if(crcval==recvCrc)
                                {
                                    tStatus.txt="crc ok"
                                    nIt.val=nIt.val+1
                                    ucopy tCmd.txt,4,payloadLength-5,0
                                    udelete payloadLength-1
                                    bufferPos=0
                                    // here is the location where acual code should be
                                }
                                // copy calculated crc and recived crc to nCrcRecv and nCrcCalc, for debugging
                                nCrcRecv.val=recvCrc
                                nCrcCalc.val=crcval
                                //ucopy nCrcCalc.val,u[2]+3,3,0
                            }
                        }
                    }
                    // next character
                    bufferPos++
                }
                if(bufferPos==usize)
                {
                    // copy whole buffer to tCmd.txt, for debugging
                    //ucopy tBuffer.txt,0,usize,0
                    // ucopy nIt.val,0,usize,0
                    // clear whole buffer
                    //code_c
                    //bufferPos=0
                }
            }
        
