⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tf.cpp

📁 用于信号采集的VC++串口单路互相关上位机程序.
💻 CPP
字号:
// llyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "lly.h"
#include "llyDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLlyDlg dialog

CLlyDlg::CLlyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLlyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLlyDlg)
	m_strCount = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLlyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLlyDlg)
	DDX_CBString(pDX, IDC_COMBO_NN, m_strCount);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLlyDlg, CDialog)
	//{{AFX_MSG_MAP(CLlyDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLlyDlg message handlers

BOOL CLlyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	if(!m_comDev.OpenConnection(2))
	{
	    MessageBox("对不起,你的COM1或COM2打不开,故不能\n通信,请检查后再试!");
	    return TRUE;
	}

    //SetTimer(ID_TIME_CLOCK,200,NULL);
	m_strCount="1024";
	UpdateData(false);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CLlyDlg::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 CLlyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLlyDlg::OnButton1() 
{	/*	FILE *p;
	long len=0;
	if((p=fopen("E:\\lly.txt","w"))==NULL)
		 MessageBox("打开文件失败,再试!");
	while(len==0)
	{	len=ReadCommBlock(m_comDev,inbuf,charLEN);
		inbuf[len]='\0';
		fprintf(p,"%s\n",inbuf);
        }
	fclose(p);*/

	
//建立数据文件
	
	FILE    *fp2;
	FILE	*fp;
	FILE    *fp1;
	FILE *p;	
    if((p=fopen("E:\\lly.txt","w"))==NULL)
		 MessageBox("打开文件失败,再试!");
	if((fp=fopen("E:\\inxyt.txt","w"))==NULL)
		 MessageBox("打开文件失败,再试!");
	 if((fp1=fopen("E:\\inxyt1.txt","w"))==NULL)
		 MessageBox("打开文件失败,再试!");
	 if((fp2=fopen("E:\\inxyt2.txt","w"))==NULL)
		 MessageBox("打开文件失败,再试!");
	//建立数据文件
	//读串口数据
	long len;
    int i,j;
	int ich;
	int iarray[charL];
	int icount;
	int inbuf1[charL];
	int inbuf2[charL];
   
	int iReadCount;
	CString str1;

	UpdateData(true);
	str1=m_strCount;
	iReadCount=0;

	if(str1.Compare("1024")==0)
		iReadCount=2;
	else
		if(str1.Compare("2048")==0)
			iReadCount=4;
		else
			if(str1.Compare("4096")==0)
				iReadCount=8;
			else
				if(str1.Compare("8192")==0)
					iReadCount=16;

	//////进入循环
	for(j=0;j<iReadCount;j++)
	{	
		len=0;
		while(len==0)
			len=ReadCommBlock(m_comDev,inbuf,charLEN);
		inbuf[len]='\0';
		fprintf(p,"%s",inbuf);
		////////////////////////////////////////////////////////
		char szError[128];
		wsprintf(szError,"\n%ld bytes read", len);
		OutputDebugString(szError);
		//////////////////////////////////////////////////////
		//读串口数据
		//转化为10进制数并保存
		int ii;
		icount=0;
		for(i=0;i<len-1;i+=2)
		{	
			ii=AlterChar2ToChar1(inbuf[i],inbuf[i+1]);
			iarray[icount++]=ii;
			fprintf(fp,"%d",ii);
			fprintf(fp,"%c",'\n');
		}
		
		//转化为10进制数并保存
		//将保存的10进制数分为两部分并保存
		int ibuf1,ibuf2;

		ibuf1=0;
		ibuf2=0;
		for(i=0;i<icount;i++)
		{
			if(i % 2 ==0)
			{
				inbuf1[ibuf1]=iarray[i]; 
				ibuf1++;
			}
			else
			{
				inbuf2[ibuf2]=iarray[i]; 
				ibuf2++;
			}
		}

		for(i=0;i<ibuf2;i++)
		{
			ich=inbuf2[i];
			fprintf(fp2,"%d",ich);
			fprintf(fp2,"%c",'\n');
		}
	
		for(i=0;i<ibuf1;i++)
		{
			ich=inbuf1[i];
			fprintf(fp1,"%d",ich);
			fprintf(fp1,"%c",'\n');
		}
	}
	//////退出循环体
	fclose(p);
	fclose(fp);
	fclose(fp2);
    fclose(fp1);
	//将保存的10进制数分为两部分并保存
	str1.Format("%d",j);
	MessageBox("running is over!   j is  "+str1);
}

int CLlyDlg::AlterChar2ToChar1(char ch1, char ch2)
{	
	unsigned char uch;
	char chs[4];
	char chss[8];
	int ichss=0;
	int itemp=0;

	uch=(unsigned char)ch1;
	if(uch==0)
		chss[ichss++]=0x30;
	else
	{
		itemp=HexToDec(ch1);
	    if(itemp>=0 && itemp<=15) 
			chss[ichss++]=ch1;
	    else
		{ 
			_itoa(uch,chs,16);
		    chss[ichss++]=chs[0];
		    chss[ichss++]=chs[1];
		} 
	} 
	 
	uch=(unsigned char)ch2;
	if(uch==0)
		chss[ichss++]=0x30;
	else
	{
		itemp=HexToDec(ch2);
	    if(itemp>=0 && itemp<=15) 
			chss[ichss++]=ch2;
	    else
		{ 
			_itoa(uch,chs,16);
		    chss[ichss++]=chs[0];
		    chss[ichss++]=chs[1];
		} 
	} 
	//if(uch>15)
	//{
	//	_itoa(uch,chs,16);
	//	chss[ichss++]=chs[0];
	//	chss[ichss++]=chs[1];
	//}
	//else
	//	chss[ichss++]=ch2;

	int ir=0;

	itemp=0;
	switch(ichss)
	{
		case 2:itemp=HexToDec(chss[0]);
			   ir+=itemp*16;
			   itemp=HexToDec(chss[1]);
			   ir+=itemp;
			   break;
		case 3:itemp=HexToDec(chss[0]);
			   ir+=itemp*16*16;
			   itemp=HexToDec(chss[1]);
			   ir+=itemp*16;
			   itemp=HexToDec(chss[2]);
			   ir+=itemp;
			   break;
		case 4:itemp=HexToDec(chss[0]);
			   ir+=itemp*16*16*16;
			   itemp=HexToDec(chss[1]);
			   ir+=itemp*16*16;
			   itemp=HexToDec(chss[2]);
			   ir+=itemp*16;
			   itemp=HexToDec(chss[3]);
			   ir+=itemp;
			   break;
	}
		
	return ir;
}

int CLlyDlg::HexToDec(char ch)
{
	int ir=-1;

	switch(ch)
	{
		case '0':ir=0;	break;
		case '1':ir=1;	break;
		case '2':ir=2;	break;
		case '3':ir=3;	break;
		case '4':ir=4;	break;
		case '5':ir=5;	break;
		case '6':ir=6;	break;
		case '7':ir=7;	break;
		case '8':ir=8;	break;
		case '9':ir=9;	break;
		case 'a':ir=10;	break;
		case 'b':ir=11;	break;
		case 'c':ir=12;	break;
		case 'd':ir=13;	break;
		case 'e':ir=14;	break;
		case 'f':ir=15;	break;
		case 'A':ir=10;	break;
		case 'B':ir=11;	break;
		case 'C':ir=12;	break;
		case 'D':ir=13;	break;
		case 'E':ir=14;	break;
		case 'F':ir=15;	break;
		default:break;
	}
	return ir;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -