c - Converting uint8_t to int issues -



c - Converting uint8_t to int issues -

i trying communicate usb dongle via serial communication. communication works, cant device correctly parse communication. devices reads message , compares hardcoded c-string. parses , recognizes it's right string, when seek parse value after : character, returns 0x00000000 , have no thought why. i've tried using char cast , utilize atoi, tried using simple ascii translation, , doing bitwise add-on operation shown here: convert subset of vector<uint8_t> int

for example: send "heart rate:55" parses , recognizes "heart rate:" when tell go find 55 , bring stuff it, gives me 0x00000000 heres snippet:

const uint8_t hrmset[] = "heart rate:"; /** find : character in string , break apart find if matches, , determine value of value of desired heart rate. **/ int parse(uint8_t *input, uint8_t size) { (uint8_t = 0; < size; i++) { if (input[i] == ':') { if (compare_string(input, hrmset, i) == 0) { int val = 0; (int j = i+1; j < size; j++) { if (!isdigit(input[j])) { (int k = i; k < j; k++) { val <<= 8; val |= input[k]; } } } homecoming val; } homecoming -1; } } homecoming -1; }

compare string function

/** compare input const values byte byte determine if equal.**/ int compare_string(uint8_t *first, const uint8_t *second, int total) { (int = 0; < total; i++) { if (*first != *second) { break; } if (*first == '\0' || *second == '\0') { break; } first++; second++; } if (*first == ':' && *second == ':') { homecoming 0; } else { homecoming -1; } }

the problem here using nested loops perform tasks should done sequential loops.

for example, i loop searches colon, , loop in compare_string searches colon again. run i loop first, , phone call compare_string after i loop finishes. improve design have compare_string search colon while comparing, , homecoming index of character after colon (or -1 if colon not found).

the same true of j , k nested loops. j loop searching end of number. k loop runs 1 time after j loop finished, , k loop should after j loop, not nested. improve design single loop converts number while searching end of number.

the code below demonstrates 1 possible implementation using techniques i've described.

const uint8_t hrmset[] = "heart rate:"; int compare_string( uint8_t *input, const uint8_t *expected, int size ) { ( int = 0; < size; i++ ) { if ( *input != *expected || *expected == '\0' ) return( -1 ); if ( *input == ':' && *expected == ':' ) return( + 1 ); input++; expected++; } return( -1 ); } int parse( uint8_t *input, uint8_t size ) { int i, val; if ( (i = compare_string( input, hrmset, size )) < 0 ) return( -1 ); val = 0; ( ; < size && isdigit( input[i] ); i++ ) val = val * 10 + input[i] - '0'; return( val ); } int main( void ) { uint8_t input[] = "heart rate:75"; int rate = parse( input, sizeof(input) - 1 ); printf( "%d\n", rate ); }

c serial-communication rs485

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -