做过智能车摄像头的都知道,我们在做车的过程中如果光是看那些单片机传回来的01字符串的图像数据的话,那可是一件相关无聊的事情。不仅如此,看这样的数据还很废眼睛的。这样的话看没过多眼睛就开始累了,就开始不想干活了。
然后再附上原代码:
#include <iostream>
#include <windows.h>
#include <winbase.h>
#include <conio.h>
#include <cstdio>
#include <cstdlib>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <cxcore.h>
using namespace std;
HANDLE hComDev;
OVERLAPPED m_ov;
COMSTAT comstat;
DWORD m_dwCommEvents;
bool bRead = true;
bool bResult = true;
DWORD dwError = 0;
DWORD bytesRead = 0;
/**打开串口设备**/
bool openPort(char *portname);
/**设置串口设备DCB参数**/
bool setupDcb(int rate_arg);
/**设置串口设备超时限制参数**/
bool setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,
DWORD ReadTotalconstant,DWORD WriteTotalMultiplier,
DWORD WriteTotalconstant);
/**串口收发数据**/
void ReceiveChar(unsigned char *RXBuffer);
bool WriteChar(char* m_szWriteBuffer,DWORD m_nToSend);
void createImage(void);
int main(int argc,char**argv)
{
bool openFlag = false;
openFlag = openPort((char*)"COM5");
if(openFlag)
{
printf("打开串口成功!\n");
if(setupDcb(115200))
{
printf("波特率设置成功\n");
}
if(setuptimeout(0, 0, 0, 0, 0))
{
printf("超时限制设置成功\n");
SetCommMask(hComDev,EV_RXCHAR);//当有字符在 inbuf 中时产生这个事件
//清除串口的所有操作
PurgeComm(hComDev,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT);
//WriteChar((char*)"please send data now",20);
printf("received data:\n");
createImage();
CloseHandle(hComDev);
}
}
return 0;
}
/**打开串口设备**/
bool openPort(char *portname)
{
hComDev = CreateFile(portname, //串口号
GENERIC_READ|GENERIC_WRITE,//允许读写
0,//通讯设备以独占方式打开
0,//无安全属性
OPEN_EXISTING,//通讯设备已存在
FILE_FLAG_OVERLAPPED,//异步I/O
0);//通讯设备不能用模板打开
if(hComDev == INVALID_HANDLE_VALUE)
{
CloseHandle(hComDev);
printf("打开串口失败!\n");
return false;
}
else
{
return true;
}
}
bool setupDcb(int rate_arg)
{
DCB dcb;
//int rate = rate_arg;
memset(&dcb, 0, sizeof(dcb));
if(!GetCommState(hComDev, &dcb))
{
return false;
}
else
{
// set DCB to configure the serial port
//波特率为115200,无奇偶校验,8位数据位,1位停止位
//BuildCommDCB("115200,N,8,1",&dcb);
dcb.DCBlength = sizeof(dcb);
// ---------- Serial Port Config -------
dcb.BaudRate = rate_arg;//波特率
dcb.Parity = NOPARITY;//奇偶校验
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;//1位停止位
dcb.ByteSize = 8;//8位数据位
dcb.fOutxCtsFlow = 0;
dcb.fOutxDsrFlow = 0;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = 0;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutX = 0;
dcb.fInX = 0;
//---------- misc parameters -----
dcb.fErrorChar = 0;
dcb.fBinary = 1;
dcb.fNull = 0;
dcb.fAbortOnError = 0;
dcb.wReserved = 0;
dcb.XonLim = 2;
dcb.XoffLim = 4;
dcb.XonChar = 0x11;
dcb.XoffChar = 0x13;
dcb.EvtChar = 0;
// set DCB
if(!SetCommState(hComDev,&dcb))
{
return false;
}
else
{
return true;
}
}
}
bool setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,DWORD
ReadTotalconstant,DWORD WriteTotalMultiplier,
DWORD WriteTotalconstant)
{
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout=ReadInterval;
timeouts.ReadTotalTimeoutConstant=ReadTotalconstant;
timeouts.ReadTotalTimeoutMultiplier=ReadTotalMultiplier;
timeouts.WriteTotalTimeoutConstant=WriteTotalconstant;
timeouts.WriteTotalTimeoutMultiplier=WriteTotalMultiplier;
if(!SetCommTimeouts(hComDev, &timeouts))
return false;
else
return true;
}
void ReceiveChar( unsigned char *RXBuffer)
{
if(bRead)
{
bResult = ReadFile(hComDev,//Handle to Comm port
RXBuffer,//RX Buffer Pointer
1, //Read one byte
&bytesRead,//Stores number of bytes read
&m_ov); //pointer to the m_ov structure
if(!bResult)
{
switch(dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
bRead = false;
break;
}
default:
{
break;
}
}
}
else
{
bRead = true;
}
}
if(!bRead)
{
bRead = true;
bResult = GetOverlappedResult(hComDev,//Handle to Comm port
&m_ov,//Overlapped structure
&bytesRead,//Stores number of bytes read
true);// Wait flag
}
}
void createImage(void)
{
IplImage *src = NULL;
IplImage *dst = NULL;
int height,width,step,channels;
int i,j,k;
unsigned char *imgdata = NULL;
unsigned char data;
src = cvCreateImage(cvSize(145,48),IPL_DEPTH_8U, 1);
dst = cvCreateImage(cvSize(145 * 5,48 * 5),IPL_DEPTH_8U, 1);
height = src->height;
width = src->width;
step = src->widthStep;
channels = src->nChannels;
imgdata = (unsigned char *)src->imageData;
bRead = true;
bResult = true;
dwError = 0;
bytesRead = 0;
for(;;)
{
bResult = ClearCommError(hComDev,&dwError, &comstat);
if (comstat.cbInQue == 0)
{
continue;
}
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
for(k=0; k<channels; k++)
{
ReceiveChar(&data);
if(data == '1')
{
imgdata[i*step+j*channels+k]=255;
}
else
{
imgdata[i*step+j*channels+k]=0;
}
}
}
}
cvResize(src, dst, CV_INTER_LINEAR);
cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
cvShowImage("src", dst);
if(cvWaitKey(1) == 27)
{
cvDestroyWindow ("src");
cvReleaseImage(&src);
cvReleaseImage(&dst);
return;
}
}
}
bool WriteChar(char* m_szWriteBuffer,DWORD m_nToSend)
{
BOOL bWrite = TRUE;
BOOL bResult = TRUE;
DWORD BytesSent = 0;
//HANDLE m_hWriteEvent;
//ResetEvent(m_hWriteEvent);
if (bWrite)
{
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;
// Clear buffer
bResult = WriteFile(hComDev, // Handle to COMM Port
m_szWriteBuffer, // Pointer to message buffer in calling finction
m_nToSend, // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&m_ov ); // Overlapped structure
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
// continue to GetOverlappedResults()
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
// all other error codes
break;
}
}
}
} // end if(bWrite)
if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(hComDev, // Handle to COMM port
&m_ov, // Overlapped structure
&BytesSent, // Stores number of bytes sent
TRUE); // Wait flag
// deal with the error code
if (!bResult)
{
printf("GetOverlappedResults() in WriteFile()");
}
} // end if (!bWrite)
// Verify that the data size send equals what we tried to send
if (BytesSent != m_nToSend)
{
printf("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n",
(int)BytesSent, (int)strlen((char*)m_szWriteBuffer));
}
return true;
}