Results 1 to 16 of 16

Thread: QT segmentation falut when communicating with a bluetooth function..

  1. #1
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default QT segmentation falut when communicating with a bluetooth function..

    Hi every one,

    I am trying to integrate the the bluetooth functionality with the QT applciation. I was just wondering what would be the problem with the sprintf statement(tested out both) but does not work...

    the function is called from the main function which specifies the struct and initialises its comPort ... Then the data are passed to the connectRobot function and hence this error...

    A error of segmentation is shown up if i debug the program at line 9.... crashing the whole program when I run it...

    any suggestions on the mistake i would have made...


    Thanking you...

    Rajesh Medampudi


    Qt Code:
    1. int connectRobot(struct robot *rbt, int comPort)
    2. {
    3.  
    4.  
    5.  
    6. char portname[20];
    7. sprintf(portname,"COM%d",comPort);
    8. //sprintf(portname,"\\\\.\\COM%d",comPort);
    9. hSerialPort[rbt->serialConnection] = CreateFileA(portname,
    10. GENERIC_READ | GENERIC_WRITE,
    11. 0,
    12. 0,
    13. OPEN_EXISTING,
    14. FILE_ATTRIBUTE_NORMAL,
    15. 0);
    16. if (hSerialPort[rbt->serialConnection] == INVALID_HANDLE_VALUE) return -1;
    17.  
    18.  
    19.  
    20. dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
    21. if (!GetCommState(hSerialPort[rbt->serialConnection], &dcbSerialParams)) return -2;
    22.  
    23. dcbSerialParams.BaudRate = 115200;
    24. dcbSerialParams.ByteSize = 8;
    25. dcbSerialParams.StopBits = ONESTOPBIT;
    26. dcbSerialParams.Parity = NOPARITY;
    27.  
    28. comt.ReadIntervalTimeout = 500;
    29. SetCommTimeouts( hSerialPort[rbt->serialConnection], &comt);
    30. if (!SetCommState(hSerialPort[rbt->serialConnection], &dcbSerialParams)) return -3;
    31.  
    32. rbt->port=comPort;
    33. rbt->connected=1;
    34.  
    35.  
    36.  
    37. return 0;
    38. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    Where do you initialize hSerialPort and how do you call connectRobot()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    I just initialized them right before in the same file at the start....

    Qt Code:
    1. struct robot robots[7];
    2.  
    3.  
    4. HANDLE hSerialPort[7]; //up to 7 connection
    5. DCB dcbSerialParams={0};
    6. COMMTIMEOUTS comt;
    7.  
    8. // fine
    9.  
    10.  
    11.  
    12. //stat
    13. clock_t startTime;
    14. clock_t endTime;
    15.  
    16. int dtime;
    To copy to clipboard, switch view to plain text mode 


    and connect robot is called something like this....


    Qt Code:
    1. connectRobot(&robots[1], 1036);
    To copy to clipboard, switch view to plain text mode 
    Last edited by medampudi; 13th September 2010 at 15:56. Reason: updated contents

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

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    And what does the constructor for struct robot look like? Is the serialConnection field initialized properly?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    anything else that i need for this....



    Qt Code:
    1. struct robot
    2. {
    3. int connected;
    4. int serialConnection; //refers to HANDLE vector hSerialPort
    5. int port;
    6.  
    7. //Sensors
    8. int IR[8]; //infrared sensors 'N'
    9. int LI[8]; //light sensors 'O'
    10. int FL[3]; //floor sensors 'M'
    11. int AC[3]; //acceleration s. 'A'
    12. int MC[3]; //microphone 'U'
    13. int MR[6]; //microretina //cam is dived in 6 sector an then for each sector the number of pixel of a target color is computed
    14. char videoBuffer[1600]; //img 'I'
    15. int imageRGB[1600]; //rgb image built upon videobuffer
    16. char sensorsBuffer[60];
    17. int camW; //cam width 'X cam_mode,cam_widht, cam_heigth,cam_zoom :expressed in byte
    18. int camH; //cam height
    19. int camZ; //cam zoom
    20. //used by Joachim
    21. int old_floor_input[2]; //floor input from previous timestep
    22. int floor_mean[3]; //mean floor value from previous timesteps
    23. int target_prototype[3];//prototypes of floorsensor values for [0]=no target(white), [1]=target1(grey), [2]=target2(black)
    24. int epuck_angle_old; //angle of other epuck at previous timestep
    25. float old_input[3]; //camera input from previous timestep
    26.  
    27.  
    28. };
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    But do you initialize the object in any way? If not, then rbt->serialConnection will return a random value and you will dereference the hSerialPort array in a wrong way causing a crash of your program.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    Is this what was on your mind....



    Qt Code:
    1. int initRobots()
    2. {
    3. int id;
    4. for(id=0;id<7;id++)
    5. {
    6. robots[id].serialConnection=id;
    7. robots[id].port=-1;
    8. robots[id].connected=0;
    9. }
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    Where do you call it? By the way, I suggest you make your code less C-ish and more C++-ish, it will get much more cleaner and more readable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    Thank you for the suggestion and i have tried to change a lot of things in my code. i would also be adding the codes and the errors...

    the cpp file...

    Qt Code:
    1. #include "epucksercomm.h"
    2.  
    3.  
    4. epuckSercomm::epuckSercomm()
    5. {
    6.  
    7. }
    8. int epuckSercomm::initRobots(struct robot *rbt)
    9. {
    10. int id;
    11. for(id=0;id<7;id++)
    12. {
    13. rbt[id].serialConnection=id;
    14. rbt[id].port=-1;
    15. rbt[id].connected=0;
    16. }
    17. return 0;
    18. }
    19. int epuckSercomm::connectRobot(struct robot *rbt, int comPort)
    20. {
    21.  
    22.  
    23.  
    24. char portname[20];
    25. //sprintf(portname,"COM%d",comPort);
    26. sprintf(portname,"\\\\.\\COM%d",comPort);
    27. hSerialPort[rbt->serialConnection] = CreateFileA(portname,
    28. GENERIC_READ | GENERIC_WRITE,
    29. 0,
    30. 0,
    31. OPEN_EXISTING,
    32. FILE_ATTRIBUTE_NORMAL,
    33. 0);
    34. if (hSerialPort[rbt->serialConnection] == INVALID_HANDLE_VALUE) return -1;
    35.  
    36.  
    37.  
    38. dcbSerialParams.dcb.DCBlength=sizeof(dcbSerialParams);
    39. if (!GetCommState(hSerialPort[rbt->serialConnection], &dcbSerialParams.dcb)) return -2;
    40.  
    41. dcbSerialParams.dcb.BaudRate = 115200;
    42. dcbSerialParams.dcb.ByteSize = 8;
    43. dcbSerialParams.dcb.StopBits = ONESTOPBIT;
    44. dcbSerialParams.dcb.Parity = NOPARITY;
    45.  
    46. comt.ReadIntervalTimeout = 500;
    47. SetCommTimeouts( hSerialPort[rbt->serialConnection], &comt);
    48. if (!SetCommState(hSerialPort[rbt->serialConnection], &dcbSerialParams.dcb)) return -3;
    49.  
    50. rbt->port=comPort;
    51. rbt->connected=1;
    52.  
    53.  
    54.  
    55. return 0;
    56. }
    57.  
    58.  
    59. int epuckSercomm::setRobotSpeed(struct robot *rbt, int speedL, int speedR)
    60. {
    61.  
    62.  
    63.  
    64. DWORD dwBytesReadWrite = 0;
    65. char command[5];
    66.  
    67. command[0]='D';
    68. command[1]=speedL & 0xff;
    69. command[2]=speedL >> 8;
    70. command[3]=speedR & 0xff;
    71. command[4]=speedR >>8;
    72.  
    73. if (!WriteFile(hSerialPort[rbt->serialConnection],command,5,&dwBytesReadWrite,NULL)) return -1;
    74.  
    75.  
    76.  
    77. return 0;
    78.  
    79.  
    80. }
    81. int epuckSercomm::disconnectRobot(struct robot *rbt)
    82. {
    83.  
    84. CloseHandle(hSerialPort[rbt->serialConnection]);
    85. rbt->connected=0;
    86.  
    87.  
    88. return 0;
    89. }
    90. int epuckSercomm::printRobotSensors(struct robot *rbt)
    91. {
    92. printf("Robot no:%d\r\n",rbt->serialConnection);
    93.  
    94. printf("IR: %d %d %d %d %d %d %d %d\r\n",
    95. rbt->IR[0],rbt->IR[1],rbt->IR[2],rbt->IR[3],
    96. rbt->IR[4],rbt->IR[5],rbt->IR[6],rbt->IR[7]);
    97.  
    98. printf("LI: %d %d %d %d %d %d %d %d\r\n",
    99. rbt->LI[0],rbt->LI[1],rbt->LI[2],rbt->LI[3],
    100. rbt->LI[4],rbt->LI[5],rbt->LI[6],rbt->LI[7]);
    101.  
    102. printf("FL: %d %d %d\r\n",
    103. rbt->FL[0],rbt->FL[1],rbt->FL[2]);
    104.  
    105. printf("MC: %d %d %d\r\n",
    106. rbt->MC[0],rbt->MC[1],rbt->MC[2]);
    107.  
    108. return 0;
    109.  
    110. }
    111. int epuckSercomm::setRobotLed(struct robot *rbt,int noLed, int value)
    112. {
    113.  
    114.  
    115. DWORD dwBytesReadWrite = 0;
    116. char command[3];
    117. if (value<0)value=0; //on
    118. if (value>1)value=1; //off
    119.  
    120. command[0]='L';
    121. command[1]= noLed; //number of led to switch
    122. command[2]= value; //0: off, 1: on
    123.  
    124. if (!WriteFile(hSerialPort[rbt->serialConnection],command,3,&dwBytesReadWrite,NULL))
    125. return -1;
    126.  
    127.  
    128. return 0;
    129.  
    130.  
    131. }
    To copy to clipboard, switch view to plain text mode 



    .h FILE

    Qt Code:
    1. #ifndef EPUCKSERCOMM_H
    2. #define EPUCKSERCOMM_H
    3.  
    4. #include <QObject>
    5. #include <stdio.h>
    6. #include "mode.h"
    7. #include "windows.h"
    8. #include "time.h"
    9. #include "defs.h"
    10.  
    11. class epuckSercomm : public QObject
    12. {
    13. public:
    14. epuckSercomm();
    15. int setRobotLed(struct robot *rbt,int noLed, int value);
    16. int printRobotSensors(struct robot *rbt);
    17. int disconnectRobot(struct robot *rbt);
    18. int setRobotSpeed(struct robot *rbt, int speedL, int speedR);
    19. int connectRobot(struct robot *rbt, int comPort);
    20. int initRobots(struct robot *rbt);
    21.  
    22. //struct robot robots[7];
    23. HANDLE hSerialPort[7]; //up to 7 connection
    24. COMMCONFIG dcbSerialParams;
    25. COMMTIMEOUTS comt;
    26. // fine
    27. //stat
    28. clock_t startTime;
    29. clock_t endTime;
    30.  
    31. int dtime;
    32. };
    33.  
    34.  
    35. #endif // EPUCKSERCOMM_H
    To copy to clipboard, switch view to plain text mode 


    and the initialization...

    in the main UI program is


    Qt Code:
    1. epuckSercomm *epuck = new epuckSercomm();
    2. epuck->initRobots(&robots[0]);
    3. epuck->connectRobot(&robots[1], 1036);
    4. epuck->setRobotLed(&robots[1],7,1);
    5. epuck->setRobotLed(&robots[1],7,0);
    To copy to clipboard, switch view to plain text mode 



    i am getting the following as my output error...i think it is something really that i am missing out... could you please point it out...
    image.jpg
    Last edited by wysota; 13th September 2010 at 18:55. Reason: changed [qtclass] to [code]

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

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    Ouch... first of all please preview your posts before you send them. Second of all don't inline large amount of code directly in your posts. Use attachments instead.

    Your error is probably caused by the same file being referenced twice in the project file (or wrong files being there in the first place).

    And really... your code is hardly readable because of all those pointers and dereferencing, why don't you clean it up a bit?

    Qt Code:
    1. class RobotController {
    2. public:
    3. RobotController(int rbtCnt = 7) {
    4. for(int i=0; i<rbtCnt; i++) {
    5. robot r;
    6. r.serialConnection = i;
    7. r.port = -1;
    8. r.connected = false;
    9. m_robots.append(r);
    10. }
    11. }
    12.  
    13. bool connectRobot(int index, int port) {
    14. if(index<0 || index>=m_robots.count()) return;
    15. QString portName = QString("//./COM%1").arg(port);
    16. // seems that you can use QFile and QFileSystemWatcher or QSocketNotifier here but ok...
    17. m_handles[index] = CreateFileA(portName.toAscii().constData(), ...);
    18. if(!m_handles[index]) {
    19. m_handles.remove(index);
    20. return false;
    21. }
    22. //...
    23. m_robots[index].port = port;
    24. m_robots[index].connected = true; // this is not needed, robot is connected if there is a handle for it or port != -1
    25. return true;
    26. }
    27.  
    28. void disconnectRobot(int index) {
    29. if(!m_robots.contains(index)) return; // not connected
    30. CloseFile(m_handles.value(index));
    31. m_handles.remove(index);
    32. m_robots[index].port = -1;
    33. m_robots[index].connected = false; // again, not needed
    34. }
    35. private:
    36. QList<robot> m_robots; // clean up "robot" class too
    37. QMap<int, HANDLE> m_handles;
    38. };
    To copy to clipboard, switch view to plain text mode 

    and so on...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    IT is true that many of the Classes in the robots and the other things needs cleaning up but they are the ones needed in my project for robot control... any how... the point is even after making all the changes you have suggested i am still getting the same error at the line 17 of the code shown above.. the HANDLE gives out a error always and leads to improper shutting down of the whole program.. While debugging it I have come across that the HANDLE returns a INVALID_HANDLE_VALUE always leading the program to shutdown improperly... is there any way i could use something for the CreateFileA instead and get the HANDLE working....

    Rajesh...

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

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    The handle is not the problem. The problem is you are indexing the array improperly. Please provide a backtrace/callstack after the crash and we'll see what is going on.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    medampudi (14th September 2010)

  14. #13
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    I am really thankful to you MR Wysota... I think both our codes work.. it was just a problem of referencing of my bluetooth port number... the number 1036 i gave a couple of posts ago was the pass code kind of number for the robot which i was passing in vain ..... I am really sorry about this grave but silly blunder... It was actually connected to COM5 so i changed the number to 5 and it works perfectly...

    thank you for bearing up with me..

    I am really sorry once again...

    Rajesh Medampudi.......

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

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    No problem. Just please make your code less C-ish and more object oriented
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    I thought the post 9 had more c++ in it than the previous post ... dont you think it is good... I definitely agree that my code base was just c- ish and changes need to be made to a complete c++ kind .... i will transition my code to c++ from now on... i really like it... And my platform base would be QT for it...

    thank you once again...

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

    Default Re: QT segmentation falut when communicating with a bluetooth function..

    Quote Originally Posted by medampudi View Post
    I thought the post 9 had more c++ in it than the previous post
    Not at all. Wrapping a set of C functions into a class doesn't make the code object oriented.

    ... dont you think it is good...
    Not really. First of all you are exposing pointers to the outside world which is not necessary. Moreover you are also receiving pointers into your own code and operating on them - instead of using external structs you should make those structures proper classes and move all those methods to the class itself.

    For example:

    Qt Code:
    1. class Robot {
    2. public:
    3. Robot() { /* initialization Code goes here */ }
    4. bool connect(int port) { /* epuckSercomm::connectRobot() functionality goes here */ }
    5. bool disconnect() { /* epuckSercomm::disconnectRobot() functionality goes here */ }
    6. void printSensors() { ... }
    7. void setSpeed(int left, int right) { ... }
    8. private:
    9. // ... member variables related to the robot go here
    10. };
    To copy to clipboard, switch view to plain text mode 
    and so on. Then each robot operates on itself:
    Qt Code:
    1. Robot robot(...);
    2. robot.connect(5);
    To copy to clipboard, switch view to plain text mode 
    If you want to manage all robots from a single place then write a RobotManager class (similar to what I wrote a few posts ago). If you have to use pointers for the Robot (which makes some sense here as robots are individuals) then go ahead:
    Qt Code:
    1. Robot *robot = new Robot(...);
    2. robot->connect(5);
    To copy to clipboard, switch view to plain text mode 

    Finally, don't expose your internal variables to the outside world. If you need to access them, add proper methods for your class to get and set their values. There you can sanitize the values passed so that you are sure nothing will break your object too easily (i.e. you could verify that 'port' value passed to Robot::connect() is not negative).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. The following user says thank you to wysota for this useful post:

    medampudi (14th September 2010)

Similar Threads

  1. Communicating with a USB device in Qt?
    By N3wb in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2011, 11:30
  2. Qt communicating with USB device
    By jimmydean101 in forum Newbie
    Replies: 6
    Last Post: 2nd March 2010, 18:02
  3. communicating between threads
    By freekill in forum Newbie
    Replies: 2
    Last Post: 27th November 2009, 09:13
  4. Problems communicating with external editor
    By titaniumdecoy in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 21:12
  5. Bluetooth app?
    By danoh in forum Newbie
    Replies: 2
    Last Post: 30th August 2009, 23:48

Tags for this Thread

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.