ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi,
I have been working on a project over many revisions.
The application worked before. Its major duty is to read data off the serial port (COM1) and present data on Linux
On the other port (COM3), I sent packets from the same PC (Windows Simulation) with a string like #z121F2B3.. etc
The previous versions would output 0.1 or 2.5 as a result of converting Ascii to hex and Or(ing) them and adding them together. I can't really explain well... but kind of things like these have been going on...
Outputs are all zeros.
Now Even the previous versions output zeros as well.
My application can sense the arrival of data even displaying them as strings. I used the same logic again, I don't think it would be the program's problem. What I'd rather think is I once deleted all /usr/lib files and directories and reinstalled them. Would that be some libraries missing which led to this mentioned bahaviour of the application?
loat MyPacket::GetFloatData (unsigned int iDataIndex)
// Converts ASCII data stream into a float number
{
long temp;
float ret;
long *cast;
cast = (long *)&ret;
// 8 bytes segments
//unsigned int idx = iDataIndex * 8;
bool MyPacket::CheckContent(void)
// Check the content of the packet received
{
int i;
float j;
for (i = 0; i < NO_ITERATIONS || m_bEOF != true; i++)
{
// check whether it is end of data or a carriage return
if (CheckEOF() || i >= 50) {
SkipCheckSum(2);
m_bEOF = true;
return true;
}
j = GetFloatData(i);
printf ("The reading is %5.2f\n", j);
Skip(8);
}
}
Thanks
Jack
Last edited by lucky6969b; 03-18-2006 at 11:45 PM.
You are passing the data as an ASCII string rather than as numbers
The raw ASCII data arrives as you expect
The conversion from ASCII to numbers on the receiving machine is where the problem lies
The first point is very important because if you are sending the data as numerical data then the problem is probably related to the endianess of the machines.
PS:
I'm assuming, of course, that you haven't changed platforms and that you've verified
that you're actually performing I/O over your RS-232 port (in which case the only
possible answer could be "software regression").
Hmmm - My 64 bit machine is unplugged (at the moment, I'm doing some 16-bit DOS/Windows 9x stuff with LanTastic, believe it or not!), but I'm curious if a "long" is 64 bits or 32 (my recollection is "32", but my recollection is also that the C standard says "long" has to be AT LEAST as long as an "int", so that would make it 64).
The problem could indeed be (the wrong) 32 bits of some argument being truncated in one function call or another, due to the 64 bit CPU.
You say that the data arrives correctly, but isn't being processed correctly?
Have you stepped through "GetFloatData ()" and "AsciitoHex()" under a debugger?
Are you sure "signed char" vs "unsigned char" might not be significant (just shots in the dark there...)?
Try parsing the input string for valid characters, and convert the string to an int while removing unwanted chars.
Try this function:
Code:
int StrToHex(char *input)
{
unsigned int num=0x0;
short err=0;
/* loop through "input" */
int i;
for (i=0; i<8; i++) /* replace 8 with length of string */
{
num *= 0x10; /* next number to add */
TOP:
switch ((int)input[i])
{
case 48: break; /* 48 == '0' */
case 49: num += 0x1; break; /* 49 == '1' */
case 50: num += 0x2; break; /* ... */
case 51: num += 0x3; break;
case 52: num += 0x4; break;
case 53: mun += 0x5; break;
case 54: num += 0x6; break;
case 55: num += 0x7; break;
case 56: num += 0x8; break;
case 57: num += 0x9; break;
case 65: case 97: num += 0xA; break; /* 65 == 'A'; 97 == 'a' */
case 66: case 98: num += 0xB; break; /* 66 == 'B'; 98 == 'b' */
case 67: case 99: num += 0xC; break; /* ... */
case 68: case 100: num += 0xD; break;
case 69: case 101: num += 0xE; break;
case 70: case 102: num += 0xF; break;
default: goto TOP; /* on invalid char, do nothing to "num" */
}
}
return num;
}
also, commpile a short program to see the size of all data types on your computer:
Hi BurninGPi,
Thanks for helping. The result were still -0.0 and -0.0...
And the sizes of the primitive types are
short: 2
int: 4
long: 4
Thanks once again
Jack
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.