Knowledge Base

Test 2: Send two name-value pairs, display the response

Test 2 sends the query string “a=5&b=-9“, and then displays the server response, which is the sum a+b, or -4. The code is written to make it easy for you to send other values of a and b.

Prepare

The first bit is the same as in test 1, where we Idle the SX10509 and then clear its network Tx buffer.

Test2:
        GoSub           SCH_Idle                ;Force the SCH into idle
        WaitForST       sSCH_GotEcho            ;Wait for the command to be echoed
        GoSub           SCH_ClearTx             ;Make sure the SX10509 Tx buffer is clear
        WaitForST       sSCH_GotEcho            ;Wait for the command to be echoed

We have to clear the buffer before writing to it, because writes to the SX10509 network Tx buffer always append to whatever is already there.

Write the query string

The next block of code writes “a=5&b=-9” to the SX10509 network Tx buffer. It does this in several small bits to make it easy for you to hack the code, and also to illustrate an important aspect of the iiPrintText and similar SPLat instructions.

        LoadI           0                       ;Write offset pointer for iiPrintText and similar instructions
        iiPrintText     abTxData,"a="           ;Write the first name
        LoadX           5                       ;Set the first value, use an integer
        iiuPrintXVW     abTxData                ;Write the first value
        iiPrintText     abTxData,"&b="          ;Write delimiter and the second name
        fLoadW          -9                      ;Set the 2nd value as a float
        iifPrintWVW     abTxData,10,3           ;Write the 2nd value, max 10 characters, max 3 decimal places

The instructions that start with ii all use the index register I as an offset from a base address, then increment I as they process several contiguous bytes of data. When the instruction has completed, I is left pointing one byte beyond the last byte processed. Hence, in the above code, the 4 separate elements of the total query string will be written to separate, contiguous area of the Xwire transmit data block that starts at address abTxData.

Write to the SX10509

Next we have to Write the query string to the SX10509 network Tx buffer. After the iiPrints (etc) above, I has been incremented by the total number of bytes written to the Tx data block. Because it started out containing 0, it now has the byte count.

        ItoX                                    ;Get the number of bytes written
        Store           bTxLength               ;Data length byte in the Xwire N/W Tx data block
        GoSub           SCH_Write               ;Set the command byte to Write
        WaitForST       sSCH_GotEcho            ;Wait for the command to be echoed 
Send a GET

Now the query string has been written to the SX10509 network Tx buffer, we can generate a GET request. This will be sent to the server with the just-written query string appended:

        GoSub           SCH_Get                 ;Send a GET request to the server
        WaitForST       sSCH_GotEcho            ;Wait for the command to be echoed
Check the status code from the server

Just like in Test 1, the status code should be 200. This time I have chosen not to display it if it’s bad – I simply abandon the operation.

        fRecallW        abRxData                ;Get the result code -> W
        fLoadQ          200                     ;OK code
        fTestWeqQ                               ;OK?
        GoIfZ           WAFbutton               ;g/ bad response - give up
Read and display the data returned by the server

This is just the same as before.

        LoadI           0                       ;Offset into SCH network Rx buffer
        GoSub           SCH_Read
        WaitForST       sSCH_GotEcho            ;Wait for the command to be echoed
        GoSub           RxDisplay               ;Display the result

        GoTo            WAFbutton