3

エンコーダーの値であるRS485を介して送信された値を読み取っています。最初にE文字が返されたかどうかを確認し(エンコーダーがエラーを報告している)、そうでない場合は次の手順を実行します。

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

バッファの値は4033536で、位置は33536に設定されます。これは、この関数では毎回発生するわけではありません。おそらく、カウントしていませんが、1000回に1回です。プログラムカウンターを元に戻し、失敗した場合に行を再実行すると同じ結果が返されますが、デバッガーを再度起動すると、値が正しく変換されます。

私はkeiluvision4を使用しています。これは、stm32f103vet6とstm32f10ライブラリV2.0.1を使用するカスタムボードです。

ありがとう

4

1 に答える 1

1

誰も知らないので、私がやったことを投稿するだけです。それは、変換のための独自の関数を書くことでしたが、理想的ではありませんでしたが、うまくいきました。

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;

}

于 2013-08-06T14:34:44.643 に答える