Results 1 to 20 of 20

Thread: TCP Write Raw data

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TCP Write Raw data

    Quote Originally Posted by ^NyAw^ View Post
    Ok, so it's only important because of the data that will be sent becomes longer and it could be smaller.
    It isn't about sending few bytes less, but about adhering to given protocol. If it requires you to send a one-byte field followed by two-byte one, you can't send 1B of data, 1B of garbage and then 2B of data. If you use some custom protocol and both sender and receiver follow the same rules of alignment, you don't have to worry about it, but if you want to implement Modbus, you have to follow its specification and there are no holes in Modbus messages.

    Quote Originally Posted by ^NyAw^ View Post
    But if forcing not to align the data, when using the variables in memory will produce TLB miss or a page faults
    Yes, some CPUs can't even fetch unaligned data from memory, so accesses to such structures might be split into several operations.

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Write Raw data

    Hi,

    Searching for the web I found this two helpful links (for Visual Studio compiler):
    http://msdn2.microsoft.com/en-us/lib...65(VS.80).aspx
    http://msdn2.microsoft.com/en-us/lib...y1(VS.80).aspx

    The second one works for me:
    Qt Code:
    1. #pragma pack(1)
    2. struct MB_Request
    3. {
    4. uchar function_code;
    5. quint16 start_adr;
    6. quint16 quantity_regs;
    7. };
    8.  
    9. #pragma pack(1)
    10. struct MBAP_Header
    11. {
    12. quint16 transaction_id;
    13. quint16 protocol_id;
    14. quint16 len;
    15. uchar unit_id;
    16. };
    17.  
    18. #pragma pack(1)
    19. struct MB_Message
    20. {
    21. MBAP_Header mb_hdr;
    22. MB_Request mb_req;
    23. };
    To copy to clipboard, switch view to plain text mode 

    Then, executing your example code, the structs are unaligned as I need for Modbus protocol.

    Could I use "#ifndef" statment to know wich compiler is used to then use "#pragma pack(1)" or "__attribute__((packed))" depending on compiler?
    So when I use GCC on GNU/Linux(or windows also) it will pack the data unaligned as expected. Could use GCC with Visual Studio?

    Thanks,

    Thanks,
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TCP Write Raw data

    Quote Originally Posted by ^NyAw^ View Post
    Could I use "#ifndef" statment to know wich compiler is used to then use "#pragma pack(1)" or "__attribute__((packed))" depending on compiler?
    Yes, but using __desclspec(align(1)) might be more cleaner, because you will need only two macros (depending on the compiler one of them will be empty), instead of ifdef before each structure.

    Quote Originally Posted by ^NyAw^ View Post
    Could use GCC with Visual Studio?
    AFAIR it's possible, but I don't use VS, so I can't help you with that.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Write Raw data

    Hi,

    Yes, but using __desclspec(align(1)) might be more cleaner
    I've tryied this:
    Qt Code:
    1. __declspec(align(1)) struct MB_Request
    2. {
    3. uchar function_code;
    4. quint16 start_adr;
    5. quint16 quantity_regs;
    6. };
    7.  
    8. int main()
    9. {
    10. int iA1 = __alignof(MBAP_Header); // This returns "2" byte alignment and using #pragma pack(1) returns me "1" " byte alignment
    11. }
    To copy to clipboard, switch view to plain text mode 

    instead of ifdef before each structure
    I've found that GCC also supports this #pragma for compatibility with Win32:
    http://gcc.gnu.org/onlinedocs/gcc/St...g-Pragmas.html

    Thanks,
    Òscar Llarch i Galán

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TCP Write Raw data

    Quote Originally Posted by ^NyAw^ View Post
    __declspec(align(1)) struct MB_Request
    ...
    int iA1 = __alignof(MBAP_Header); // This returns "2" byte
    Have you placed __declspec before MBAP_Header too?

    Quote Originally Posted by ^NyAw^ View Post
    I've found that GCC also supports this #pragma for compatibility with Win32
    In that case, you don't need any ifdefs.

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Write Raw data

    Hi,

    Have you placed __declspec before MBAP_Header too?
    Yes:
    Qt Code:
    1. //#pragma pack(1)
    2. __declspec(align(1)) struct MB_Request
    3. {
    4. uchar function_code;
    5. quint16 start_adr;
    6. quint16 quantity_regs;
    7. };
    8.  
    9. //#pragma pack(1)
    10. __declspec(align(1)) struct MBAP_Header
    11. {
    12. quint16 transaction_id;
    13. quint16 protocol_id;
    14. quint16 len;
    15. uchar unit_id;
    16. };
    17.  
    18. //#pragma pack(1)
    19. __declspec(align(1)) struct MB_Message
    20. {
    21. MBAP_Header mb_hdr;
    22. MB_Request mb_req;
    23. };
    To copy to clipboard, switch view to plain text mode 

    But
    Qt Code:
    1. int iA1 = __alignof(MBAP_Header);
    2. int iA2 = __alignof(MB_Request);
    3. int iA3 = __alignof(MB_Message);
    To copy to clipboard, switch view to plain text mode 
    Returns always "2" for iA1, iA2 and iA3.

    Don't worry about it, using "#pragma pack(1)" is going well.

    Thanks for your time and your knowdelege.
    Òscar Llarch i Galán

Similar Threads

  1. Replies: 4
    Last Post: 19th October 2007, 19:47
  2. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14
  3. FSWriteFork in MAC OS to write data to a file.
    By vishal.chauhan in forum General Programming
    Replies: 5
    Last Post: 2nd July 2007, 06:48
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.