工具/软件:TI-RTOS
您好、
我真的不知道这是 C 问题还是 TI RTOS 问题。 (我不是 C 语言家庭)
我正在尝试从用户读取 UART 并放入一个数组。
当消息超过一定数量的字节时、我会分配更多字节(当然是动态的)
以下是我的代码:
System_printf ("任务1开始");\
UART_Handle 句柄;
UART_Params 参数;
//初始化 UART 并指定非默认参数
UART_PARAMS_INIT (params);
params.baudrate = 115200;
params.writeDataMode = UART_DATA_BINARY;
params.readDataMode = UART_DATA_BINARY;
//params.readCallback = readCallback;
//打开 UART 并启动第一次读取
句柄= UART_OPEN (Board_UART、params);
int bytesOfMessage = 10;
UChar*消息;
UChar* tempMessage;
Message = malloc (bytesOfMessage * sizeof (UChar));
int i = 0;
while (true)
{
int rxBytes = UART_Read (handle、rxBuf、1);
//System_printf ("%d \n"、rxBuf[0]);
if (rxBuf[0]!= 13)
{
message[i]= rxBuf[0];
i++;
int l = 1;
if (i ==字节 OfMessage)
{
System_printf ("第一条消息:\n");
for (l=0;<bytesOfMessage;l++)
{
//System_printf ("%c"、message[l]);
}
System_printf ("第一条消息的末尾"\n);
bytesOfMessage += 10;
//message = realloc (message、bytesOfMessage * sizeof (UChar));
tempMessage =(UChar*) realloc (message、bytesOfMessage * sizeof (UChar));
Message = tempMessage;
tempMessage =空;
System_printf ("Message2 %d:\n"、bytesOfMessage);
for (l=0;<bytesOfMessage;l++)
{
//System_printf ("%c"、message[l]);
}
System_printf ("第二条消息的末尾"\n);
}
UART_WRITE (handle、rxBuf、rxBytes);
}
其他
{
rxBuf[0]='\r\n';
rxBuf[1]='\n';
rxBuf[2]='n';
rxBuf[3]='e';
rxBuf[4]='w';
UART_WRITE (handle、rxBuf、5);
}
}
第一个 realloc 函数工作正常。
第二个是卡滞。 我认为有足够的内存。
也许有人知道我为什么会发生这种情况?
体层器。