#include #include #include"uart.h" char hex_to_ascii(int iHex) { if (iHex<10) return(iHex+'0'); else return('A'+iHex-10); } /* ----------------------------------------------------------------------- */ /* return : CheckSumError --> chksum error NoError --> OK */ int chk_chksum(unsigned char *cBuf) { int i,len; unsigned char sum; char h,l; len=strlen(cBuf); for (i=0,sum=0; i OK 1 --> port value error 2 --> timeout */ int SEND_CMD(int iPort, char *cCmd, long int lTimeout, int iChksum) { int i,ret; unsigned char CheckSum=0; extern long ToComTimeOut; i=0; ToComTimeOut=lTimeout; while (cCmd[i]) { ret=ToCom(iPort,cCmd[i]); if(!ret) { if(iChksum) CheckSum+=cCmd[i]; i++; continue; } else { return ret; } } if (iChksum) { ret=ToCom(iPort,hex_to_ascii(CheckSum/16)); /* CHKSUM high byte */ if(!ret){ ret=ToCom(iPort,hex_to_ascii(CheckSum&0xf)); /* CHKSUM low byte */ if(ret){ /* error */ return ret; } } else return ret; } return ToCom(iPort,'\r'); /* send out 0x0D */ } /* ----------------------------------------------------------------------- */ /* return : 0 --> OK 1 --> port value error 2 --> timeout 3 --> chksum error */ int RECEIVE_CMD(int iPort, char *cCmd, long int lTimeout, int iChksum) { int i; char c; long t; i=0; t=0; for(;;) { while(!IsCom(iPort)){ t++; if (t>lTimeout) return TimeOut; /* time_out */ } c=ReadCom(iPort); if (c==0x0d) break; /* receive 0x0d --> end of command */ else cCmd[i++]=c; /* store the command */ if(i>35){ cCmd[i]=0; /* string must terminate with 0 */ printf("[%s]\7\7\7",cCmd); break; } } cCmd[i]=0; /* string must terminate with 0 */ if(iChksum) return chk_chksum(cCmd); else return NoError; }