View previous topic :: View next topic |
Author |
Message |
bauche
Joined: 06 Nov 2010 Posts: 22 Location: montreal qc
|
memcpy want to copy some value |
Posted: Tue Nov 05, 2013 5:22 am |
|
|
Hi i try to figure how to send only some data from reveived string...not succesfull...want to extract like: D8 FE 52 F7 in the string...
Code: |
(data in received buffer :FF FF FF FF FF 06 80 00 0E 00 D8 FE 52 F7 05 05 03 1D 18 00 00 06 28 23 00)
char* Received;
char new[26];
Received=fgetc(COM1);
fprintf(COM2,"%c",Received); // working see all value
memcpy(new,&Received[10],1);
new[10] = 0;
fprintf(COM2,"%c",new); // not working
|
can somebody show me how to do it???
Thanks... |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Tue Nov 05, 2013 5:38 am |
|
|
For the example data, show us what you expect to see as the output. |
|
|
bauche
Joined: 06 Nov 2010 Posts: 22 Location: montreal qc
|
|
Posted: Tue Nov 05, 2013 5:41 am |
|
|
Hi ....send the result value in hex by com2
Regards |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Nov 05, 2013 5:57 am |
|
|
total confusion!
- fgetc() result is char not pointer type
- Received[10] is neither in declared variable space nor loaded with a value
- fprintf(COM2,"%c",new); is not printing new[10] or new[0]
Suggestion: Learn quite a bit about C data types, characters, strings, pointers and string functions |
|
|
bauche
Joined: 06 Nov 2010 Posts: 22 Location: montreal qc
|
|
Posted: Tue Nov 05, 2013 6:02 am |
|
|
i know it's no good ...it's not working
and how i can extract some value for my received buffer and send it on com2???
use substring?? memcpy??
regards... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Tue Nov 05, 2013 8:41 am |
|
|
Fvm has pointed out some of the problems. It is just not correct C. Comments inline:
Code: |
char* Received;
//received is now a _pointer_ to a char, but as yet points nowhere.
//No memory saved for it to point to, and the initial value is
//indeterminate...
char new[26];
//new is now a pointer to a 26 character memory area.
Received=fgetc(COM1);
//You now write a single received byte into the _pointer_. Garbage.
//_Single byte_.
fprintf(COM2,"%c",Received); // working see all value
//Here the incorrectly stored pointer, will be automatically cast 'back'
//to a character. It'll sort of work, but it is wrong in multiple ways.
memcpy(new,&Received[10],1);
//'Received[10], will be 10 greater than the value received. Could be
//pointing anywhere in memory. You could retrieve anything.
new[10] = 0;
fprintf(COM2,"%c",new); // not working
//fprintf, expects to receive a character. You are giving it the address
//of the first byte of 'new'. Of course it won't work.
|
You need to get a C primer, and read the section on pointers and arrays, then try some simple exercises on a PC C. Because of the error trapping on this, you will get errors when you try to talk to memory areas you shouldn't which may help you to understand just how wrong everything is.
There is only about one line of the code that is not a glaring error... |
|
|
|