1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Converting data into a stream of bytes

Discussion in 'C' started by boschow, Feb 23, 2008.

  1. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Hi all,

    i am tring to implement modbus communication protocol. Since socket programing have no sense of data type and they operate only with bytes i have to make 4 functions to convert data into stram of bytes. I need to write a function to convert coils (bits) into bytes, to conver char into bytes, to convert intgers into bytes and to convert long into bytes. I dont have any experience how to do this, please help me on this one.

    Best regards,
    BoSChoW.
     
  2. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    I have figured out something but a new problem emerged. I have a structure with various types and i want to know the size of each type in order to determine the number of bytes of each variable. So i made something like this:

    Code:
    typedef struct
    {
    int a;
    short b;
    long c;
    short e;
    int a;
    .
    .
    .
    }modbus;
    
    int main()
    {
    modbus bus;
    modbus *busPtr;
    busPtr = &bus
    
    int typeSize = sizeof(busPtr->a); // this works
    typeSize = sizeof(&busPtr[0]); // this dosent work
    
    //i am tring to achieve something like this
    for (int i = 0; i < 4; i++)
    {
         typeSize = sizeof(&busPtr[i]); // this dosent work
    }
    }
    
    Does anybody know how this is done ?

    Thanks and best regards,
    BoSCHoW.
     

Share This Page