📄 sercomnewview1.cpp
字号:
// SerComNewView1.cpp : implementation of the CSerComNewView1 class
//
#include <string.h>
#include <stdio.h>
#include "stdafx.h"
#include "SerComNew.h"
#include "SerialPort.h"
#include "ConfigDlg.h"
#include "SerComNewDoc.h"
#include "SerComNewView1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSerComNewView1
IMPLEMENT_DYNCREATE(CSerComNewView1, CFormView)
BEGIN_MESSAGE_MAP(CSerComNewView1, CFormView)
//{{AFX_MSG_MAP(CSerComNewView1)
ON_BN_CLICKED(IDC_BTN_CONFIGURE, OnBtnConfigure)
ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BTN_CLEAR, OnBtnClear)
ON_BN_CLICKED(IDC_BUTTON_FILESNED, OnBtnFileSend)
ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclose)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
ON_MESSAGE(WM_COMM_RXCHAR,OnReceiveData)
ON_MESSAGE(WM_COMM_ERR_DETECTED,OnErrHappen)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerComNewView1 construction/destruction
CSerComNewView1::CSerComNewView1()
: CFormView(CSerComNewView1::IDD)
{
//{{AFX_DATA_INIT(CSerComNewView1)
m_strSend = _T("");
m_bRepeatSend = FALSE;
m_bHexShow = FALSE;
m_strReceive=_T("");
m_strFilePath = _T("");
flag=1;
m_bytesReceived=0;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CSerComNewView1::~CSerComNewView1()
{
}
void CSerComNewView1::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerComNewView1)
DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
DDX_Check(pDX, IDC_CHECK_REPEAT, m_bRepeatSend);
DDX_Check(pDX, IDC_CHECK_HEXSHOW, m_bHexShow);
DDX_Text(pDX, IDC_EDIT_FILEPATH, m_strFilePath);
//}}AFX_DATA_MAP
}
BOOL CSerComNewView1::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CSerComNewView1::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
SetTimer(1,500,NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CSerComNewView1 printing
BOOL CSerComNewView1::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSerComNewView1::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSerComNewView1::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CSerComNewView1::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CSerComNewView1 diagnostics
#ifdef _DEBUG
void CSerComNewView1::AssertValid() const
{
CFormView::AssertValid();
}
void CSerComNewView1::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CSerComNewDoc* CSerComNewView1::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSerComNewDoc)));
return (CSerComNewDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSerComNewView1 message handlers
void CSerComNewView1::OnBtnConfigure()
{
// TODO: Add your control notification handler code here
CConfigDlg* dlg=new CConfigDlg(this,m_Ports[0].GetDCB());
DWORD dwCommEvents = m_Ports[0].GetCommEvents();
dlg->m_CommBreakDetected = (dwCommEvents & EV_BREAK) > 0 ? TRUE : FALSE;
dlg->m_CommCTSDetected = (dwCommEvents & EV_CTS) > 0 ? TRUE : FALSE;
dlg->m_CommDSRDetected = (dwCommEvents & EV_DSR) > 0 ? TRUE : FALSE;
dlg->m_CommERRDetected = (dwCommEvents & EV_ERR) > 0 ? TRUE : FALSE;
dlg->m_CommRingDetected = (dwCommEvents & EV_RING) > 0 ? TRUE : FALSE;
dlg->m_CommRLSDDetected = (dwCommEvents & EV_RLSD) > 0 ? TRUE : FALSE;
dlg->m_CommRxchar = (dwCommEvents & EV_RXCHAR) > 0 ? TRUE : FALSE;
dlg->m_CommRxcharFlag = (dwCommEvents & EV_RXFLAG) > 0 ? TRUE : FALSE;
dlg->m_CommTXEmpty = (dwCommEvents & EV_TXEMPTY) > 0 ? TRUE : FALSE;
if (dlg->DoModal() == IDOK)
{
DWORD dwCommEvents = 0;
if (dlg->m_CommBreakDetected)
dwCommEvents |= EV_BREAK;
if (dlg->m_CommCTSDetected)
dwCommEvents |= EV_CTS;
if (dlg->m_CommDSRDetected)
dwCommEvents |= EV_DSR;
if (dlg->m_CommERRDetected)
dwCommEvents |= EV_ERR;
if (dlg->m_CommRingDetected)
dwCommEvents |= EV_RING;
if (dlg->m_CommRLSDDetected)
dwCommEvents |= EV_RLSD;
if (dlg->m_CommRxchar)
dwCommEvents |= EV_RXCHAR;
if (dlg->m_CommRxcharFlag)
dwCommEvents |= EV_RXFLAG;
if (dlg->m_CommTXEmpty)
dwCommEvents |= EV_TXEMPTY;
m_Ports[0].InitPort(this, 4,
atoi(dlg->m_strBaudRate),
dlg->m_strParity[0],
atoi(dlg->m_strDataBits),
atoi(dlg->m_strStopBits),
dwCommEvents,
atoi(dlg->m_strSendBuffer));
m_Ports[0].StartMonitoring();
}
}
DWORD CSerComNewView1::OnReceiveData(WPARAM ch,LPARAM port)
{
static CFileDialog FileDlg(false);
FileDlg.m_ofn.lpstrFileTitle="选择保存的文件";
FileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0jpg Files(*.jpg)\0*.jpg\0All Files(*.*)\0*.*\0\0";
FileDlg.m_ofn.lpstrDefExt="txt";
CSerComNewDoc *pDoc=GetDocument();
char charTemp=ch;
CString strTemp;
switch(charTemp)
{
case '$':
flag=1;
m_strReceive=_T("");
m_strFlag=_T("");
break;
case '*':
flag=2;
break;
case '#':
if("DATA"==m_strFlag)
{
pDoc->m_strArray.Add(m_strReceive);
pDoc->UpdateAllViews(NULL);
}
if("FILE"==m_strFlag)
{
// AfxMessageBox("file writting");
int nCount=m_strReceive.GetLength();
char *charToWrite=m_strReceive.GetBuffer(nCount);
fwrite(charToWrite,1,nCount,m_pFile);
m_bytesReceived+=nCount;
}
if("COMMAND"==m_strFlag)
{
if("NEWFILE"==m_strReceive)
{
AfxMessageBox("new FILE");
//if(IDOK==FileDlg.DoModal())
//{
//BOOL openResult;
//openResult=m_file.Open(FileDlg.GetFileName(),CFile::modeWrite);
//}
m_pFile=fopen("1.txt","wb");
// if (!(m_file.Open("1.txt",CFile::modeCreate|CFile::modeWrite)))
// AfxMessageBox("创建文件失败");
}
if("CLOSE"==m_strReceive)
{
char chNum[4];
char receiveResult[100];
strcpy(receiveResult,"接收到的字节:");
itoa(m_bytesReceived,chNum,10);
AfxMessageBox("file close");
strcat(receiveResult,chNum);
AfxMessageBox(receiveResult);
m_bytesReceived=0;
// m_file.Close();
fclose(m_pFile);
}
}
break;
default:
if(1==flag)
{
m_strFlag+=charTemp;
}
if(2==flag)
{
if(m_bHexShow)
{
strTemp.Format("0x%x ",charTemp);
m_strReceive+=strTemp;
}
else
{
m_strReceive+=charTemp;
}
}
break;
}
return 0;
}
void CSerComNewView1::OnErrHappen(WPARAM err,LPARAM port)
{
AfxMessageBox("err happen!");
}
void CSerComNewView1::OnBtnSend()
{
// TODO: Add your control notification handler code here
UpdateData(true);
CString strTemp;
if(m_bRepeatSend)
{
AfxMessageBox("请先关闭自动发送");
}
else
{
char *charToSend=(char*)m_strSend.GetBuffer(m_strSend.GetLength());
int len=strlen(charToSend);
char *charTemp=DataPackage(charToSend,1,len);
m_Ports[0].WriteToPort(charTemp,strlen(charTemp));
delete[] m_charPackage;
//SendString(strTemp,strTemp.GetLength());
// char ch[12]={'$','D','A','T','A','*',48,0x0,48,50,51,'#'}; //验证原来的类不能发送二进制模式数据
// m_Ports[0].WriteToPort(ch,sizeof(ch));
}
}
void CSerComNewView1::SendString(CString strSend,int n)
{
//char cf=13,ln=10;
//strSend='$'+strSend+'*';
char *string=(char *)strSend.GetBuffer(strSend.GetLength());
m_Ports[0].WriteToPort(string,n);
}
void CSerComNewView1::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
UpdateData(true);
CString strTemp;
if(m_bRepeatSend)
{
char *charToSend=(char*)m_strSend.GetBuffer(m_strSend.GetLength());
int len=strlen(charToSend);
char *charTemp=DataPackage(charToSend,1,len);
m_Ports[0].WriteToPort(charTemp,strlen(charTemp));
delete[] m_charPackage;
}
CFormView::OnTimer(nIDEvent);
}
void CSerComNewView1::OnBtnClear()
{
// TODO: Add your control notification handler code here
CSerComNewDoc* pDoc=(CSerComNewDoc*)GetDocument();
pDoc->m_strArray.RemoveAll();
pDoc->UpdateAllViews(NULL);
}
bool CSerComNewView1::OnBtnFileSend()
{
// TODO: Add your control notification handler code here
if(!(m_FileThread=AfxBeginThread(FileThread,this)))
return false;
TRACE("File Thread start\n");
return TRUE;
/* CFileDialog FileDlg(true);
FileDlg.m_ofn.lpstrTitle="选择要发送的文件";
FileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0jpg Files(*.jpg)\0*.jpg\0All Files(*.*)\0*.*\0\0";
FileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==FileDlg.DoModal())
{
m_strFilePath=FileDlg.GetPathName();
UpdateData(false);
CFile file(FileDlg.GetPathName(),CFile::modeRead|CFile::typeBinary);
long nRemain=file.GetLength();
CString strTemp;
strTemp=DataPackage("NEWFILE",3);
//SetEvent(m_Ports[0].m_hWriteEventFlag);
WaitForSingleObject(m_Ports[0].m_hWriteEventFlag,INFINITE);
SendString(strTemp);
ResetEvent(m_Ports[0].m_hWriteEventFlag);
while(nRemain>0)
{
char charSend[100];
memset(charSend,0,sizeof(charSend));
file.Read(charSend,100);
strTemp=charSend;
strTemp=DataPackage(strTemp,2);
WaitForSingleObject(m_Ports[0].m_hWriteEventFlag,INFINITE);
SendString(strTemp);
nRemain=nRemain-100;
ResetEvent(m_Ports[0].m_hWriteEventFlag);
}
file.Close();
/* char *pReadBuffer=new char[nRemain]; //使用ReadHuge()函数读大块数据
memset(pReadBuffer,0,sizeof(pReadBuffer));
file.ReadHuge(pReadBuffer,nRemain);
strTemp=pReadBuffer;
strTemp=DataPackage(strTemp,2);
SendString(strTemp);
file.Close();*/
/*FILE *file=fopen(m_strFilePath,"r");
fseek(file,0,SEEK_END);
int len=ftell(file);
rewind(file);
while(len>0)
{
char charSend[10];
memset(charSend,0,sizeof(charSend));
fread(charSend,1,10,file);
strTemp=charSend;
strTemp=DataPackage(strTemp,2);
SendString(strTemp);
len=len-10;
Sleep(1);
}
Sleep(1);
fclose(file);
strTemp=DataPackage("CLOSE",3);
SendString(strTemp);
}*/
}
UINT CSerComNewView1::FileThread(LPVOID pParam)
{
CSerComNewView1 *pComView=(CSerComNewView1*)pParam;
CFileDialog FileDlg(true);
FileDlg.m_ofn.lpstrTitle="选择要发送的文件";
FileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0jpg Files(*.jpg)\0*.jpg\0All Files(*.*)\0*.*\0\0";
FileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==FileDlg.DoModal())
{
pComView->m_strFilePath=FileDlg.GetPathName();
//UpdateData(false);
CFile file(FileDlg.GetPathName(),CFile::modeRead|CFile::typeBinary);
long nRemain=file.GetLength();
char *charTemp=pComView->DataPackage("NEWFILE",3,7);
//SetEvent(pComView->m_Ports[0].m_hWriteEventFlag);
WaitForSingleObject(pComView->m_Ports[0].m_hWriteEventFlag,INFINITE);
//pComView->SendString(strTemp,strTemp.GetLength());
pComView->m_Ports[0].WriteToPort(charTemp,17);
ResetEvent(pComView->m_Ports[0].m_hWriteEventFlag);
delete[] pComView->m_charPackage;
long n=0;
char readResult[100];
memset(readResult,0,sizeof(readResult));
strcpy(readResult,"发送的文件字节数");
char cn[8];
memset(cn,0,sizeof(cn));
itoa(nRemain,cn,10);
strcat(readResult,cn);
strcat(readResult,"; ");
char charSend[20];
while(nRemain>0)
{
memset(charSend,0,sizeof(charSend));
file.Read(charSend,20);
//strTemp=charSend;
charTemp=pComView->DataPackage(charSend,2,20);
WaitForSingleObject(pComView->m_Ports[0].m_hWriteEventFlag,INFINITE);
//pComView->SendString(strTemp,strTemp.GetLength());
pComView->m_Ports[0].WriteToPort(charTemp,20+7);
n+=strlen(charSend);
ResetEvent(pComView->m_Ports[0].m_hWriteEventFlag);
delete[] pComView->m_charPackage;
nRemain=nRemain-20;
}
file.Close();
memset(cn,0,sizeof(cn));
itoa(n,cn,10);
strcat(readResult,"读取文件字节数");
strcat(readResult,cn);
AfxMessageBox(readResult);
/* char *pReadBuffer=new char[nRemain]; //使用ReadHuge()函数读大块数据
memset(pReadBuffer,0,sizeof(pReadBuffer));
file.ReadHuge(pReadBuffer,nRemain);
strTemp=pReadBuffer;
strTemp=DataPackage(strTemp,2);
SendString(strTemp);
file.Close();*/
/* FILE *file=fopen(FileDlg.GetPathName(),"rb");
fseek(file,0,SEEK_END);
int len=ftell(file);
rewind(file);
while(len>0)
{
char charSend[101];
memset(charSend,0,sizeof(charSend));
fread(charSend,1,100,file);
strTemp=charSend;
strTemp=pComView->DataPackage(strTemp,2);
WaitForSingleObject(pComView->m_Ports[0].m_hWriteEventFlag,INFINITE);
pComView->SendString(strTemp);
ResetEvent(pComView->m_Ports[0].m_hWriteEventFlag);
len=len-100;
}
fclose(file);*/
WaitForSingleObject(pComView->m_Ports[0].m_hWriteEventFlag,INFINITE);
charTemp=pComView->DataPackage("CLOSE",3,5);
// pComView->SendString(strTemp,strTemp.GetLength());
pComView->m_Ports[0].WriteToPort(charTemp,15);
ResetEvent(pComView->m_Ports[0].m_hWriteEventFlag);
delete[] pComView->m_charPackage;
}
return 0;
}
char* CSerComNewView1::DataPackage(char *charTemp,UINT type,int len)
{
// CString strTemp=_T("");
// switch (type)
// {
// case 1:
// strTemp="$DATA*"+strData+'#';
// break;
// case 2:
// strTemp="$FILE*"+strData+'#';
// break;
// case 3:
// strTemp="$COMMAND*"+strData+'#';
// break;
// default:
// break;
// }
//
// return strTemp;
int i=0;
switch(type)
{
case 1:
m_charPackage=new char[len+7];
strcpy(m_charPackage,"$DATA*");
for(i=0;i<len;i++)
{
m_charPackage[6+i]=charTemp[i];
}
m_charPackage[6+len]='#';
return m_charPackage;
break;
case 2:
m_charPackage=new char[len+7];
strcpy(m_charPackage,"$FILE*");
for(i=0;i<len;i++)
{
m_charPackage[6+i]=charTemp[i];
}
m_charPackage[6+len]='#';
return m_charPackage;
break;
case 3:
m_charPackage=new char[len+10];
strcpy(m_charPackage,"$COMMAND*");
for(i=0;i<len;i++)
{
m_charPackage[9+i]=charTemp[i];
}
m_charPackage[9+len]='#';
return m_charPackage;
break;
default:
break;
}
}
void CSerComNewView1::OnBtnclose()
{
// TODO: Add your control notification handler code here
m_Ports[0].ClosePort();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -