📄 ssdlg.cpp
字号:
// ssDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ss.h"
#include "ssDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSsDlg dialog
CSsDlg::CSsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSsDlg)
m_edit = _T("");
m_edit1 = _T("");
m_edit2 = _T("");
m_edit3 = _T("");
m_edit4 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSsDlg)
DDX_Control(pDX, IDC_MSCOMM1, m_com1);
DDX_Text(pDX, IDC_EDIT, m_edit);
DDX_Text(pDX, IDC_EDIT1, m_edit1);
DDX_Text(pDX, IDC_EDIT2, m_edit2);
DDX_Text(pDX, IDC_EDIT3, m_edit3);
DDX_Text(pDX, IDC_EDIT4, m_edit4);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSsDlg, CDialog)
//{{AFX_MSG_MAP(CSsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_OPEN_FILE, OnOpenFile)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
ON_BN_CLICKED(IDC_RADIO_SEND, OnRadioSend)
ON_BN_CLICKED(IDC_RADIO_RECEIVE, OnRadioReceive)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSsDlg message handlers
BOOL CSsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_edit=" ";
m_edit1=" ";
count=0;
file=0;
if(m_com1.GetPortOpen())
m_com1.SetPortOpen(FALSE);
m_com1.SetCommPort(1); //选择com1
if( !m_com1.GetPortOpen())
m_com1.SetPortOpen(TRUE);//打开串口
else
AfxMessageBox("cannot open serial port");
m_com1.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位
m_com1.SetInputMode(1); //1:表示以二进制方式检取数据
m_com1.SetRThreshold(1);
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_com1.SetInputLen(0); //设置当前接收区数据长度为0
m_com1.GetInput();//先预读缓冲区以清除残留数据
return TRUE; // return TRUE unless you set the focus to a control
}
void CSsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSsDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BEGIN_EVENTSINK_MAP(CSsDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CSsDlg)
ON_EVENT(CSsDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CSsDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
//count = 0;
VARIANT m_input1;
COleSafeArray m_input2;
long length,i;
BYTE data[1024];
CString str,str1;
str1.Empty();
if(m_com1.GetCommEvent()==2)
{
m_input1=m_com1.GetInput(); //读取缓冲区内的数据
m_input2=m_input1; //将VARIANT型变量转换为COleSafeArray型变量
length=m_input2.GetOneDimSize(); //确定数据长度
for(i=0;i<length;i++)
m_input2.GetElement(&i,data+i); //将数据转换为BYTE型数组
/*if(file==1)
{
CFile cf;
GetDlgItemText(IDC_EDIT4,m_edit4);
if(cf.Open(m_edit4,CFile::modeCreate|CFile::modeWrite))
{
try
{
cf.SeekToEnd();
//用于移动到文件尾,保证文件开头的数据不会在每次附加数据写入文件时被覆盖
cf.Write(data,length);
cf.Flush();
cf.Close();
}
catch(CFileException *e)
{
//Alert users to error
e->Delete();
}
}
file = 0;
count = 0;
m_edit4.Empty();
UpdateData(FALSE);
CButton*pWnd = (CButton*)GetDlgItem(IDC_RADIO_RECEIVE);
pWnd->SetCheck(BST_UNCHECKED);
}*/
for(i=0;i<length;i++) //将数组转换为CString型变量
{
BYTE a=*(char*)(data+i);
str.Format("%c",a);
str1+=str;
count++;
}
if(file==1&&m_com1.GetCommEvent()==EOF)
{
CFile cf;
GetDlgItemText(IDC_EDIT4,m_edit4);
if(cf.Open(m_edit4,CFile::modeCreate|CFile::modeWrite))
{
try
{
cf.SeekToEnd();
//用于移动到文件尾,保证文件开头的数据不会在每次附加数据写入文件时被覆盖
cf.Write(data,length);
cf.Flush();
cf.Close();
}
catch(CFileException *e)
{
//Alert users to error
e->Delete();
}
}
file = 0;
count = 0;
m_edit4.Empty();
UpdateData(FALSE);
CButton*pWnd = (CButton*)GetDlgItem(IDC_RADIO_RECEIVE);
pWnd->SetCheck(BST_UNCHECKED);
}
str1+="\r\n";
m_edit+=str1;
//SetDlgItemText(IDC_EDIT,m_Str);
//m_Str1=m_Str;
m_edit1.Format("%d",count);
m_edit1+="\r\n";
}
UpdateData(FALSE);
}
void CSsDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_edit.Empty();
m_edit1.Empty();
m_edit2.Empty();
//count=0;
//str.Format("d%",count);
//SetDlgItemText(IDC_EDIT,m_Str);
//SetDlgItemText(IDC_EDIT,str);
UpdateData(FALSE);
}
void CSsDlg::OnSend()
{
// TODO: Add your control notification handler code here
if(file==1)
{
FILE *pf;
//extern bool tg;
//tg=true;
CByteArray sendData;
int line=0;
int i=0;
//long int Filelength;
sendData.SetSize(17);
BYTE* sendArr = new BYTE[17];
/*CFileDialog Dlg(true);
//Dlg.m_ofn.lpstrFilter="Txt File(*.dat)\0*.dat\0All File(*.*)\0*.*\0\0";
//CString fnamee;
if(IDOK==Dlg.DoModal())
{
fnamee=Dlg.GetPathName();
fnamee.Replace("\\","\\\\");
CFile FileDlg(Dlg.GetFileName(), CFile::modeRead );
Filelth=FileDlg.GetLength();
FileDlg.Close();
}*/
int Filelth = count;
GetDlgItemText(IDC_EDIT3,m_edit3);
pf=fopen(m_edit3,"r+b");
CString cs;
cs.Format("%d",Filelth);
AfxMessageBox("统计文件字节数中");
AfxMessageBox(cs);
while(line*16<Filelth)
{
if(line*16+16<=Filelth)
{
fseek(pf,line*16,0);
for(int ii=0;ii<16;ii++)
fread(&sendArr[ii],1,1,pf); //将文件数据存入BYTE型数组中
for(int j =0; j<16; j++)
{
sendData.SetAt(j, sendArr[j]); //将BYTE型数据转换为COleVariant型
}
m_com1.SetOutput(COleVariant(sendData));
//CString bb;
}
else
{
i=Filelth%16;
fseek(pf,line*16,0);
for(int ii=0;ii<i;ii++)
fread(&sendArr[ii],1,1,pf);
for(int j =0; j<i; j++)
{
sendData.SetAt(j, sendArr[j]);
}
m_com1.SetOutput(COleVariant(sendData));
}
line++;
}
delete sendArr;
if(Filelth==(line-1)*16+i)
AfxMessageBox("发送完毕");
fclose(pf);
file = 0;
CButton*pWnd = (CButton*)GetDlgItem(IDC_RADIO_SEND);
pWnd->SetCheck(BST_UNCHECKED);
m_edit3.Empty();
UpdateData(FALSE);
}
UpdateData(TRUE);
m_com1.SetOutput(COleVariant(m_edit2));
}
void CSsDlg::OnOpenFile()
{
// TODO: Add your control notification handler code here
CButton*pWnd = (CButton*)GetDlgItem(IDC_RADIO_SEND);
BOOL bSend = pWnd->GetCheck(); //检查Radio Button是否是发送状态
//static int Filelth;
if(bSend)
{
CString strFilePath;
char szFileters[] = "C files (*.c)|*.c|Text files(*.txt)|*.txt|All files(*.*)|*.*||";
//对文件类型过滤器进行设置
CFileDialog Filedlg(TRUE, //指明构造一个打开文件的对话框
NULL, //不指定扩展名
NULL, //不显示初始文件名
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|
OFN_ALLOWMULTISELECT|OFN_EXPLORER,
szFileters, //允许设置文件类型过滤器
NULL);
if(Filedlg.DoModal()==IDOK)
{
//strFilePath = Filedlg.GetPathName();
CFile FileOp(Filedlg.GetPathName(), CFile::modeRead );
//按照Filedlg.GetPathName()所获得路径以只读方式打开文件
count=FileOp.GetLength(); //获取文件大小(字节数)
CWnd*pWnd = GetDlgItem(IDC_EDIT3);
pWnd->SetWindowText(Filedlg.GetPathName()); //将路径显示于对应编辑框中
file = 1;
}
//m_edit3 = strFilePath;
}
}
void CSsDlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
CButton*pWnd = (CButton*)GetDlgItem(IDC_RADIO_RECEIVE);
BOOL bReceive = pWnd->GetCheck(); //检查Radio Button是否是接收状态
if(bReceive)
{
char szFileters[] = "C files (*.c)|*.c|Text files(*.txt)|*.txt|All files(*.*)|*.*||";
//对文件类型过滤器进行设置
CFileDialog Filedlg(FALSE, //指明构造一个保存文件的对话框
NULL, //不指定扩展名
NULL, //不显示初始文件名
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|
OFN_ALLOWMULTISELECT|OFN_EXPLORER,
szFileters, //允许设置文件类型过滤器
NULL);
if(Filedlg.DoModal()==IDOK)
{
//CString strFilePath = Filedlg.GetPathName();
CWnd*pWnd = GetDlgItem(IDC_EDIT4);
pWnd->SetWindowText(Filedlg.GetPathName()); //将路径显示于对应编辑框中
file = 1;
}
}
}
void CSsDlg::OnRadioSend()
{
// TODO: Add your control notification handler code here
file=1;
}
void CSsDlg::OnRadioReceive()
{
// TODO: Add your control notification handler code here
file=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -