trim strings

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

Hey guys, I need help manipulating strings in GNU GCC, its for USART... this is the prob:

I set up a simple at24c02 eeprom to see all the text sent from PC (br@ys terminal) to AVR when using scanf... so lets say I send my command:

>ewrite
>enter string:
>brent

the output on my eeprom is:
62 72 65 6e 74 0 0 0 0 0 0 e1 4 30 73 7f 2a 31 5c
b r e n t . . . . . . . . . . . . . .

no matter the length of the string I enter, it tags on those 14 extra chars, im sure they are used in GCC to tell it when scanf should stop recording or what have you... but I need a way to cut those last 14 chars off...

I have done some searching but I cannot find what I am looking for, I know commands like strncmp but that doesent help...

I need to somehow only read from start to - the last 14

anyone have any ideas?

BTW: im using the latest winAVR, and I have stdio(fdevopen) set up for USART like this (if it helps)

FILE * stdio_dev;
stdio_dev = fdevopen(uart_tx, uart_rx);

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

ph0rkeh wrote:
anyone have any ideas?

Idea #1: post your code

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

ph0rkeh wrote:
Hey guys, I need help manipulating strings in GNU GCC, its for USART... this is the prob:

I set up a simple at24c02 eeprom to see all the text sent from PC (br@ys terminal) to AVR when using scanf... so lets say I send my command:

>ewrite
>enter string:
>brent

the output on my eeprom is:
62 72 65 6e 74 0 0 0 0 0 0 e1 4 30 73 7f 2a 31 5c
b r e n t . . . . . . . . . . . . . .

no matter the length of the string I enter, it tags on those 14 extra chars, im sure they are used in GCC to tell it when scanf should stop recording or what have you... but I need a way to cut those last 14 chars off...

I have done some searching but I cannot find what I am looking for, I know commands like strncmp but that doesent help...

I need to somehow only read from start to - the last 14

anyone have any ideas?

BTW: im using the latest winAVR, and I have stdio(fdevopen) set up for USART like this (if it helps)

FILE * stdio_dev;
stdio_dev = fdevopen(uart_tx, uart_rx);

The strring stops at the first 0. All string functions will stop processing there. Your receive routine would have filled to theat first 0, those additional characters are random garbage. If your eeprom write routine would have done the same, you likely would have seen (assuming the EEPROM was fully erased before writing to it)

62 72 65 6e 74 00 ff ff ff ff ff ff ff ff ff ff ff ff ff
b  r  e  n  t  .  .  .  .  .  .  .  .  .  .  .  .  .  .

How did you come up with the extra length? the strlen function would have returned '5' (as would scanf IIRC)

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

Quote:
How did you come up with the extra length? the strlen function would have returned '5' (as would scanf IIRC)

ok that reminded me of something, this code was originally from when I first tried interfacing with a at34c** eeprom... I found the prob:

I know what I did wrong here, when I was using scanf with a char that was given 20 (eg I was setting scanf into a 'char get[20]', don't ask why :P... so of course im sure the extra junk is given there... now that makes alot more sense! (I just copied this code from an earlier project)

I'm sorry guys, stupid mistake! but even though I just caught this, a new question arises! what is a better way to handle scanf, so I don't have to have the extra garbage? by that I mean, what should I be loading scanf into?

clawson: here is my code (well part of it, just the part used to take from scanf and store, you can easily see what i did wrong, but I still need your help) if you want the full source just ask.

http://www.ursrc.com/index.php?s...

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

You always need to allocate a buffer, and pass that buffer to scanf... scanf cannot allocate the buffer for you... so what you did was correct. It's just how you used that buffer... scanf may not use all of the buffer, anything unused will not be touched, thus potentially contain garbage characters. Note that your buffer needs to be larg enough to accept whatever input you are passing to it... you can make your scanf format string truncate, so that you don't overrun your buffer.

You have a few choices here, you can store the whole buffer, as you already are, and just initialize it to all 0's or all ff's before calling scanf. (this won't save characters, it'll just replace teh junk with known values) or you could use strlen to determine how many bytes the string really is, and only store that many into the EEPROM (but then to be useful, you will need to store the length as well, or otherwise terminate the string)

Going to your site, the highlighted portion is not the problem... this is the problem

#define EE_WRITE(addr, str) eeprom_write_bytes(addr, sizeof(str)-1, str)

you see sizeof(str) returns the size of the array, not the size of the string. use strlen() instead, (and don't subtract 1 from it) [in fact you should add 1 so that you include the null terminator]

sizeof() is a compiler macro, not a runtime function.
and therefor gets it's value at compile time, not at runtime.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

awesome glitch, I appreciate it!