做過智能車攝像頭的都知道,我們在做車的過程中如果光是看那些單片機傳回來的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;
}