0

First, I have the following typedefs :

typedef unsigned char   BYTE;    
typedef unsigned short  WORD;  

Then, I have a function in class A, which calculates a value with the type WORD:

    class A {
        ...

        private:
           WORD calculatedData;
        ...

        void calculateCanData()
        {
            ...
            calculatedData = calcCrc();
            B::setCanData(m_canId, &calculatedData, sizeof(calculatedData) );
        }

    } 

In class B, I have the setData() function with the following interface:

void B::setCanData(WORD canId, BYTE * data, int length)

Now, I can't send a WORD at once, so I guess I need to somehow split the WORD into two BYTEs and the pass them as parameters to the function?

Or is there a better solution?

Wballer3
  • 141
  • 1
  • 9
  • 5
    If you care about the exact size of your types, consider using the [fixed width integer types](http://en.cppreference.com/w/cpp/types/integer). – François Andrieux Jun 01 '18 at 14:45
  • 4
    Your example is incomplete. It uses many identifiers that you haven't defined and contains many errors and typos. Please read about [MCVE]s. – François Andrieux Jun 01 '18 at 14:46

1 Answers1

1

The function prototype for void B::setCanData(WORD canId, BYTE * data, int length) looks like it is designed to send an arbitrary number of bytes by specifying the address and the length, which I assume is number of bytes.

So you should be able to do something like

B::setCanData( m_canId, (BYTE * )&calculatedData, sizeof(calculatedData) );.

I assume that the type for calculatedData is a WORD? You may need to consider whether the endiness or the byte order for a WORD as it is stored is the appropriate byte order for the protocol you are using or if you will need to swap bytes around. That will really depend on the expectation of what ever is consuming this data stream you are sending.

I expect that the function calcCrc() calculates some kind of a CRC which is part of a data stream and you are providing the CRC for an already sent message in a series of bytes. I would expect there is documentation on the actual format of the message including the CRC along with information about byte order of the message being provided.

If you provide more details about the protocol by updating your posting, it would help people to provide a better answer than this one.

Wballer3
  • 141
  • 1
  • 9
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106