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(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);
Command_String++;
}
}
if (strncmp(Command_String,"ABC",3))
usart_printstring(Command_String);
free (Command_String);
}
}
P.S. some definitions in .h
static inline void RingBuffer_Insert(RingBuff_t* const Buffer,
const uint8_t Data){...}
void usart_putchar(char data) {
while ( !(UCSR0A & (1<<UDRE0)) );
UDR0 = data;
}
void usart_printstring(char *s) {
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));
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); }
So there's something wrong about my approach with this char* Command_String