我搞砸了哪里?在USART中使用环形/循环缓冲区 [英] Where did I screw up? using a Ring/Circular buffer in USART
问题描述
这是我上一篇文章的缩小版。我正在尝试使用一些在USART中实现环形缓冲区的函数。它们在头文件中定义。
http://www.fourwalledcubicle.com/files/ LightweightRingBuff.h [ ^ ]
我试图在main.c中使用这些函数,但我想某处我搞砸了一些指针/ char / uint8_t类型。简而言之,我已将此代码编程到我的微控制器,但在发送ABC后似乎没有收到任何回复
你能不能当关于我使用char / pointers的方式有什么可疑之处?如果需要澄清,请告诉我。
This is a narrow-down from my previous post. I''m trying to use some functions that implement a Ring buffer in USART. They are defined in a header file.
http://www.fourwalledcubicle.com/files/LightweightRingBuff.h[^]
I''ve attempted to use these functions in main.c, but I think somewhere I screw up some pointer/char/uint8_t type. In short, i''ve program this code to my microcontroller but don''t seem to receive anything back from it after sending "ABC"
Could you gurus spot anything fishy about the way I''m using the char/pointers? let me know if clarifications needed.
RingBuffer_InitBuffer(&Buffer);
char holder;
// if a key has been pressed, then process it
if(usart_kbhit()) {
holder = usart_getchar();
if (!RingBuffer_IsFull(&Buffer))
{
RingBuffer_Insert(&Buffer, holder);
}
if (holder == 'C') {
uint16_t BufferCount = RingBuffer_GetCount(&Buffer);
char* Command_String = malloc (BufferCount);
while (BufferCount--) {
if (!RingBuffer_IsEmpty(&Buffer))
{
*Command_String = RingBuffer_Remove(&Buffer); // is this a screw up?
Command_String++;
}
}
if (strncmp(Command_String,"ABC",3))//another screw up?
usart_printstring(Command_String);
free (Command_String);
}
}
P.S. .h中的一些定义
P.S. some definitions in .h
static inline void RingBuffer_Insert(RingBuff_t* const Buffer,
const uint8_t Data){...}
void usart_putchar(char data) {
// Wait for empty transmit buffer
while ( !(UCSR0A & (1<<UDRE0)) );
// Start transmission
UDR0 = data;
}
void usart_printstring(char *s) {
// loop through entire string
while (*s) {
usart_putchar(*s);
s++;
} }
############################# ###################
更新:
如果我在收到字符后执行此操作并将其存储在缓冲区,它的工作原理
################################################
UPDATE:
if I do this after receiving the chars and store them in the Buffer, it works
uint16_t BufferCount = RingBuffer_GetCount(&Buffer);
while (BufferCount--)
usart_putchar(RingBuffer_Remove(&Buffer));
但是当我尝试将缓冲区中的字符移动到此char * Command_String时,我将其定义为在Buffer中读取整个字符串一次终止字符(我可以使用''C''或\ n或\0 )收到了,我从Command_String得不到任何东西
But when I try to move the chars in Buffer to this char* Command_String, which I define to read the whole string in Buffer once a terminating char (I could use ''C'' or \n or \0) is received, I get nothing from Command_String
...
char holder ;
holder = usart_getchar();
RingBuffer_Insert(&Buffer, holder);
if (holder == 'C') {
uint16_t BufferCount = RingBuffer_GetCount(&Buffer);
char* Command_String = malloc (BufferCount);
while (BufferCount--) {
*Command_String = RingBuffer_Remove(&Buffer);
Command_String++; }
usart_pstr(Command_String);
free (Command_String); }
所以我用这个char的方法有问题* Command_String
So there''s something wrong about my approach with this char* Command_String
推荐答案
它'如果没有实际运行,很难确定是什么问题,但是......你的意思是只在不是ABC时发送数据吗?
It''s difficult to be sure quite what is wrong without actually runningthat, but... Did you mean to only send data when it wasn''t "ABC"?
if (strncmp(Command_String,"ABC",3))//another screw up?
usart_printstring(Command_String);
与您之前的评论有什么冲突:
发送ABC后似乎没有收到任何回复也许你试过:
Which conflicts with your earlier comments:
"don''t seem to receive anything back from it after sending "ABC""Perhaps if you tried:
if (strncmp(Command_String,"ABC",3) == 0)
{
usart_printstring(Command_String);
}
*Command_String = RingBuffer_Remove(&Buffer); // is this a screw up?
Command_String++;
是的,确实如此。你不想先将指针保存到缓冲区吗?否则,以下
Yes, it is. Don''t you want to save the pointer to your buffer first? Otherwise, the following
strncmp(Command_String,"ABC",3)
未看到您刚复制的字符串in,但是它背后的东西。
请尝试以下方法:
is not seeing the string that you just copied in, but the stuff that''s behind it.
So try the following:
char* pBuffer = malloc (BufferCount + 1);
for (int idx = 0; idx < BufferCount; ++idx)
pBuffer[idx] = RingBuffer_Remove(&Buffer);
pBuffer[BufferCount] = '\0';
// plausibility test
if (strncmp (pBuffer, "ABC", 3))
usart_printstring (pBuffer);
free (pBuffer);
这篇关于我搞砸了哪里?在USART中使用环形/循环缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!