Getting string from RAM

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I spent all day today trying to get some strings from RAM without success. I can do it one by one, but I was unable to join all those in one string. I came from assembly language and learn C is bit of challenge. I read the string tutorial but looks like I need more knowledge than this to perform this task.

 

The problem: I read some characters from serial port and saved in RAM by the code below. Let say I have a string like this: "The quick brown fox jumps over the lazy dog". I need a part of it copied to another string, like only the word "quick". I tried with strcat, stcpy and several other ways but always getting some kind of error. So I would like to ask for help on this: How to get the part "quick" of the long string and save it to a different string named part

 

uint8_t flag_usart_rx;
uint8_t flag;
char rx_serial_string [70];
int prt_serial;    
char uart_getchar;
char part[20]


void RX_SERIAL (void)
{
	if (flag_usart_rx==1)
	{
		flag_usart_rx=0;
		if (uart_getchar=='#' || flag==1)
		{
			rx_serial_string[prt_serial] = uart_getchar;	
			prt_serial++;
			flag=1;	
		}
		if (prt_serial==69)
		{
			prt_serial=0;
			flag=0;
		}
	}
}

Any suggestion will be welcomed.

 

This topic has a solution.

Good Soldering JRGandara

Last Edited: Sat. May 23, 2015 - 02:18 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What you have posted only shows loading SRAM with the string.  Can you post what you have tried that is not working for retrieval, or possible the whole program?

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim, thank you for the reply. 

 

There not much more, only the includes in the begging and the interrupt at the end. I just post the code for clarity. This code is almost empty because is only test. I tried several forms, this one was the last tried waiting I could get each letter added to part

char temp;
				temp = rx_serial_string [15];
				strcat(part,temp);
				temp = rx_serial_string [16];
				strcat(part,temp);
				temp = rx_serial_string [17];
				strcat(part,temp);
				temp = rx_serial_string [18];
				strcat(part,temp);
				temp = rx_serial_string [19];
				strcat(part,temp);

 

Good Soldering JRGandara

Last Edited: Sat. May 23, 2015 - 03:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

'Some kind of error' that really doesn't help the situation does it?

First up, C strings are terminated by a 0. If you use any of the string lib functions, they expect this. This is how they determine the end of the string. If you want zero as a valid value, then don't use C string functions.
How would you perform your task in assembler? We need to know where to start copying, the number of chars and where to copy them to.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I said some kind of error because I tried in so many ways that I can´t recall every one anymore. I´m asking for suggestions because I already tried a lot today with no luck. This is not my area of expertise, but I will get there eventually. Every now and then I try to learn and experiment something with C and AVR which I like. Never had time enough in the past, but now I get the time to invest on this. I know the basic idea of what I have to do. 
From your post I could see I should be using int instead string. I did not know that. This kind of information we just learn from experience. A newbie will not get this idea just on the first read. I changed to int and coded the following just to test and learn the function:

 

char part[20];

strcpy (part,rx_serial_string [19]);

 

I expected to have in part 52 which is what I have in RAM, but instead I got 13 and the following alert:

"passing argument 2 of 'strcpy' makes pointer from integer without a cast [enabled by default]"

I tried this strcpy just to see if I get at least one byte. If it works, I would try to use strcat to add more bytes to a char. I understand the variables are not similar and should be the reason of the alert, but before I dig in to this, someone suggest a more appropriate function for this situation? 

 

Good Soldering JRGandara

Last Edited: Sat. May 23, 2015 - 05:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
uint8_t fred(const char input[], char output[], uint8_t output_size)
{
//  on entry, input ...
//  on entry, output points to the start of at least output_size chars
//  on exit, output ....
//  on exit, fred returns 0 unless .....
......
}

Still not clear on what you want.

If you can fill in ... , .... and ..... , it might help us help you fill in ...... .

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you mean something like this?

char string_src[] = "The quick brown fox jumps over";
char string_dest[20]; 

int main(void)
{
      // copy 5 chars from string_src to string_dest, start on string_src[4] 
      strncpy(string_dest, string_src+4, 5);
}

 

Last Edited: Sat. May 23, 2015 - 06:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As such, C doesn't know about strings, it knows about arrays. A string is an array of chars. The standard libraries implement the concept of a string. This is an array of chars terminated by a value of 0. Not an int, a char with a value of 0 or '\0'. The function strlen() returns the length of a string by stepping through the array until it finds a value of 0. At each step it increments a value which is the length. Strcpy() copies each char from the source to the destination until it hits a value of 0.

Then we get into 'pointers'. rx_serial_string is an array. It actually holds the ram address of that array. It 'points' to the array. When you write rx_serial_string[19] you are referring to the 20th character in that array. Strcpy wants the address. Therefore you want the address of. &rx_serial_string[19] gives you the address of the array with an offset of 19. You use pointers all the time in assembler - the address or what's in that address.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Visovian,

That was exactly what I needed. I just was not dealing well with the syntax, and being newbie to C, did not understand well the error messages. Searching for causes of these errors messages not always took me to right solution. In addition, add to this not having a well-established concept of C, is a recipe for frustration. Thank you for this one, it help me a lot.

 

Kartman,

These paragraphs enlightened me more on these concepts which many full texts I already read. I did a lot of assembly in the past, but I do not do that anymore for good 10 years. I still have many concepts and ideas of how to write software, but not all that is still vivid and C is different beast for me. In assembly, I knew I have to think low level, in C I thought would be more like Visual Basic, and obviously is not. However, I´m sure in no time I will get along with C and who knows soon I will ask better questions.

 

Last night I read all the “Newbie? Start here!” and wow! Resuming in a sarcastic way, any newbie should read at least 10 books about the subject and have at least 5 years of experience in the matter before dare to disturb the delicate geniuses (paraphrasing Seinfeld). Nevertheless, I learn a lot of how do not ask stupid questions and I should improve for the next ones.

 

Being serious now, this AVR Freaks is a fantastic source for information and help.  Thank you all!

Ps: Of course I bumped in a new obstacle, but I will search more before dare to ask again. :-)

 

Good Soldering JRGandara

Last Edited: Sun. May 24, 2015 - 06:47 PM