質問

I am reading a value sent over RS485 which is the value of an encoder I first check if it has returned an E character (the encoder is reporting an error) and if not then do the following

    *position = atoi( buffer ); 
    // Also tried *position = (s32) strtol(buffer,NULL,10);

The value in the buffer is 4033536 and position gets set to 33536 this does not happen every time in this function probably 1 in 1000 times maybe although I am not counting. Setting the program counter back and doing the line again if has failed returns the same result but starting the debugger again causes the value to convert correctly.

I am using keil uvision 4, its a custom board using an stm32f103vet6 and the stm32f10 library V2.0.1 This one has really got me stumped never come across something like this before any help would be much appreciated.

Thanks

役に立ちましたか?

解決

As no one knows I will just post what I ended up doing which was to write my own function for convertion not ideal but it worked.

bool cdec2s32(char* text, s32 *destination)
{
    s32 tempResult = 0;
    char currentChar;
    u8 numDigits = 0;
    bool negative = FALSE;
    bool warning = FALSE;

    if(*text == '-')
    {
      negative = TRUE;
      text++;
    }

while(*text != 0x00 && *text != '\r') //while current character not null or carridge return
{
    numDigits++;
    if(*text >= '0' && *text <= '9')
    {
        currentChar = *text;
        currentChar -= '0';

        if((warning && ((currentChar > 7 && !negative) || currentChar > 8 && negative )) || numDigits > 10) // Check number not too large
        {
            tempResult = 2147483647;
            if(negative)
                tempResult *= -1;

            *destination = tempResult;
            return FALSE;
        }

        tempResult *= 10;
        tempResult += currentChar;
        text++;
        if(numDigits >= 9)
        {
            if(tempResult >= 214748364)
            {
                warning = TRUE; //Need to check next digit as close to limit
            }
        }
    }
    else if(*text == '.' || *text == ',')
    {
        break;
    }
    else
        return FALSE;
}
if(negative)
    tempResult *= -1;

*destination = tempResult;
return TRUE;

}

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top