가장 많이 본 글

이 블로그 검색

2018년 6월 8일 금요일

Processing(Interpreting) commands


Processing(Interpreting) commands

After getting hashcode, then processing commands is very simple.
Firstly, check the rx buffer integrity if some errors exist. Then use switch statement to identify command and then clear buffer for the next. Below are the example. Call CommandProcess() from main().

Prepare flags and enumerations for the Serial port.

/* Defines status struct */
struct rsMap{
    // RS232 flags
    unsigned tx_in_progress : 1; // holding high until tx ends
    unsigned rx_in_progress : 1; // holding high until rx terminates by timeout
    unsigned command_received : 1; // set if rx buf has a command
    unsigned command_reserved : 1; // set if a reporting process is progressing
    unsigned comm_error : 1; // set if there's an error in command
    unsigned emergency : 1; // set if emergency rxed
    unsigned remote_on : 1; // set if remote command rxed
    unsigned comm_test : 1; // set if test needed

    unsigned no_response_req : 1; // set if either unknown command or other's data
    unsigned tx_disable : 1; // set if the last data has been written to tx buf
    // in uart, then tx_enable will be disabled by the
    // timer after about 3 mS
unsigned rxResponse : 3; /* expansion of no_response_req */
};

struct rsMap rs;
enum{
    comm1, comm2, comm3, comm4
} cmdSource;

enum{
    noResp, sendACK, sendNAK, sendStatus

} rxResponse;

/* Check received command format */

uint8 checkCmdFormat(void) {

    uint16 i, argCnt;

    if(!rs.comm_error) // Check Communication format error
    {
        /* check command error. identify no response. command error included. */

        rs.no_response_req = 0;

        /* when exceed max_cmd_len. */
        /* rx_count has char count received. */
        if(rx_count > max_cmd_len) rs.no_response_req = 1; // no response for length mismatch.

        // comment the following 2 lines for none cr/lf termination command
        if((rx_buf[rx_count - 1] != cr) && (rx_buf[rx_count - 1] != lf)) rs.no_response_req = 1;
        if((rx_buf[rx_count - 2] != cr) && (rx_buf[rx_count - 2] != lf)) rs.no_response_req = 1;

        // check if there's a non ASCII code
        for(i = 0; i <= rx_count - 3; i++) {
            if(rx_buf[i] < 0x20 || rx_buf[i] > 0x7f) {
                rs.no_response_req = 1;
                break;
            }
        }
        // command code mismatch causes no response.

        /* Extract each command string */
        if(!rs.comm_error && !rs.no_response_req) argCnt = cmdExtractor();

    }
    return argCnt;
}

void convToUpperCaseString(char *sPtr) {
    while(*sPtr != '\0') {
        if(islower(*sPtr)) *sPtr = toupper(*sPtr);
        sPtr++;
    }
}

void CommandProcess(void) {

    uint16 i, cmdOffset, argCnt;
    uint16 l;
    int iTemp;
    float f1, f2;

    /* Check command format and argument list before processing */
    argCnt = checkCmdFormat(); 

    if(!rs.comm_error && !rs.no_response_req) // do normal process
    {

        cmdOffset = cmdOffset; // set command string offset
        rs.rxResponse = noResp; /* Disable response */

        uint32 cmdCode[15];

            
        /* Command processing  ----------------------------------*/

        /* Get all hashcode from the received command -------*/
        if(argCnt > 10) argCnt = 10;
        /* Ptn download has big argument count */
        for(i = 0; i <= argCnt - 1; i++) {
            /* Convert all strings to Uppercase */
            convToUpperCaseString(cmdString[cmdOffset + i]);
            /* Get hashCode */
            cmdCode[i] = getHash(cmdString[cmdOffset + i]);
        }

        /* Jump to appropreate routine -----------------------*/
        switch(cmdCode[0]) {
            case hsOPR: /* Operation commands ----------------*/
            {
                /* Do appropriate works */
                break;
            }
            case hsAUTO: /* Auto commands --------------------*/
            {
                /* Do appropriate works */
                break;
            }
            case hsMAN: /* Manual commands -------------------*/
            {
                /* Do appropriate works */
                break;
            }
            case hsCCL: /* Cycle commmands -------------------*/
            {
                /* Do appropriate works */
                break;
            }
            case hsSD: /* SD memory commands ----------------*/
            {
                /* Do appropriate works */
                break;
            }
            

            default:
            {
                /* Unknown commmand error */

                           /* Do appropriate works */
                break;
            }
        }
    }

    /* Send Response  -------------------------------------------*/
    if(rs3.rxResponse) {
        switch(rs3.rxResponse) {
            case noResp:
            {
                /* Do nothing */
                break;
            }
            case sendACK:
            {
                /* Do appropriate works */

                break;
            }
            case sendNAK:
            {
                
                /* Do appropriate works */

                break;
            }
            case sendStatus:
            {
                /* Do appropriate works */
                break;
            }

            /* Insert more cases here */

            default: break;
        }
    }

    /* reset buffer consts after use */

    rx_count = 0;
    rs.no_response_req = 0;
    rs.command_received = 0;
    rs.comm_error = 0;
}



댓글 없음:

댓글 쓰기