This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] MSP430F2274:STRCAT 垃圾

Guru**** 2525540 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/777114/msp430f2274-strcat-garbage

器件型号:MSP430F2274

你好!

我已经读过一些关于 strcat 和 strcpy 的其他文章、但我不明白为什么我的字符串会这样发送。 输出应为:

"rpm:1234 mA:0213"、但我得到的是"rpm:1234{4字节 LF 或垃圾}mA:0213"

以下是代码片段:

 

void SendData()
{
volatile unsigned int TempDataInt = 0;



strcpy (&StrBuf[0]," ");
strcpy (&StrBuf[0]、"rpm:");

TempDataInt = binToBCD ((long unsigned int) rpm);

DataBuf[3]=(TempDataInt & 0x000F)| 0x30;// u
TempDataInt >=4;
DataBuf[2]=(TempDataInt & 0x000F)| 0x30;// d
TempDataInt >=4;
DataBuf[1]=(TempDataInt & 0x000F)| 0x30;// c
TempDataInt >=4;
DataBuf[0]=(TempDataInt & 0x000F)| 0x30;// m
TempDataInt >=4;

strcat (&StrBuf[0]、&DataBuf[0]);
strcat (&StrBuf[0]、"mA:");

TempDataInt = binToBCD ((long unsigned int)(current*1000));

DataBuf[3]=(TempDataInt & 0x000F)| 0x30;
TempDataInt >=4;
DataBuf[2]=(TempDataInt & 0x000F)| 0x30;
TempDataInt >=4;
DataBuf[1]=(TempDataInt & 0x000F)| 0x30;
TempDataInt >=4;
DataBuf[0]=(TempDataInt & 0x000F)| 0x30;
TempDataInt >=4;


strcat (&StrBuf[0]、&DataBuf[0]);


__no_operation();

for (r=0;<strlen(&StrBuf[0]);r++)      r
{
UCA0TXBUF = StrBuf[r];
while (UCA0STAT 和 UCBUSY);
}
} 

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    您好、Mirco、
    我认为<strlen(&StrBuf[0])' are causing the problem. r 您是否可以尝试使用常数代替它?


    此致、
    现金 Hao
  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    我使用了固定长度(15个字符)、四个"LF"仍然出现、移走了我差安培的数字!
  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    如何(以及在何处)声明 StrBuf 和 DataBuf? 这看起来像是一个字符串溢出/初始化。
  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    我怀疑 DataBuf[]中的第五个字符不是字符串结尾标记。
  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    事实上,在本节之后:
    DataBuf[3]=(TempDataInt & 0x000F)| 0x30;// u
    TempDataInt >=4;
    DataBuf[2]=(TempDataInt & 0x000F)| 0x30;// d
    TempDataInt >=4;
    DataBuf[1]=(TempDataInt & 0x000F)| 0x30;// c
    TempDataInt >=4;
    DataBuf[0]=(TempDataInt & 0x000F)| 0x30;// m
    TempDataInt >=4;

    您必须在 DataBuf 中输入'\0'字符,以便以下 strcat()知道停止连接的位置。

    规则是:如果您在不使用 strxxx()函数的情况下修改 C 字符串,*您*必须确保字符串末尾后有一个'\0'。
  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    谢谢你们、我真的没有想到那个通道中的 EOS 字符。