> From: Phil Whitmarsh > To: PICLIST@MITVMA.MIT.EDU > Subject: CCS 'C' Code Help Req' > Date: Tuesday, 27 May 1997 01:38 > > Hello All, > > I have the PCM version of the CCS compiler and i am having problems reading a > string through > the serial port and then converting this to a value eg. send "255" and convert > to 255 You want something like a sscanf() function (shudder) - which doesn't come in the library. I wrote a couple of routines called parsexxx, where xxx is int or byte. This extracts the nth number from a line. It's not terribly bulletproof, nor efficent, but it works... // parse.c - contains routines for extracting numbers from command lines // 5/11/96 Mike Smith #define SHORTINT long #define BYTE unsigned char #define BOOL short int #define FALSE 0 #define TRUE 1 // system includes #include <16c74.h> #include // pCH points to a null-terminated string // parm indicates which white-space separated param you want. (starts with 0) // returns a 2 byte int SHORTINT ParseInt(char* pCH, BYTE parm) { SHORTINT val; char ch; do { val = 0; while(*pCH != '\0' && !isdigit(*pCH)) // skip white space pCH++; ch = *pCH; while(isdigit(ch)) { val *= 10; val += (ch - '0'); pCH++; ch = *pCH; } } while(parm--); return val; } // same as above, but returns a one byte int BYTE ParseBYTE(char* array, BYTE parm) { SHORTINT val; val = ParseInt(array, parm); return (BYTE)val; } // returns a bit value BOOL ParseBOOL(char* array, BYTE parm) { SHORTINT val; val = ParseInt(array, parm); return (BOOL)val; } // doesn't expect hex numbers to be prefixed with 0x SHORTINT ParseHexInt(char* pCH, BYTE parm) { SHORTINT val; char ch; do { val = 0; while(*pCH != '\0' && !isxdigit(*pCH)) // skip white space pCH++; ch = *pCH; while(isxdigit(ch)) { val *= 16; if(isdigit(ch)) val += (ch - '0'); else val += (toupper(ch) - 'A' + 10); pCH++; ch = *pCH; } } while(parm--); return val; } BYTE ParseHexBYTE(char* array, BYTE parm) { SHORTINT val; val = ParseHexInt(array, parm); return (BYTE)val; } > anybody able to give an example, also anybody got sample programs for the CCS > compiler > apart from the ones it came with ! > Like for what? I think we'd try the patience of PICLIST members by posting unspecific code... MikeS BTW, do ppl here have a preference for how stuff like the above is posted? As an attachment? A zipped attachment? Inlined with mail?(as above)