|
// 下面的在arm上是调通的,参考一下:
#include "stdafx.h"
#include "CommApp.h"
#include "CommAppDlg.h"
#include "Comm.h"
#include
#include
#include
extern HANDLE hSer;
extern HANDLE g_hevWriteEnable;
//int nLineCount = 0;
extern CEdit *pm_Edit;
extern CEdit *pm_Send;
extern CEdit *pm_Status;
//////////////////////////////////////////////////////////////////////////////////
void CommInit(CString IndexText,unsigned long Baudrate)
{
DCB dcb; //串口结构
DWORD x1; //unsigned long (4*8)
unsigned long x;
unsigned long *y;
x= GENERIC_READ | GENERIC_WRITE;
hSer = CreateFile( IndexText,x, 0,NULL,OPEN_EXISTING, 0, NULL );
if( hSer == INVALID_HANDLE_VALUE )
{
pm_Status->SetWindowText(TEXT("Cannot Open COM!"));
return;
}
pm_Status->SetWindowText(TEXT("COM Opened!"));
//set the DCB structure
dcb.DCBlength = sizeof( DCB );
GetCommState( hSer, &dcb );
dcb.fParity = FALSE;
dcb.fNull = FALSE;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.BaudRate = Baudrate;
dcb.ByteSize = 8;
// BOOL bSetCommStat =
SetCommState( hSer, &dcb );
//Set the timeout
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 10;
CommTimeOuts.ReadTotalTimeoutConstant = 10;
CommTimeOuts.WriteTotalTimeoutMultiplier = 50;
CommTimeOuts.WriteTotalTimeoutConstant = 100;
SetCommTimeouts( hSer, &CommTimeOuts );
return;
}
//Rcv Thread,Read the ComBuffer
void CommRcv(void)
{
BYTE ReadBuffer[2000] = {0};
// BOOL bReadStatus = FALSE;
DWORD dwErrorFlags;
COMSTAT ComStat;
DWORD i;
DWORD dwBytesRead = 0;
TCHAR TReadBuffer[2000] = {0};
SetCommMask(hSer,EV_RXCHAR);
while(1)
{
WaitCommEvent(hSer,NULL,NULL);
SetCommMask(hSer,EV_RXCHAR);
ClearCommError( hSer, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue )continue;
// dwBytesToRead = ComStat.cbInQue;
if( !ReadFile( hSer,ReadBuffer, ComStat.cbInQue, &dwBytesRead, NULL) )
{
pm_Status -> SetWindowText(TEXT("Read COMM Failed!"));
return;
}
else
{
pm_Status -> SetWindowText(TEXT("Read COM Success!"));
MultiByteToWideChar(CP_ACP,0,(char *)ReadBuffer,dwBytesRead,TReadBuffer,dwBytesRead);
if(TReadBuffer[0])lstrcat(TReadBuffer,TEXT("\r\n"));
// nLineCount = ;
if( (pm_Edit->GetLineCount()) > 40)
{
//Clear the Output
pm_Edit -> SetSel(0,-1);
pm_Edit -> Clear();
}
//Now output
int nLen = pm_Edit->GetWindowTextLength();
pm_Edit -> SetFocus();
pm_Edit -> SetSel(nLen, nLen);
pm_Edit -> ReplaceSel(TReadBuffer);
//Clear the Buffer
for(i=0;i < dwBytesRead +2;i++)
{
ReadBuffer = 0;
TReadBuffer = 0;
}
}
}
}
//Trans Thread
void CommTrans(void)
{
DWORD dwBytes;
char WBuf[100] = {0};
TCHAR WriteBuffer[100] = {0};
while(1)
{
if(WaitForSingleObject(g_hevWriteEnable,INFINITE) == WAIT_OBJECT_0)
{
//Write to Comm
pm_Send -> GetWindowText(WriteBuffer,100);
lstrcat(WriteBuffer,TEXT("\r\n"));
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,WriteBuffer,lstrlen(WriteBuffer),WBuf,lstrlen(WriteBuffer),NULL,NULL);
if(WriteFile(hSer, (TCHAR *)WBuf,lstrlen(WriteBuffer)*sizeof(char),&dwBytes, NULL))
pm_Status -> SetWindowText(TEXT("Write COM Success!"));
else {
DWORD i = GetLastError();
pm_Status -> SetWindowText(TEXT("Write COM Failed!"));
}
}
else break;
}
return;
}
//----------------------- 打开串口命令 -----------
void CCOMMAppDlg::OnButton2()
{
// TODO: Add your control notification handler code here
if(!g_bButton1State)
{
g_bButton1State = TRUE;
//Change the Button Caption
m_ButtonState.SetWindowText(TEXT("Close"));
//Init COM
CommInit(CommText,CommBaudRate);
//Create a thread
if(!GetExitCodeThread(hRcvThread, &dwTStat)||!GetExitCodeThread(hXmitThread, &dwTStat)||
(dwTStat != STILL_ACTIVE))
{
hRcvThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)CommRcv,NULL,0,&dwTStat);
if(hRcvThread)CloseHandle(hRcvThread);
hXmitThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)CommTrans,NULL,0,&dwTStat);
if(hXmitThread)CloseHandle(hXmitThread);
}
g_hevWriteEnable = CreateEvent(NULL,FALSE,FALSE,NULL);
// g_hevReadEnable = CreateEvent(NULL,FALSE,FALSE,NULL);
}
else
{
g_bButton1State = FALSE;
//Change the caption
m_ButtonState.SetWindowText(TEXT("Open"));
pm_Status->SetWindowText(TEXT("COM Closed!"));
GetExitCodeThread(hRcvThread, &dwTStat);
TerminateThread(hRcvThread,dwTStat);
GetExitCodeThread(hXmitThread, &dwTStat);
TerminateThread(hXmitThread,dwTStat);
if(hSer != NULL)
{
CloseHandle(hSer);
hSer = NULL;
}
}
} |
|