Interfacing Digital and Analog Devices
가장 많이 본 글
-
PIC microprocessor에 대한 경험을 알려 드리고 선택에 도움이 되었으면 합니다. 저는 PIC을 접한지 꽤 되었는데 아직도 완벽 하다고는 생각지 않습니다. 너무나 종류도 많고 memory크기, interface도 다양해서 어떤...
-
PIC Peripheral Pin Select(PPS: 주변장치 Pin선택기능) PIC24 또는 32 Series 중에는 주변장치 pin을 program에서 select 할 수 있는 기능이 있는 chip들이 있습니다. HW design시 rou...
-
Development environment is more important than others. This is the stepping stone for success. 개발환경 구축이 무엇보다 중요 합니다. 첫단추를 잘 끼워야 하니까요. 개발...
이 블로그 검색
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;
}
피드 구독하기:
덧글 (Atom)