在GCC C(以及字符和指针)中尝试字符串处理新手问题 [英] Attempting string handling in GCC C (and chars and pointers) Newbie question

查看:114
本文介绍了在GCC C(以及字符和指针)中尝试字符串处理新手问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们我正在使用GCC C编译器在Atmel Studio 6.0中编写C代码。基本上我在微控制器上进行USART通信,我找到了一个示例代码(一个头文件),它使用来自Atmel员工博客的循环接收缓冲区处理发送/接收(Tx / Rx)

http://www.fourwalledcubicle.com/files/LightweightRingBuff.h [ ^ ]



有效的代码摘录:



除了初始化Buffer和其他函数外,它还插入了char类型数据它接收到Buffer中:

Hey guys I''m writing a C code in Atmel Studio 6.0 with GCC C compiler. Essentially I''m doing USART communication on a microcontroller, and I found a sample code(a header file) that handles sending/receiving (Tx/Rx) using a circular receiving buffer from an Atmel employee''s blog
http://www.fourwalledcubicle.com/files/LightweightRingBuff.h[^]

Code excerpt that works:

Besides initializing the Buffer and other functions, it insert the char type data it receives into the Buffer as such:

static inline void RingBuffer_Insert(RingBuff_t* const Buffer,
                                         const RingBuff_Data_t Data)  // type of Data: uint8_t
    {
        *Buffer->In = Data;//store the char data in the Buffer.

        if (++Buffer->In == &Buffer->Buffer[BUFFER_SIZE])
          Buffer->In = Buffer->Buffer;

        ATOMIC_BLOCK(ATOMIC_RESTORESTATE)  // ignore this detail, not relevant to topic
        {
            Buffer->Count++;
        }
    }



和这里'头文件中的函数可以让我们出来


and here''s the function in the header file to get''em out

static inline RingBuff_Data_t RingBuffer_Remove(RingBuff_t* const Buffer)
        {
            RingBuff_Data_t Data = *Buffer->Out;

            if (++Buffer->Out == &Buffer->Buffer[BUFFER_SIZE])
              Buffer->Out = Buffer->Buffer;

            ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
            {
                Buffer->Count--;
            }

            return Data;
        }





然后在main.c中我们可以用它来读出缓冲区中的字符



And then in main.c we can use this to read out chars in the Buffer

// Print contents of the buffer one character at a time
     while (BufferCount--)
       putc(RingBuffer_Remove(&Buffer));





我的目标:我要等到整个字符串存储在缓冲区(以''\ 0''或''\ n''终止?),读出字符串,然后解析它。





代码摘录不起作用:



现在作为一个绝对的C新手,这里是我试图做的事情(带有明显错误指针使用的错误代码)只是为了说明我希望实现的目标:



My goal: I''m trying to wait until an entire string of chars is stored in the Buffer (terminated with a ''\0'' or ''\n''? ), read out the string, and maybe parse it.


Code excerpt that doesn''t work:

Now as an absolute C newbie, here''s what I have attempted to do (buggy code with blatantly wrong pointer usage coming thru) Just to illustrate what I hope to achieve:

int main( void ) {
 //... some initialization
RingBuffer_InitBuffer(&Buffer);
unsigned char holder;

while(true) {
if(usart_kbhit()) // this detects the receiving flag in USART register
  {	holder = usart_getchar();  //holder is char type. Stores the char received 
       //in the Rx register
	RingBuffer_Insert(&Buffer, holder); // function above
        
        if (holder == '\n')   { // I suppose I can send  '\n'  as the last char to 
          //terminate my string?
	 //now I'm trying to read the chars in Buffer and store'em in 
         // 'Command_String' which I suppose should be a char [] ??
         // Not so sure about how I should define 'Command_String'

        uint16_t BufferCount = RingBuffer_GetCount(&Buffer); // get the char count
       
        char Command_String[BufferCount];  //  I know this is seriously wrong in 
         //terms of how the pointer should be used.
					
        while (BufferCount--)
	  strcat(Command_String,RingBuffer_Remove(&Buffer)); // trying to append 
          // the char that I read off the Buffer to Command_String
          // but this doesn't work.
			
	  // Not working .  RingBuffer_Remove() gives a string, strcat requires  
          //pointer??  
			
						
	if strncmp(Command_String,"ABC\n",4) // my attempt to compare the string I 
         //get in Command_String with a hard-coded list of commands 
        tell_the_microcontroller_to_do_stuff (); // if received "ABC" then do stuff
                                }
   }
             }
                  }





对于长篇帖子感到抱歉,但我需要的是对我的方式的一些评论从Buffer中读出字符串,将其存储在Command_String中,并将Command_String与ABC进行比较





这里''我的代码没有罗嗦的评论





Sorry about the long post but all I need are some comments about the way I read out the string from Buffer, store it in Command_String, and compare Command_String with "ABC"


Here''s my code without the wordy comments

while(true) {
      // if a key has been pressed, then process it
if(usart_kbhit())   {
   holder = usart_getchar();

   RingBuffer_Insert(&Buffer, holder);

   if (holder == '\n')   {
uint16_t BufferCount = RingBuffer_GetCount(&Buffer);
 char Command_String[BufferCount] ;

   while (BufferCount--)
               strcat(Command_String,RingBuffer_Remove(&Buffer));
   // Not working .


   if strncmp(Command_String,"ABC\n",4)
    tell_the_microcontroller_to_do_stuff ();

                                }
   }
             }
                  }







谢谢




Thank you

推荐答案

char Command_String[BufferCount] ;

  while (BufferCount--)
              strcat(Command_String,RingBuffer_Remove(&Buffer));





不能在C中工​​作。你不能在堆栈上分配一个char数组(作为自动变量) )具有可变长度。必须在编译时知道长度。所以要么分配一个固定大小的数组,比如说你想要连续的最大字符块,要么你必须从堆中分配空间,然后释放它,例如



That won''t work in C. You cannot allocate an array of char on the stack (as auto variable) with a variable length. The length must be know at compile time. So either allocate an array of fixed size, say the maximim chunk of chars you expect in a row, or you must allocate the space from the heap, and later free it, e.g.

char* pBuffer = malloc (bufferCount);
...
free (pBuffer);



至于你关于''\ n''或''\ 0''作为终结者的问题,这完全取决于你的情况。通常,你想在USART上等待''\0''。但这取决于你的缓冲机制,无论你想要完成什么。


As for your question about ''\n'' or ''\0'' as terminator, that depends entirely on your situation. Ususally, you want to wait for ''\0'' on a USART. But that depends on your buffering mechanism any what your trying to accomplish.


这篇关于在GCC C(以及字符和指针)中尝试字符串处理新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆