Page pageSerialTest
    Attributes
        ID                                  : 0
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Locked                              : no
        Swide up page ID                    : disabled
        Swide down page ID                  : disabled
        Swide left page ID                  : disabled
        Swide right page ID                 : disabled
    
    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
        Associated Keyboard                 : none
        Value                               : 0
    
Number nCrcCalc
    Attributes
        ID                                  : 6
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Associated Keyboard                 : none
        Value                               : 0
    
Number nIt
    Attributes
        ID                                  : 11
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Associated Keyboard                 : none
        Value                               : 0
    
Number nLength
    Attributes
        ID                                  : 12
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Associated Keyboard                 : none
        Value                               : 0
    
Text tStatus
    Attributes
        ID                                  : 3
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Associated Keyboard                 : none
        Text                                : newtxt
        Max. Text Size                      : 100
    
Text tCmd
    Attributes
        ID                                  : 4
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Associated Keyboard                 : none
        Text                                : newtxt
        Max. Text Size                      : 500
    
Text tBuffer
    Attributes
        ID                                  : 7
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Associated Keyboard                 : none
        Text                                : newtxt
        Max. Text Size                      : 100
    
Text tDesc
    Attributes
        ID                                  : 8
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        Associated Keyboard                 : none
        Text                                : CRC Recv:\r\nCRC Calc:\r\n\r\nStatus\r\n\r\nCommand
        Max. Text Size                      : 60
    
Picture p0
    Attributes
        ID                                  : 1
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
    
Button b0
    Attributes
        ID                                  : 9
        Scope                               : local
        Dragging                            : 0
        Disable release event after dragging: 0
        Send Component ID                   : disabled
        State                               : unpressed
        Text                                : bck
        Max. Text Size                      : 10
    
    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
        State                               : unpressed
        Text                                : ON
        Max. Text Size                      : 10
    
    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
                }
            }
        
