Results 1 to 2 of 2

Thread: Synchronize serial data read from Rfcomm in Qt

  1. #1
    Join Date
    Jan 2017
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Synchronize serial data read from Rfcomm in Qt

    I have created a producer-consumer thread system to read data from a serial port /dev/rfcomm0. The producer is calling the serial read function and storing data in a circular buffer. The serial read fucntion also takes care to parse the data and return it in a readable format, which stores it in the circular buffer. The consumer accesses this data from the buffer and calls slots to update a graph.

    The data being read from the serial buffer is correct as long as i run the code in debug mode. The breakpoint is set after the serial read function call. But if I execute the program in normal mode, only the first data is read correctly, but subsequent data is garbled. The read buffer has no readable data in it. I do not understand why the serial buffer is not being read correctly.

    I'm using Qt 5.3.2 with Qt creator 3.2.1 on Raspberry Pi 3.

    My serial read code is as follows:

    Qt Code:
    1. int serialReadData (int fd, char * outStr)
    2. {
    3. char data[23];
    4. bool flag = true;
    5. bool valid_data = false;
    6. int count = 0;
    7. int i = 0;
    8. int n = read(fd,&data,23);
    9.  
    10. if((n == 23) && (data[0] == 's') && (data[1] == 't'))
    11. {
    12. for(int i = 0; i < 21; i++)
    13. {
    14. if((data[i+3] != 'e') && (data[i+4] != 'n'))
    15. {
    16. outStr[i] = data[i+3];
    17. }
    18. else
    19. {
    20. outStr[i] = '\n';
    21. break;
    22. }
    23. }
    24. //serialFlush(fd);
    25. return 1;
    26. }
    27. return 0;
    28. }
    To copy to clipboard, switch view to plain text mode 

    My Producer thread is as follows:

    Qt Code:
    1. void rxDatafromBT::run()
    2. {
    3. char readData[DataSize];
    4. int dataCount = 0;
    5.  
    6. /*Open serial port connection to read data from /dev/rfcomm0*/
    7. int fd = serialOpen("/dev/rfcomm0",9600);
    8.  
    9. /*If serial open not successful, report error and exit the thread*/
    10. if(fd == -1)
    11. {
    12. qDebug("Error Opening Serial Port to /dev/rfcomm \n");
    13. emit errorOpeningPort();
    14. }
    15. /*If successful, then enter into while(1) loop*/
    16. else
    17. {
    18. /*Run this Forever-Until program is terminated*/
    19. while(!this->isInterruptionRequested()) {
    20. /*Acquire data here from serial port*/
    21.  
    22. /*If first byte of data is the correct validity byte
    23.   Copy the data to data buffer, else ignore data*/
    24. //if((readData != NULL) && (readData[0] == 0x5A))
    25.  
    26. if(serialReadData(fd,readData))
    27. {
    28. freeBytes.acquire();
    29. for(int i = 0; i < DataSize; i++)
    30. {
    31. if(*(readData + i) == '\n')
    32. {
    33. break;
    34. }
    35. buffer[dataCount][i] = *(readData + i);
    36. }
    37.  
    38. usedBytes.release();
    39.  
    40. dataCount++;
    41. if(dataCount>=BufferSize)
    42. {
    43. dataCount = 0;
    44. }
    45. }
    46.  
    47. }
    48.  
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 

    Will be gald to accept any help/suggestion to resolve this issue.

    Best Regards,
    Ksir
    Last edited by Ksir; 27th January 2017 at 15:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Synchronize serial data read from Rfcomm in Qt

    How do you transport the data to the consumer thread?
    Is the data structure properly protected against concurrent access?

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 4th February 2016, 11:48
  2. Serial read misses to read data from the serial port
    By mania in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 18th August 2014, 09:49
  3. Replies: 10
    Last Post: 27th January 2014, 07:32
  4. read data from serial port and display it
    By vanduongbk in forum Newbie
    Replies: 1
    Last Post: 30th June 2013, 04:46
  5. read data from serial port
    By amitpatel22 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 20th July 2011, 18:11

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.