Results 1 to 6 of 6

Thread: win32 API serial port in Qt4

  1. #1
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question win32 API serial port in Qt4

    Hi Everyone,

    I am a newbie who started creating a GUI for image processing in Qt4 with openCV two months back. I was successful to

    1. Read an image from specific path.
    2. Convert it into Grayscale image.
    3. Converting grayscale image into binary thresholded image.
    4. Finding the contours and drawing the contours.
    5. Labeling the contours.
    6. Feature extracting of the contours with its area and centroid and storing it in a text file.

    Now instead of opening an image from specific path, I need to read an image from serial port. I figured out that there were some libraries for serial port in Qt, instead I implemented with win32 API. I am not certain whether I am right or wrong. Plz someone guide me in this.

    My code for opening an image from specific path:

    Qt Code:
    1. void finalv1::openimage()
    2. {
    3. QString filename=QFileDialog::getOpenFileName(this,tr("Load Image"),".",tr("Image Files(*.png *.jpg *.jpeg *.bmp *.tiff)"));
    4. imageop = cvLoadImage(filename.toAscii().data());
    5. cvNamedWindow("image",1);
    6. cvShowImage("image",imageop);
    7. }
    8.  
    9. void finalv1::on_pushButton_open_clicked()
    10. {
    11. openimage();
    12. }
    To copy to clipboard, switch view to plain text mode 

    and I am processing the image with Iplimage structure.

    My code for reading image from serial port using win32 API:

    Qt Code:
    1. BOOL finalv1::openport(DCB config, const char *port)
    2. {
    3. hcomm=CreateFile((LPCWSTR)port,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
    4.  
    5. if(GetCommState(hcomm,&config)==0)
    6. {
    7. return FALSE;
    8. }
    9.  
    10. config.BaudRate = dcb.BaudRate;
    11. config.ByteSize = dcb.ByteSize;
    12. config.StopBits = dcb.StopBits;
    13. config.Parity = dcb.Parity;
    14.  
    15. if(SetCommState(hcomm,&config)==0)
    16. {
    17. return FALSE;
    18. }
    19.  
    20. return TRUE;
    21. }
    22.  
    23. BOOL finalv1::readchar(unsigned char *inputdata)
    24. {
    25. BOOL read;
    26.  
    27. DWORD noofbytesread=0;
    28. read = ReadFile(hcomm,inputdata,10,&noofbytesread,NULL);
    29.  
    30. if(noofbytesread==0){
    31. return FALSE;
    32. }
    33. inputdata[noofbytesread]='\0';
    34. return TRUE;
    35. }
    To copy to clipboard, switch view to plain text mode 

    I am not getting any errors, but I dont know whether I am doing right and how to declare the 'input data' as IplImage structure.

    Thanx!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: win32 API serial port in Qt4

    Hardly a Qt question: you want to use Win32 API to receive data and send it to OpenCV. Here are some thoughts anyway:

    Line 3 seems unlikely to be correct. LPCWSTR means "long pointer constant wide string" and you are providing a pointer to a const narrow string. A simple C-style cast does not convert the data, it just silences the compiler error. A wide-string literal looks like L"COM1" or you might use the _T("COM1") macro.

    Before you can pass data to OpenCV it needs to be in some format OpenCV will accept, dictated by the sender, and you need to known when the received data is complete. As with network communication you cannot assume the file is received all at once, and readable in one hit, even if it was sent in one write. You must therefore buffer the received data until such time as the data is complete, which generally means you need some way to know the length of the image data.

    Line 33 does not make a lot of sense if the data is a binary (image) and not a nul-terminated text string.

  3. The following user says thank you to ChrisW67 for this useful post:

    jakr13 (15th February 2013)

  4. #3
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: win32 API serial port in Qt4

    Hi chris,

    I changed it according to what you said.

    Qt Code:
    1. BOOL finalv1::readchar(char *inputdata, const unsigned int &buffer, unsigned long &length)
    2. {
    3. BOOL read;
    4. read = ReadFile(hcomm,inputdata,buffer,&length,NULL);
    5.  
    6. if(read==0)
    7. {
    8. return FALSE;
    9. }
    10. if(length> 0)
    11. {
    12. inputdata[length]=NULL;
    13. return TRUE;
    14. }
    15. return TRUE;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Is this right?

    Thanx!
    Last edited by jakr13; 15th February 2013 at 05:08.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: win32 API serial port in Qt4

    Because it is a picture so it is binary data. This means that the value of 0x00 is a valid value. You can not value 0x00 (or NULL) as a marker of the end of 0x00 data in method finalv1::readchar.

  6. The following user says thank you to Lesiok for this useful post:

    jakr13 (15th February 2013)

  7. #5
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: win32 API serial port in Qt4

    can you give me some suggestion to make it right?

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: win32 API serial port in Qt4

    COM port is just a normal cable. In the case of binary data you must have defined a protocol that transmits data in packets of fixed structures. Packet might look like this: byte "start package", byte "data length", the data bytes, CRC, byte "end of data". Read about the ISO multi-layer transmition model.

Similar Threads

  1. Serial port performance
    By marcvanriet in forum General Programming
    Replies: 1
    Last Post: 2nd January 2012, 12:40
  2. serial port communication
    By robotics in forum Qt Programming
    Replies: 19
    Last Post: 28th September 2011, 15:11
  3. Serial Port communication
    By mgurbuz in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2011, 02:38
  4. data from serial port
    By bhe in forum Newbie
    Replies: 4
    Last Post: 3rd May 2009, 10:19
  5. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 02:05

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.