PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Calculate word from two bytes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Calculate word from two bytes

Réponse
 
LinkBack Outils de la discussion
Vieux 15/01/2008, 02h18   #1
TBass
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Calculate word from two bytes

The controller I'm talking to sends back a string of bytes. The 8-bit
bytes are actually 16-bit word values sent out in HI-LO order.

So I get: <HI1><LO1><HI2><LO2>....
So register 1 = <H21>*256 + LO1 Register 2 = <HI2>*256+<HI2>
etc....

But I don't seem to be putting the values together correctly in my
code below:



/* THE INLINE FUNCTION getWord */
char
getWord( char HiByte, char LoByte){ return ((HiByte & 0xFF)<<8) |
(LoByte & 0xFF); }


/* The modPacket type */
class CModPacket
{
public:
CModPacket(void);
~CModPacket(void);

unsigned int File;
unsigned int Offset;
unsigned int Length;
unsigned int DataLength;
unsigned int UnitID;

std::vector<unsigned int> Data;

unsigned int TransRef;
};


#include ".\modpacket.h"

CModPacket::CModPacket(void) :
File(4),
Offset(1),
Length(1),
DataLength(0)
{
Data.reserve(255);
}

CModPacket::~CModPacket(void)
{
}


/************************************************** *************
* parseReadMultiple
************************************************** *************/
int
CModbusTCP_Compile::parseReadMultiple( char *pStream,
unsigned int Length,
CModPacket *pPacket )
{

unsigned int byte_length;
unsigned int x;
unsigned int y;
char *p_stream;
std::stringstream mystring;


/* GET NUMBER OF BYTES THAT FOLLOW */
byte_length = pStream[0];

if ( (unsigned int)(byte_length+1) > Length )
{
MessageBox( NULL, "Length no good", "parseReadMultiple", MB_OK );
return false;
}


/* COPY THE DATA TO THE STRUCTURE */
p_stream = &pStream[1];
char myhi;
char mylo;
for( x=0,y=0;x<byte_length;x+=2,++y )
{
myhi = ( p_stream[x] & 0xFF );
mylo = ( p_stream[x+1] &0xFF );

/* At this point, myhi = 0x1D and mylo = 0xE6 */
pPacket->Data[y] = getWord( myhi, mylo );

/* pPacket->Data[y] ends up with the value 4294967270 */
/* Expected value is 0x1D *256 + 0xE6 */
/* So expected value is: 7654 */

mystring.str("");
mystring << "[" << y << "]" << pPacket->Data[y] << " = " << myhi <<
" , " << mylo;
MessageBox( NULL, mystring.str().c_str(), "parseReadMultiple ",
MB_OK );
}

pPacket->DataLength = y;

return true;

} /* parseReadMultiple */



As you can see from the code below, I'm expecting the value 7654, but
end up with 4294967270.
Huh? That's not even a 16-bit number.

If I convert the character values to unsigned ints and put them in a
message box, like so:

mystring.str("");
mystring << "[" << y << "]" << pPacket->Data[y] << " = " <<
(unsigned int_myhi << " , " << (unsigned int)mylo;
MessageBox( NULL, mystring.str().c_str(), "parseReadMultiple ",
MB_OK );

The message returned is:

"4294967270 = 29, 4294967270"

So, the problem is in the low byte, mylo. But how can it equal
4294967270 when it is taken from an 8-byte char that I have AND'd with
0xFF. It should have a maximum value of 255.

I'm confused. Can anyone tell me what I'm doing wrong?

Thanks in advance,
T

  Réponse avec citation
Vieux 15/01/2008, 02h31   #2
TBass
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calculate word from two bytes

Ouch. I see it. Stupid.

/* THE INLINE FUNCTION getWord */
char
getWord( char HiByte, char LoByte){ return ((HiByte & 0xFF)<<8) |
(LoByte & 0xFF); }

should be

unsigned int
getWord( char HiByte, char LoByte){ return ((HiByte & 0xFF)<<8) |
(LoByte & 0xFF); }
  Réponse avec citation
Vieux 15/01/2008, 10h54   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calculate word from two bytes

On Jan 15, 3:31 am, TBass <t...@automateddesign.com> wrote:
> Ouch. I see it. Stupid.


> /* THE INLINE FUNCTION getWord */
> char
> getWord( char HiByte, char LoByte){ return ((HiByte & 0xFF)<<8) |
> (LoByte & 0xFF); }


> should be


> unsigned int
> getWord( char HiByte, char LoByte){ return ((HiByte & 0xFF)<<8) |
> (LoByte & 0xFF); }


You might also want to declare the parameters "unsigned char",
since that's how you use them. If they're unsigned char, on a
machine with 8 bit bytes, you don't need the "& 0xFF"s.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11468 seconds with 11 queries