View previous topic :: View next topic |
Author |
Message |
Scottl
Joined: 21 Feb 2005 Posts: 25
|
fgets() with hex problem |
Posted: Sun Nov 04, 2007 7:47 pm |
|
|
I am trying to receive values from a serial port of a pc and the format is as follows:
0x450x110x0D
The decimal format is 691713
I am using fgets(data,COM_A)! Should I use fgetc instead?
How can I compare this in an if strcmp(data, "691713")?
Do I need to convert this before the compare?
This serial stuff is killing me!
Thanks in advance,
Scott |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Nov 05, 2007 3:01 am |
|
|
Yes you need to convert the values to a 24 bit number before doing the compare OR you can do
strcmp(data, "0x450x110x0d")
to convert would have been simple IF the compiler included the sscanf function. Not sure what compiler you are using, you can check if you want.
Otherwise you will have to do it manually.
Repeat 3 times the following
Read 4 chars, skip the first 2 and convert the hex value to decimal
If you need more help in doing that then post! |
|
![](templates/subSilver/images/spacer.gif) |
Scottl
Joined: 21 Feb 2005 Posts: 25
|
|
Posted: Thu Nov 08, 2007 9:12 pm |
|
|
Hi Wayne,
Been out traveling this week! The customer has change the message to ASCII "EDC1" which in hex is 0x450x11. When I get the string can I compare as is or still need converting to DEC?
If I still need conversion can you give me any pointers?
Thanks for your help,
Scott |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Nov 12, 2007 3:39 am |
|
|
Is the message "EDC1" or "0x450x11"
The characters coming in are what you have got to compare NOT what they represent unless you convert them.
so either strcmp(data, "EDC1") or strcmp(data, "0x450x11") would do depending on the actual data.
strcmp returns 0 if they exactly match! you would need to ensure your data string has a NULL terminating character or you could use
strncmp(data, "EDC1", 4) this will only check the first 4 characters.
So if you know what the characters are that you are recieving then you can check those. If the data will change then you have 2 options. Convert the data so you can do range checking e.g.
if (inVal > 0 && inVal < 100)
of have multiple string comparisons.
hope this helps,
Wayne |
|
![](templates/subSilver/images/spacer.gif) |
|