Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: How to transfer file through serial communication

  1. #21
    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

    Is it me or are you trying to send a QString object through the line? I'm surprised it even compiles... Provided that your write() calls really mean ::write() then the second argument should be a pointer to a const char* buffer. Could you paste the real and current code you're using? Because I don't think there is a strlen() variant that takes a QString as well...

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

    Default Re: How to transfer file through serial communication

    the code which i've posted b4 is the one which i m using currently.

    if u think sending a QString object cud b erroneous then plz suggest wat 2 use instead. i very well know that the second argument requires const char * but the problem is i dont know how to define/declare it and secondly the getch() function returns an integer, again i donno how to convert that integer into const char * . so i just tried to use QString


    i hope now this is not a difficult question 4 u
    please help...
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  3. #23
    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

    Use QString::toAscii().constData( means const char *).

    Regards

  4. #24
    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

    And PLEASE - DON'T use sms lingo here! it is so anoying to read it like that, and there is NO reason for it, unless you are posting through WAP on your mobile.
    ==========================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.

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

    Default Re: How to transfer file through serial communication

    the function as said above doesnt work
    it says theres no such function as QString::toAscii() etc..
    anyways i have done some changes to the code. the entire code is as below :

    Qt Code:
    1. void Form1::send_clicked()
    2. {
    3. int size;
    4. int i,j,k;
    5. QString send;
    6. QFile file("/sfiles/mxdrv.tgz");
    7. if(!file.open( IO_ReadOnly ))
    8. {
    9. QMessageBox::information(0,"Error","Error opening file");
    10. }
    11. else
    12. {
    13. cout<<"File opened successfully"<<endl;
    14. }
    15. cout<<"file opened...."<<endl;
    16. unsigned char fname[]="mxdrv.tgz";
    17. struct termios t;
    18. char *device = "/dev/ttyS0";
    19. int sPort, rbyte, status, sPortdest,length;
    20. long count=0;
    21. unsigned char send1[32];
    22.  
    23. t.c_cc[VMIN] = 1;
    24. t.c_cc[VTIME]=0;
    25. t.c_cc[VEOF]='\n';
    26.  
    27. t.c_iflag |= (ISIG);
    28. t.c_iflag &= ~BRKINT;
    29. t.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
    30.  
    31. sPort = open(device,O_RDWR|O_NOCTTY|O_NDELAY);
    32.  
    33. if(sPort != -1)
    34. {
    35. cout<<"open successfully\n";
    36.  
    37. status = ioctl(sPort,TCSETS,&t);
    38. cout<<"Entering status"<<status<<endl;
    39. if(status==0)
    40. {
    41. //for(count=0;count<20;count++)
    42. cout<<"sending name"<<endl;
    43. rbyte=write(sPort,fname,strlen((char *)fname));
    44. cout<<rbyte<<endl;
    45. QMessageBox::information(0,"sent","Name sent");
    46. //usleep(3000);
    47. cout<<"name sent"<<endl;
    48. cout<<"now sending file"<<endl;
    49. usleep(3000);
    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. GenerateByte(i,send);
    60. //usleep(3000);
    61. rbyte=TEMP_FAILURE_RETRY( write(sPort,send,qstrlen(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. else
    72. {
    73. cout<<"Device could not be opened";
    74. }
    75. file.close();
    76. }
    77. void Form1::GenerateByte(int i, QString &ssend)
    78. {
    79. ssend.sprintf("%d",i);
    80. }
    To copy to clipboard, switch view to plain text mode 
    and about using the QString object in write() function, i have checked it with the previous code of my project, and it is working fine. The only thing is instead of strlen() i have to use qstrlen().
    the code which i have pasted above doesnt give any error while compiling but while transferring file what happens exactly is that the write() function sometimes returns some positive integer and sometimes returnds -1. but most of the time it returns -1.
    then after some time the following comes on the terminal :
    *** glibc detected *** ./filetransfer: munmap_chunk(): invalid pointer: 0xbfab4940 ***
    ======= Backtrace: =========
    /lib/libc.so.6(cfree+0x1bb)[0xbb267b]
    /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x6874871]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QWidget5closeEb+0x1f6)[0x5198e6]
    ./filetransfer[0x8050af0]
    ./filetransfer[0x804fb7f]
    /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(_ZN7QButton15keyReleaseEventEP9QKeyEvent+0 x5a)[0x57fd5a]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN7QWidget5eventEP6QEvent+0x325)[0x519235]
    /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 +0x54d)[0x474a1d]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN9QETWidget17translateKeyEventEPK7_XEven tb+0x36a)[0x408b0a]
    /usr/lib/qt-3.3/lib/libqt-mt.so.3(_ZN12QApplication15x11ProcessEventEP7_XEve nt+0x635)[0x409a75]
    /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+0xe37)[0x804f04f]
    /lib/libc.so.6(__libc_start_main+0xdc)[0xb5ef2c]
    ./filetransfer(_ZN11KMainWindow10setCaptionERK7QStri ngb+0x49)[0x804e391]
    ======= Memory map: ========
    00101000-00117000 r-xp 00000000 03:07 483707 /usr/lib/libart_lgpl_2.so.2.3.17
    00117000-00118000 rwxp 00016000 03:07 483707 /usr/lib/libart_lgpl_2.so.2.3.17
    00118000-0012d000 r-xp 00000000 03:07 686877 /usr/lib/qt-3.3/plugins/styles/bluecurve.so
    0012d000-0012e000 rwxp 00015000 03:07 686877 /usr/lib/qt-3.3/plugins/styles/bluecurve.so
    00132000-00163000 r-xp 00000000 03:07 483702 /usr/lib/liblcms.so.1.0.15
    00163000-00164000 rwxp 00030000 03:07 483702 /usr/lib/liblcms.so.1.0.15
    00164000-00167000 rwxp 00164000 00:00 0
    00167000-00170000 r-xp 00000000 03:07 487954 /lib/libnss_files-2.5.so
    00170000-00171000 r-xp 00008000 03:07 487954 /lib/libnss_files-2.5.so
    00171000-00172000 rwxp 00009000 03:07 487954 /lib/libnss_files-2.5.so
    00193000-001a5000 r-xp 00000000 03:07 483704 /usr/lib/libXft.so.2.1.2
    001a5000-001a6000 rwxp 00012000 03:07 483704 /usr/lib/libXft.so.2.1.2
    001a8000-00210000 r-xp 00000000 03:07 483703 /usr/lib/libmng.so.1.0.0
    00210000-00213000 rwxp 00067000 03:07 483703 /usr/lib/libmng.so.1.0.0
    00215000-00224000 r-xp 00000000 03:07 1329158 /lib/libresolv-2.5.so
    00224000-00225000 r-xp 0000e000 03:07 1329158 /lib/libresolv-2.5.so
    00225000-00226000 rwxp 0000f000 03:07 1329158 /lib/libresolv-2.5.so
    00226000-00228000 rwxp 00226000 00:00 0
    0022a000-00a5b000 r-xp 00000000 03:07 196091 /usr/lib/qt-3.3/lib/libqt-mt.so.3.3.6
    00a5b000-00a9c000 rwxp 00830000 03:07 196091 /usr/lib/qt-3.3/lib/libqt-mt.so.3.3.6
    00a9c000-00aa0000 rwxp 00a9c000 00:00 0
    00ab3000-00ac2000 r-xp 00000000 03:07 483684 /usr/lib/libXext.so.6.4.0
    00ac2000-00ac3000 rwxp 0000e000 03:07 483684 /usr/lib/libXext.so.6.4.0
    00ac3000-00ac4000 r-xp 00ac3000 00:00 0 [vdso]
    00ac5000-00ae4000 r-xp 00000000 03:07 1329151 /lib/libexpat.so.0.5.0
    00ae4000-00ae6000 rwxp 0001e000 03:07 1329151 /lib/libexpat.so.0.5.0
    00ae8000-00b0f000 r-xp 00000000 03:07 483675 /usr/lib/libfontconfig.so.1.1.0
    00b0f000-00b17000 rwxp 00027000 03:07 483675 /usr/lib/libfontconfig.so.1.1.0
    00b19000-00b21000 r-xp 00000000 03:07 483680 /usr/lib/libXrender.so.1.3.0
    00b21000-00b22000 rwxp 00007000 03:07 483680 /usr/lib/libXrender.so.1.3.0
    00b24000-00b28000 r-xp 00000000 03:07 483688 /usr/lib/libXfixes.so.3.1.0
    00b28000-00b29000 rwxp 00003000 03:07 483688 /usr/lib/libXfixes.so.3.1.0
    00b2c000-00b45000 r-xp 00000000 03:07 1329142 /lib/ld-2.5.so
    00b45000-00b46000 r-xp 00018000 03:07 1329142 /lib/ld-2.5.so
    00b46000-00b47000 rwxp 00019000 03:07 1329142 /lib/ld-2.5.so
    00b49000-00c80000 r-xp 00000000 03:07 1329143 /lib/libc-2.5.so
    00c80000-00c82000 r-xp 00137000 03:07 1329143 /lib/libc-2.5.so
    00c82000-00c83000 rwxp 00139000 03:07 1329143 /lib/libc-2.5.so
    00c83000-00c86000 rwxp 00c83000 00:00 0
    00c88000-00cad000 r-xp 00000000 03:07 1329150 /lib/libm-2.5.so
    00cad000-00cae000 r-xp 00024000 03:07 1329150 /lib/libm-2.5.so
    00cae000-00caf000 rwxp 00025000 03:07 1329150 /lib/libm-2.5.so
    00cb1000-00cb3000 r-xp 00000000 03:07 1329144 /lib/libdl-2.5.so
    00cb3000-00cb4000 r-xp 00001000 03:07 1329144 /lib/libdl-2.5.so
    00cb4000-00cb5000 rwxp 00002000 03:07 1329144 /lib/libdl-2.5.so
    00cb7000-00cca000 r-xp 00000000 03:07 1329145 /lib/libpthread-2.5.so
    00cca000-00ccb000 r-xp 00012000 03:07 1329145 /lib/libpthread-2.5.so
    00ccb000-00ccc000 rwxp 00013000 03:07 1329145 /lib/libpthread-2.5.so
    00ccc000-00cce000 rwxp 00ccc000 00:00 0
    00cd0000-00ce2000 r-xp 00000000 03:07 483673 /usr/lib/libz.so.1.2.3
    00ce2000-00ce3000 rwxp 00011000 03:07 483673 /usr/lib/libz.so.1.2.3
    00ce5000-00cea000 r-xp 00000000 03:07 483678 /usr/lib/libXdmcp.so.6.0.0
    00cea000-00ceb000 rwxp 00004000 03:07 483678 /usr/lib/libXdmcp.so.6.0.0
    00ced000-00cef000 r-Aborted

    i dont know wat does this mean.

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

    regards
    shamik

  6. #26
    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

    When I said QString::toAscii().constData() I meant the method name and location.
    But now I see you're using Qt3. Sorry about that .
    In Qt3 you must use send.ascii(). This will give you a const char*.
    And you must use this, because the code is still not correct.

    For length, you can use send.length() or send.size(). I don't know which one is available in Qt3.

    Regards

  7. #27
    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 used send.ascii() instead of send

    the qstrlen() function perfectly gives the length of the string so i dont think the problem is there.

    still the output is the same.
    the write() function which returns -1 only when there is an error while sending, and some other digit when data is sent corrently, most of the time gives -1 and only few times somewhere in between gives some other digits.
    later after some more time, the above mentioned error comes

    i donno wat is wrong over here. i think now its becoming difficult to find out.
    There are 10 kinds of people in this world. Those who understand binary, and those who dont.

    regards
    shamik

  8. #28
    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

    For length, you can use send.length() or send.size(). I don't know which one is available in Qt3.
    The reason is, that qstrlen() returns the length of the given string, but QString is not a string, its a class that manages a string, so QString is an object containing a string.
    send.length() will return the length of the string it self which is contained by the QString object.

    you posted your last post while I was writing mine so:
    the qstrlen() function perfectly gives the length of the string so i dont think the problem is there.
    I can't explain this, it should not be so, because of the explanation above.
    You are giving a QString object to qstrlen() and it awaits a const char*.
    As wysota said, this should not even compile.
    ==========================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.

  9. #29
    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

    qstrlen works because QString has operator const char * overloaded in Qt3.
    But it is recommended not to be used.

  10. #30
    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

    The only thing is instead of strlen() i have to use qstrlen().
    the code which i have pasted above doesnt give any error while compiling but while transferring file what happens exactly is that the write() function sometimes returns some positive integer and sometimes returnds -1. but most of the time it returns -1.
    See what is the value of errno after write returns -1.
    Something like this( from MSDN ):
    Qt Code:
    1. if (( bytesWritten = _write( fileHandle, buffer, sizeof( buffer ))) == -1 )
    2. {
    3. switch(errno)
    4. {
    5. case EBADF:
    6. perror("Bad file descriptor!");
    7. break;
    8. case ENOSPC:
    9. perror("No space left on device!");
    10. break;
    11. case EINVAL:
    12. perror("Invalid parameter: buffer was NULL!");
    13. break;
    14. default:
    15. // An unrelated error occured
    16. perror("Unexpected error!");
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    Regards

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.