PDA

View Full Version : win32 API serial port in Qt4



jakr13
15th February 2013, 03:27
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:



void finalv1::openimage()
{
QString filename=QFileDialog::getOpenFileName(this,tr("Load Image"),".",tr("Image Files(*.png *.jpg *.jpeg *.bmp *.tiff)"));
imageop = cvLoadImage(filename.toAscii().data());
cvNamedWindow("image",1);
cvShowImage("image",imageop);
}

void finalv1::on_pushButton_open_clicked()
{
openimage();
}


and I am processing the image with Iplimage structure.

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



BOOL finalv1::openport(DCB config, const char *port)
{
hcomm=CreateFile((LPCWSTR)port,GENERIC_READ|GENERI C_WRITE,0,NULL,OPEN_EXISTING,0,NULL);

if(GetCommState(hcomm,&config)==0)
{
return FALSE;
}

config.BaudRate = dcb.BaudRate;
config.ByteSize = dcb.ByteSize;
config.StopBits = dcb.StopBits;
config.Parity = dcb.Parity;

if(SetCommState(hcomm,&config)==0)
{
return FALSE;
}

return TRUE;
}

BOOL finalv1::readchar(unsigned char *inputdata)
{
BOOL read;

DWORD noofbytesread=0;
read = ReadFile(hcomm,inputdata,10,&noofbytesread,NULL);

if(noofbytesread==0){
return FALSE;
}
inputdata[noofbytesread]='\0';
return TRUE;
}


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!

ChrisW67
15th February 2013, 03:59
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.

jakr13
15th February 2013, 04:38
Hi chris,

I changed it according to what you said.



BOOL finalv1::readchar(char *inputdata, const unsigned int &buffer, unsigned long &length)
{
BOOL read;
read = ReadFile(hcomm,inputdata,buffer,&length,NULL);

if(read==0)
{
return FALSE;
}
if(length> 0)
{
inputdata[length]=NULL;
return TRUE;
}
return TRUE;
}


Is this right?

Thanx!

Lesiok
15th February 2013, 06:37
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.

jakr13
15th February 2013, 07:49
can you give me some suggestion to make it right?

Lesiok
15th February 2013, 09:33
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.