|
发表于 2013-10-17 08:56:03
|
显示全部楼层
// my_test_comDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Test6410.h"
#include "my_test_comDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define dim(x) (sizeof(x)/sizeof(x[0]))
// Cmy_test_comDlg 对话框
int jishu =0;
int flag=0;
IMPLEMENT_DYNAMIC(Cmy_test_comDlg, CDialog)
Cmy_test_comDlg::Cmy_test_comDlg(CWnd* pParent /*=NULL*/)
: CDialog(Cmy_test_comDlg::IDD, pParent)
, m_strRecv1(_T(""))
, m_strRecv2(_T(""))
, m_strRecv3(_T(""))
, m_stredit1(_T("abcdefgh1234567890ABCDEFGH"))
, m_stredit2(_T("abcdefgh1234567890ABCDEFGH"))
, m_stredit3(_T("abcdefgh1234567890ABCDEFGH"))
, tongguo1(_T(""))
, tongguo2(_T(""))
, tongguo3(_T(""))
{
}
Cmy_test_comDlg::~Cmy_test_comDlg()
{
}
void Cmy_test_comDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_RECV1, m_strRecv1);
DDX_Text(pDX, IDC_RECV2, m_strRecv2);
DDX_Text(pDX, IDC_RECV3, m_strRecv3);
DDX_Text(pDX, IDC_SEND1, m_stredit1);
DDX_Text(pDX, IDC_SEND2, m_stredit2);
DDX_Text(pDX, IDC_SEND3, m_stredit3);
DDX_Text(pDX, IDC_RECV_COUNT1, tongguo1);
DDX_Text(pDX, IDC_RECV_COUNT2, tongguo2);
DDX_Text(pDX, IDC_RECV_COUNT3, tongguo3);
}
BEGIN_MESSAGE_MAP(Cmy_test_comDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
ON_WM_TIMER()
ON_BN_CLICKED(IDOK, &Cmy_test_comDlg::OnBnClickedOk)
ON_BN_CLICKED(IDCANCEL, &Cmy_test_comDlg::OnBnClickedCancel)
ON_EN_CHANGE(IDC_RECV1, &Cmy_test_comDlg::OnEnChangeRecv1)
ON_EN_CHANGE(IDC_RECV3, &Cmy_test_comDlg::OnEnChangeRecv3)
END_MESSAGE_MAP()
// Cmy_test_comDlg 消息处理程序
BOOL Cmy_test_comDlg::OnInitDialog()
{
CDialog::OnInitDialog();
hComPort=INVALID_HANDLE_VALUE;//-1
TCHAR STRCOM[20];
for(int i=1;i<10;i++)
{
_stprintf_s(STRCOM,10,_T("COM%d"),i);
}
// TODO: 在此添加额外的初始化代码
//InitCommunication();
InitCom();
SetTimer(100,1000,NULL);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void Cmy_test_comDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_MY_TEST_COM_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_MY_TEST_COM_DIALOG));
}
}
#endif
void Cmy_test_comDlg::InitCom(void)
{
int i;
for(i=2;i<5;i++)
{
hhComPort[i-2]=InitCommunication(i);
}
}
HANDLE Cmy_test_comDlg::InitCommunication(int comid)
{
int btl=115200;
int jy=NOPARITY;
BSEND=FALSE;
DCB dcb;
//TCHAR szDbg[128];
COMMTIMEOUTS cto;
OVERLAPPED OverLapWrt;
HANDLE hLocal=INVALID_HANDLE_VALUE;
// DWORD dwTStat;
hComPort = INVALID_HANDLE_VALUE;
memset(&OverLapWrt,0,sizeof(OVERLAPPED));
if (hComPort != INVALID_HANDLE_VALUE)
{
CloseHandle (hComPort);
hComPort = INVALID_HANDLE_VALUE;
}
TCHAR STRCOM[20]; DWORD derrcode=0xff;
_stprintf_s(STRCOM,10,_T("COM%d:"),comid);
hLocal = CreateFile(STRCOM, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
derrcode=GetLastError();
if (hLocal != INVALID_HANDLE_VALUE)
{
// 配置串口
try
{
//PurgeComm( hLocal, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
dcb.DCBlength = sizeof (dcb);
GetCommState (hLocal, &dcb);
dcb.BaudRate = btl;
dcb.fParity = FALSE;
dcb.fNull = FALSE;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = jy;
dcb.ByteSize = 8;
SetCommState (hLocal, &dcb);
SetCommMask(hLocal, EV_RXCHAR ) ;
SetupComm(hLocal, 4096, 4096 ) ;
PurgeComm(hLocal, PURGE_TXABORT | PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR );
// 设定超时
GetCommTimeouts (hLocal, &cto);
cto.ReadIntervalTimeout = 0;
cto.ReadTotalTimeoutMultiplier = 0;
cto.ReadTotalTimeoutConstant = 0;
cto.WriteTotalTimeoutMultiplier = 0;
cto.WriteTotalTimeoutConstant = 0;
SetCommTimeouts (hLocal, &cto);
hComPort=hLocal;
m_strRecv1=_T("");
//m_stredit=_T("");
//m_recv1=0;
//m_send1=0;
//_stprintf_s(STRCOM,10,_T("COM%d打开成功"),comid);
//MessageBox(_T("COM打开成功"));
//MessageBox(STRCOM);
}catch(...)
{
//MessageBox(_T("打开失败"));
_stprintf_s(STRCOM,10,_T("COM%d打开失败"),comid);
MessageBox(STRCOM);
hComPort = INVALID_HANDLE_VALUE;
}
}
else
{
//MessageBox(_T("打开失败"));
_stprintf_s(STRCOM,10,_T("COM%d打开失败"),comid);
MessageBox(STRCOM);
hComPort = INVALID_HANDLE_VALUE;
}
if(hComPort == INVALID_HANDLE_VALUE)
{
}
return hComPort;
}
/*void Cmy_test_comDlg::OnBnClickedUartClose()
{
// TODO: 在此添加控件通知处理程序代码
if (hComPort != INVALID_HANDLE_VALUE)
CloseHandle (hComPort);
hComPort = INVALID_HANDLE_VALUE;
}*/
/*void Cmy_test_comDlg::OnBnClickedUartAut()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
}
*/
void Cmy_test_comDlg::ReadFun(int count)
{
//HWND hWnd;
DWORD cBytes=0, i;
WCHAR szText[100];
char szAnsi[100];
char szAnsi0[100];
WCHAR szAnsi1[100];
size_t siz;
COMSTAT Stat;
DWORD dwErr = 0;
int recvnum;
int u;
for(u=0;u<m_stredit1.GetLength();u++)
szAnsi1[u]=m_stredit1.GetAt(u);
wcstombs_s(&siz, szAnsi0, u+1, szAnsi1, _TRUNCATE);
if(count==0)
{
ClearCommError (hhComPort[count], &dwErr, &Stat);
if(Stat.cbInQue)recvnum=Stat.cbInQue;
else
{
m_strRecv1=_T("\r\nNO DATA");
tongguo1=_T("未通过");
UpdateData(FALSE);
return;
}
ReadFile (hhComPort[count], &szAnsi, recvnum, &cBytes, 0);
//m_recv1+=cBytes;
for(i=0;i<cBytes;i++)
{
if(szAnsi[i]!=szAnsi0[i])
{
flag=1;
}
}
if(flag==1)
{
tongguo1=_T("未通过");
}
else
{
tongguo1=_T("通过");
}
mbstowcs_s (&siz, szText, dim(szText), szAnsi, _TRUNCATE);
if(m_strRecv1.GetLength()>500)
{
m_strRecv1=_T("");
}
for(i=0;i<cBytes;i++)
{
//if((m_strRecv1.GetLength()%25==0)&&(m_strRecv1.GetLength()>0))
//m_strRecv1+=_T("\r\n");
m_strRecv1+=szText[i];
}
//m_strRecv+=szText;
m_strRecv1+=_T("\r\n");
UpdateData(FALSE);
}
if(count==1)
{
ClearCommError (hhComPort[count], &dwErr, &Stat);
if(Stat.cbInQue)recvnum=Stat.cbInQue;
else
{
;
m_strRecv2=_T("\r\nNO DATA");
tongguo2=_T("未通过");
UpdateData(FALSE);
return;
}
ReadFile (hhComPort[count], &szAnsi, recvnum, &cBytes, 0);
for(i=0;i<cBytes;i++)
{
if(szAnsi[i]!=szAnsi0[i])
{
flag=1;
}
}
if(flag==1)
{
tongguo2=_T("未通过");
}
else
{
tongguo2=_T("通过");
}
//m_recv2+=cBytes;
mbstowcs_s (&siz, szText, dim(szText), szAnsi, _TRUNCATE);
if(m_strRecv2.GetLength()>500)
{
m_strRecv2=_T("");
}
for(i=0;i<cBytes;i++)
{
//if((m_strRecv2.GetLength()%25==0)&&(m_strRecv2.GetLength()>0))
//m_strRecv2+=_T("\r\n");
m_strRecv2+=szText[i];
}
//m_strRecv+=szText;
m_strRecv2+=_T("\r\n");
UpdateData(FALSE);
}
if(count==2)
{
ClearCommError (hhComPort[count], &dwErr, &Stat);
if(Stat.cbInQue)recvnum=Stat.cbInQue;
else
{
;
m_strRecv3=_T("\r\nNO DATA");
tongguo3=_T("未通过");
UpdateData(FALSE);
return;
}
ReadFile (hhComPort[count], &szAnsi, recvnum, &cBytes, 0);
for(i=0;i<cBytes;i++)
{
if(szAnsi[i]!=szAnsi0[i])
{
flag=1;
}
}
if(flag==1)
{
tongguo3=_T("未通过");
}
else
{
tongguo3=_T("通过");
}
//m_recv3+=cBytes;
mbstowcs_s (&siz, szText, dim(szText), szAnsi, _TRUNCATE);
if(m_strRecv3.GetLength()>500)
{
m_strRecv3=_T("");
}
for(i=0;i<cBytes;i++)
{
//if((m_strRecv3.GetLength()%25==0)&&(m_strRecv3.GetLength()>0))
//m_strRecv3+=_T("\r\n");
m_strRecv3+=szText[i];
}
//m_strRecv+=szText;
m_strRecv3+=_T("\r\n");
UpdateData(FALSE);
}
//TRACE(_T("\r\nrecv=%s"),szText);
}
void Cmy_test_comDlg::senddata(int count)
{
// TODO: 在此添加控件通知处理程序代码
//HWND hwndSText;
int rc;
DWORD cBytes;
//WCHAR szText[100];
char szAnsi[100];
WCHAR szAnsi1[100];
size_t siz;
COMSTAT Stat;
DWORD dwErr = 0;
OVERLAPPED olWrite;
memset(&olWrite,0,sizeof(olWrite));
olWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if((m_stredit1.GetLength()<1)&&(m_stredit1.GetLength()>100)) return;
int u=0;
for(u=0;u<m_stredit1.GetLength();u++)
szAnsi1[u]=m_stredit1.GetAt(u);
wcstombs_s(&siz, szAnsi, u+1, szAnsi1, _TRUNCATE);
if(count==0)
{
rc = WriteFile(hhComPort[count],&szAnsi,strlen(szAnsi),&cBytes,&olWrite);
}
if(count==1)
{
rc = WriteFile(hhComPort[count],&szAnsi,strlen(szAnsi),&cBytes,&olWrite);
}
if(count==2)
{
rc = WriteFile(hhComPort[count],&szAnsi,strlen(szAnsi),&cBytes,&olWrite);
}
//if(count==0)
//m_send1+=strlen(szAnsi);
//if(count==1)
// m_send2+=strlen(szAnsi);
//if(count==2)
//m_send3+=strlen(szAnsi);
if (!rc)
{
if (ClearCommError (hhComPort[count], &dwErr, &Stat))
{
//TRACE(_T("\r\nClearCommError "));l
//MessageBox(NULL,szText,TEXT("message"),0);
}
}
else
{
//MessageBox(NULL,szText,TEXT("message"),0); }
}
}
void Cmy_test_comDlg::OnTimer(UINT nIDEvent)
{
int j;
if(hComPort==INVALID_HANDLE_VALUE) return;
if(jishu<8)
{
for(j=0;j<6;j++)
senddata(j);
Sleep(20);
}
if(jishu<8)
{
for(j=0;j<6;j++)
ReadFun(j);
Sleep(20);
}
jishu++;
}
void Cmy_test_comDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
for(int i=0;i<3;i++)
{
if(hhComPort[i]!=INVALID_HANDLE_VALUE)
{
CloseHandle(hhComPort[i]);
hhComPort[i]=INVALID_HANDLE_VALUE;
}
}
OnOK();
}
void Cmy_test_comDlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
for(int i=0;i<3;i++)
{
if(hhComPort[i]!=INVALID_HANDLE_VALUE)
{
CloseHandle(hhComPort[i]);
hhComPort[i]=INVALID_HANDLE_VALUE;
}
}
if(hComPort=INVALID_HANDLE_VALUE)
{
CloseHandle(hComPort);
hComPort=INVALID_HANDLE_VALUE;
}
OnCancel();
}
void Cmy_test_comDlg::OnEnChangeRecv1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void Cmy_test_comDlg::OnEnChangeRecv3()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
楼主看看吧 我测试过的 把三个串口的收发短路,可以同时接收数据的! |
|