Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: How to transfer file through serial communication

  1. #1
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default How to transfer file through serial communication

    hii friends,

    i am using Qt3 which comes bydefault with FC-6 installation.
    i want to transfer files like zip, pdf, jpeg etc through serial communication (i.e. using com port)

    can anyone please tell me how can i achieve this?!?
    if possible please provide me with a sample code.
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to transfer file through serial communication

    Please start by searching the forum.

  3. #3
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to transfer file through serial communication

    i've downloaded the QExtSerialPort1.1 from the net

    in this when i go to the /example/ folder and type "make" to compile everything
    it gives following error :

    make
    make: Warning: File `Makefile' has modification time 2.3e+08 s in the
    future
    g++ -c -pipe -Wall -W -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2
    -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32
    -march=i386 -mtune=generic -fasynchronous-unwind-tables -D_REENTRANT
    -D_TTY_POSIX_ -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED
    -I/usr/lib/qt-3.3/mkspecs/default -I. -I../.. -I/usr/lib/qt-3.3/include
    -Iuic/ -Imoc/ -o obj/main.o main.cpp
    main.cpp:8:28: error: qextserialport.h: No such file or directory
    In file included from main.cpp:12:
    MessageWindow.h:12:25: error: qdockwidget.h: No such file or directory
    MessageWindow.h:20: error: expected class-name before ‘{’ token
    MessageWindow.h:20: warning: ‘class MessageWindow’ has virtual functions
    but non-virtual destructor
    make: *** [obj/main.o] Error 1

    can anybody please help me with this ?
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to transfer file through serial communication

    main.cpp:8:28: error: qextserialport.h: No such file or directory
    This seems obvious - the file is missing.

    MessageWindow.h:12:25: error: qdockwidget.h: No such file or directory
    This suggest you've downloaded the wrong release as QDockWidget is part of Qt4 and not Qt3.

  5. #5
    freeskydiver Guest

    Default Re: How to transfer file through serial communication

    Quote Originally Posted by shamik View Post
    ...
    MessageWindow.h:12:25: error: qdockwidget.h: No such file or directory
    can anybody please help me with this ?
    Look to the file. If it exists? Then look for QTDIR.

  6. #6
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to transfer file through serial communication

    u're right......i had downloaded a wrong package

    now i have downloaded a correct one which works with Qt3. The make command has generated the libraries well.

    but i dont know how to use this class and its functions.

    all the function i found deals with byte transfer i.e. getch(), putch() etc.

    but i want to transfer and recieve files like zip, pdf etc.

    i cant find any such functions directly sending and recieving files...

    can any1 plz suggest
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to transfer file through serial communication

    Use getch or putch in a loop.

  8. #8
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to transfer file through serial communication

    suppose its a zip file stored somewhere like /root/xp.zip

    then how to use that getch() and putch() ??
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to transfer file through serial communication

    Oh come on.... What kind of a question is that?
    Open the file, read characters and transmit them over the serial line. You'll probably need some protocol of communication to transfer the file length and file name. You can probably use QDataStream to ease your task.

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to transfer file through serial communication

    no need to use getch().
    Just use readBytes() and you don't need a loop.
    But the rest is the same as wysota said.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to transfer file through serial communication

    Hi,

    Plenty of serial comms classes on the web...

    Personally, I use my own I wrote which is for windows, if it is for windows you want to use serial comms, just use api calls such as ::CreateFile, ::WriteFile etc...

    Here is some code from my serial class, may help ( this is for Windows )?

    Qt Code:
    1. template<class T=char, int nSize=100> class CSerialComms
    2. {
    3. public:
    4. T& operator[]( int nIndex ) { return m_buf[nIndex]; }
    5.  
    6. CSerialComms& operator=( const CSerialComms& r )
    7. {
    8. memcpy( m_buf, r.m_buf, nSize );
    9. m_cto = r.m_cto;
    10. m_dcb = r.m_dcb;
    11. m_hFile = r.m_hFile;
    12. return *this;
    13. }
    14.  
    15.  
    16. inline int SetTimeouts( DWORD dwReadInterval = 1, DWORD dwReadTotalTimeout = 100, DWORD dwReadTotalTimeoutConstant = 1000, DWORD dwWriteTotalTimeout = 0, DWORD dwWriteTotalTimeoutConstant = 0 )
    17. {
    18. int nError = SUCCESS;
    19. m_cto.ReadIntervalTimeout = dwReadInterval;
    20. m_cto.ReadTotalTimeoutMultiplier = dwReadTotalTimeout;
    21. m_cto.ReadTotalTimeoutConstant = dwReadTotalTimeoutConstant;
    22. m_cto.WriteTotalTimeoutMultiplier = dwWriteTotalTimeout;
    23. m_cto.WriteTotalTimeoutConstant = dwWriteTotalTimeoutConstant;
    24. if( !::SetCommTimeouts( m_hFile, &m_cto) )
    25. {
    26. nError = GetLastError();
    27. }
    28. return nError;
    29. }
    30.  
    31. inline int OpenPort( LPCTSTR szPort = "COM1" )
    32. {
    33. int nError = SUCCESS;
    34. m_hFile = ::CreateFile( szPort, GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
    35. if( m_hFile == INVALID_HANDLE_VALUE )
    36. {
    37. nError = GetLastError();
    38. }
    39. return nError;
    40. }
    41.  
    42. inline int SetCommsDevice( DWORD dwBaud = 57600, BYTE iParity = NOPARITY, BYTE iStopBits = ONESTOPBIT, BYTE iByteSize = 8 )
    43. {
    44. int nError = SUCCESS;
    45. memset( &m_dcb, 0, sizeof( m_dcb ) );
    46. m_dcb.DCBlength = sizeof( m_dcb );
    47. m_dcb.BaudRate = dwBaud;
    48. m_dcb.Parity = iParity;
    49. m_dcb.StopBits = iStopBits;
    50. m_dcb.ByteSize = iByteSize;
    51.  
    52. if( !::SetCommState( m_hFile, &m_dcb ) )
    53. {
    54. nError = GetLastError();
    55. }
    56. return nError;
    57. }
    58.  
    59. DWORD TxData( T* szBuf )
    60. {
    61. DWORD nowrote = 0;
    62. memset( m_buf, 0, sizeof( m_buf ) );
    63. if( sizeof( m_buf ) >= strlen( szBuf ) ) // check to see if buffer allocated is big enough
    64. {
    65. memcpy( &m_buf[1], szBuf, strlen( szBuf ) );
    66. m_buf[0] = 0x2;
    67. m_buf[ strlen(szBuf) + 1 ] = 0x3;
    68. return TxData();
    69. }
    70. return BUFFEROVERFLOW;
    71. }
    72.  
    73. DWORD TxData()
    74. {
    75. DWORD nowrote = 0;
    76. ::WriteFile( m_hFile, m_buf, strlen(m_buf), &nowrote, NULL );
    77. return nowrote;
    78. }
    79.  
    80. CSerialComms( void ){ memset( m_buf, 0, nSize ); };
    81. ~CSerialComms(){};
    82.  
    83.  
    84. private:
    85. T m_buf[nSize];
    86. COMMTIMEOUTS m_cto;
    87. DCB m_dcb;
    88. HANDLE m_hFile;
    89.  
    90. enum { FAILED = 0, SUCCESS = 1, BUFFEROVERFLOW = 2 };
    91. };
    92.  
    93. #endif // !defined(AFX_SERIALCOMMS_H__BA76EBFF_6F02_4E79_BC65_DD4C2655354B__INCLUDED_)
    To copy to clipboard, switch view to plain text mode 


    Regards,
    Steve

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to transfer file through serial communication

    Plenty of serial comms classes on the web...
    True, but QextSerialPort is based on QIODevice and therefore integrates well withe Qt Stream classes, and you can sublass it and add signals and slots to it (if you need such a thing, I didn't so far)
    And ofcourse, QextSerialPort is cross platform!
    Last edited by high_flyer; 22nd May 2007 at 15:46.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to transfer file through serial communication

    here's wat i have done

    Qt Code :

    Qt Code:
    1. int size;
    2. int *i,j,k;
    3. QFile file("/sfiles/mxdrv.tgz");
    4. if(!file.open( IO_ReadOnly ))
    5. {
    6. QMessageBox::information(0,"Error","Error opening file");
    7. }
    8. else
    9. {
    10. cout<<"File opened successfully"<<endl;
    11. }
    12. cout<<"file opened...."<<endl;
    13. unsigned char fname[]="mxdrv.tgz";
    14.  
    15. struct termios t;
    16. char *device = "/dev/ttyS0";
    17.  
    18. int sPort, rbyte, status, sPortdest,length;
    19. long count=0;
    20.  
    21. unsigned char send1[32];
    22.  
    23.  
    24. t.c_cc[VMIN] = 1;
    25. t.c_cc[VTIME]=0;
    26. t.c_cc[VEOF]='\n';
    27.  
    28. t.c_iflag |= (ISIG);
    29. t.c_iflag &= ~BRKINT;
    30. t.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
    31.  
    32. sPort = open(device,O_RDWR|O_NOCTTY|O_NDELAY);
    33.  
    34. if(sPort != -1)
    35. {
    36. cout<<"open successfully\n";
    37.  
    38. status = ioctl(sPort,TCSETS,&t);
    39. cout<<"Entering status"<<status<<endl;
    40. if(status==0)
    41. {
    42.  
    43. //for(count=0;count<20;count++)
    44. cout<<"sending name"<<endl;
    45. rbyte=write(sPort,fname,strlen((char *)send1));
    46. //usleep(3000);
    47. cout<<"name sent"<<endl;
    48. cout<<"now sending file"<<endl;
    49. while(!file.atEnd())
    50.  
    51. {
    52. //cout<<"Enter :";
    53. //cin>>send1;
    54. //strcat((char *)send1,"{MSS TYPE-D DAMA}");
    55. // write(sPort,"{012345678}",11);
    56. *i=file.getch();
    57. rbyte=write(sPort,i,sizeof(*i));
    58. cout<<rbyte<<"sent"<<endl;
    59. //cout<<"w b:"<<rbyte<<"\t Time:"<<count<<"\t string:"<<send1<<"\n";
    60. //usleep(1000);
    61.  
    62. }
    63.  
    64. }
    65. close(sPort);
    66.  
    67. }
    68. else
    69. {
    70. cout<<"Device could not be opened";
    71. }
    72. file.close();
    To copy to clipboard, switch view to plain text mode 


    by running this code from the shell, following is the output :

    File opened successfully
    file opened....
    open successfully
    Entering status0
    sending name
    name sent
    now sending file
    Segmentation fault


    can any1 please tell watz going wrong over here ??
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  14. #14
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to transfer file through serial communication

    As far as I can see, int *i is not allocated anywhere.
    You must allocate i before you can use it.

    Regards

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to transfer file through serial communication

    Don't use "int *i" but "int i". You have an uninitialised pointer. I don't see any reason why would you want to use a pointer to an int, so just use the object directly.

  16. #16
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to transfer file through serial communication

    sorry
    please refer the post below
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  17. #17
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to transfer file through serial communication

    rather i've used QString with a little bit modification

    Code :
    Qt Code:
    1. int size;
    2. int i,j,k;
    3. QString send="";
    4. QFile file("/sfiles/mxdrv.tgz");
    5. if(!file.open( IO_ReadOnly ))
    6. {
    7. QMessageBox::information(0,"Error","Error opening file");
    8. }
    9. else
    10. {
    11. cout<<"File opened successfully"<<endl;
    12. }
    13. cout<<"file opened...."<<endl;
    14. unsigned char fname[]="mxdrv.tgz";
    15.  
    16. struct termios t;
    17. char *device = "/dev/ttyS0";
    18.  
    19. int sPort, rbyte, status, sPortdest,length;
    20. long count=0;
    21.  
    22. unsigned char send1[32];
    23.  
    24.  
    25. t.c_cc[VMIN] = 1;
    26. t.c_cc[VTIME]=0;
    27. t.c_cc[VEOF]='\n';
    28.  
    29. t.c_iflag |= (ISIG);
    30. t.c_iflag &= ~BRKINT;
    31. t.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
    32.  
    33. sPort = open(device,O_RDWR|O_NOCTTY|O_NDELAY);
    34.  
    35. if(sPort != -1)
    36. {
    37. cout<<"open successfully\n";
    38.  
    39. status = ioctl(sPort,TCSETS,&t);
    40. cout<<"Entering status"<<status<<endl;
    41. if(status==0)
    42. {
    43.  
    44. //for(count=0;count<20;count++)
    45. cout<<"sending name"<<endl;
    46. rbyte=write(sPort,fname,strlen((char *)fname));
    47. //usleep(3000);
    48. cout<<"name sent"<<endl;
    49. cout<<"now sending file"<<endl;
    50. while(!file.atEnd())
    51.  
    52. {
    53. //cout<<"Enter :";
    54. //cin>>send1;
    55. //strcat((char *)send1,"{MSS TYPE-D DAMA}");
    56. // write(sPort,"{012345678}",11);
    57. cout<<"getting byte from file"<<endl;
    58. i=file.getch();
    59. send.sprintf("%d",i);
    60. cout<<send<<endl;
    61. rbyte=write(sPort,send,strlen(send));
    62. cout<<rbyte<<"sent"<<endl;
    63. //cout<<"w b:"<<rbyte<<"\t Time:"<<count<<"\t string:"<<send1<<"\n";
    64. //usleep(1000);
    65.  
    66. }
    67.  
    68. }
    69. close(sPort);
    70.  
    71. }
    72. else
    73. {
    74. cout<<"Device could not be opened";
    75. }
    76. file.close();
    To copy to clipboard, switch view to plain text mode 



    when i run the program from the terminal, the bytes get transfered which i can see in the hyperterminal of some other pc.
    but then after some time the following comes on the terminal and the program exits abruptly:

    *** glibc detected *** ./filetransfer: munmap_chunk(): invalid pointer: 0xbffe8e70 ***
    ======= Backtrace: =========
    /lib/libc.so.6(cfree+0x1bb)[0xe5d67b]
    /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x6874871]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QWidget5closeEb+0x1f6)[0x5198e6]
    ./filetransfer[0x804fdcb]
    ./filetransfer[0x804fa3b]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QObject15activate_signalEP15QConnectio nListP8QUObject+0x16a)[0x4dbe2a]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QObject15activate_signalEi+0xbd)[0x4dc95d]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QButton7clickedEv+0x2c)[0x86f8ac]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QButton17mouseReleaseEventEP11QMouseEv ent+0xbd)[0x57fa3d]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QWidget5eventEP6QEvent+0x3c5)[0x5192d5]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication14internalNotifyEP7QObjec tP6QEvent+0x9b)[0x4730fb]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication6notifyEP7QObjectP6QEvent +0x287)[0x474757]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN9QETWidget19translateMouseEventEPK7_XEv ent+0x576)[0x40af26]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication15x11ProcessEventEP7_XEve nt+0x5e6)[0x409a26]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN10QEventLoop13processEventsEj+0x4eb)[0x41b6ab]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN10QEventLoop9enterLoopEv+0x42)[0x48c672]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN10QEventLoop4execEv+0x26)[0x48c536]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication4execEv+0x1f)[0x472c0f]
    ./filetransfer(_ZN7QWidget6createEmbb+0xe2f)[0x804ef2f]
    /lib/lAborted


    any suggestions ??
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  18. #18
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to transfer file through serial communication

    Well, what button did you pressed?
    From the backtrace it seems that you pressed a button.

    BTW, do you use threads in your app?

  19. #19
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to transfer file through serial communication

    i dont use any threads in the application

    and yes i've pressed a button
    actually after pressing that button the code is executed coz the code is written in the click event of that button
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  20. #20
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to transfer file through serial communication

    What does close(sPort)?
    Can you post the code for that?

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.