LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-18-2006, 11:37 PM   #1
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
Mysterious Error


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;

printf ("Data now = %s\n", m_pData);
temp = (long) AsciiToHex (m_pData[0]) << 28 |
(long) AsciiToHex (m_pData[1]) << 24 |
(long) AsciiToHex (m_pData[2]) << 20 |
(long) AsciiToHex (m_pData[3]) << 16 |
(long) AsciiToHex (m_pData[4]) << 12 |
(long) AsciiToHex (m_pData[5]) << 8 |
(long) AsciiToHex (m_pData[6]) << 4 |
(long) AsciiToHex (m_pData[7]);

*cast = temp;
return ret;
}

char MyPacket::AsciiToHex (char cHexchar)
// Convert ASCII to Hex
{
if (cHexchar >= '0' && cHexchar <= '9')
return cHexchar - '0';
else if (cHexchar >= 'A' && cHexchar <= 'F')
return cHexchar - 'A' + 10;

fprintf (stderr, "EOF, Invalid or Corrupted Data\n");
return (char) NULL;
}

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.
 
Old 03-19-2006, 08:23 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352

Rep: Reputation: 129Reputation: 129
Can you please clarify a few points.
  • 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.
 
Old 03-19-2006, 03:00 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Version control is Good. If you're not already using it, please do so ASAP:

http://www.cvsgui.org
http://subversion.tigris.org/
etc

See also:
http://www.pragmaticprogrammer.com/bookshelf/index.html

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").

Last edited by paulsm4; 03-19-2006 at 06:04 PM.
 
Old 03-19-2006, 07:32 PM   #4
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
Hi,
Actually, the data part is going to be stripped like
#z123f4e5d

I would work on
123f4e5d
etc
The data arrived as expected, I could see signals messages appearing.

I reverse the order of the ASCII String, still 0.0 and -0.0

I recently migrated from Pentium 4 3.2E(32-bit) to Pentium 4 630 (64-bit)

Thx
Jack

Last edited by lucky6969b; 03-19-2006 at 08:57 PM.
 
Old 03-19-2006, 09:23 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,352

Rep: Reputation: 129Reputation: 129
What happens when you convert the character to uppercase first?

Have you put in diagnostics in the MyPacket::AsciiToHex() method?
 
Old 03-19-2006, 09:35 PM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
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...)?
 
Old 03-20-2006, 05:35 PM   #7
burninGpi
Member
 
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163

Rep: Reputation: 30
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:
Code:
#include <stdio.h>

int main()
{
   printf("short: %d\n", sizeof(short));
   printf("int:   %d\n", sizeof(int));
   printf("long:  %d\n", sizeof(long));
   return 0;
}
 
Old 03-20-2006, 07:39 PM   #8
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysterious Slowing billquinn Linux - General 2 09-14-2005 05:16 PM
Mysterious restart ec3042 Linux - General 12 04-20-2005 09:55 PM
mysterious crashes in computer tuxombie Linux - Hardware 0 02-04-2005 02:05 PM
Mysterious X crash theeeg Linux - Software 3 08-09-2004 04:15 AM
Mysterious Directory Dooley Mandriva 3 07-03-2004 12:26 AM


All times are GMT -5. The time now is 09:03 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration