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

📄 akyarmdlg.cpp

📁 wince 串口通讯非常好的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// akyarmDlg.cpp : implementation file
//

#include "stdafx.h"
#include "akyarm.h"
#include "akyarmDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAkyarmDlg dialog

unsigned char g_ucRevBuffer[200];
int           g_iRevBufferPoint=0;
	
CAkyarmDlg::CAkyarmDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAkyarmDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAkyarmDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	
	//初始化数据库句柄
	m_hDB = 0;
	//将数据库对象标识设置0
	m_ceOid = 0;     
	
	m_bDBOpen=false;

	int i=0;
	for(i=0;i<200;i++)
	{
		g_ucRevBuffer[i]=(unsigned char)0x0;
	}

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);


}

void CAkyarmDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAkyarmDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAkyarmDlg, CDialog)
//{{AFX_MSG_MAP(CAkyarmDlg)
	ON_BN_CLICKED(IDC_BUTTON_RUN, OnButtonRun)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_WM_CLOSE()
	ON_NOTIFY(NM_CLICK, IDC_LST_STUDENT, OnClickLstStudent)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(UM_HAVEDATA,OnMessageHaveData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAkyarmDlg message handlers

BOOL CAkyarmDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen
	
	// TODO: Add extra initialization here
	
	//设置学生列表框标题	
	CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LST_STUDENT);
	
	CRect rt;
    pListCtrl->GetClientRect(&rt);
	/*
	pListCtrl->InsertColumn(0,_T("编号"), LVCFMT_LEFT, rt.Width() * 0.3);
    pListCtrl->InsertColumn(1,_T("描述"), LVCFMT_LEFT, rt.Width() * 0);
    pListCtrl->InsertColumn(2, _T("时间"), LVCFMT_LEFT, rt.Width() * 0.4);
    pListCtrl->InsertColumn(3, _T("数值"), LVCFMT_LEFT, rt.Width() * 0.3);
	*/
	pListCtrl->InsertColumn(0,_T("编号"), LVCFMT_LEFT, (int)(rt.Width() * 0.25));
    pListCtrl->InsertColumn(1, _T("时间"), LVCFMT_LEFT, (int)(rt.Width() * 0.47));
    pListCtrl->InsertColumn(2, _T("数值"), LVCFMT_LEFT, (int)(rt.Width() * 0.28));
	
	ZB_Connect();

	SetTimer ( 1, 1000, NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CAkyarmDlg::OnSeriesRead(CWnd *pWnd, BYTE *buf, int bufLen)
{

	CAkyarmDlg *pDlg = (CAkyarmDlg *)pWnd;


	int i=0;
    //unsigned char g_ucRevBuffer[200];
	//int  g_iRevBufferPoint=0;
	//接收数据到缓冲区中
	for(i=0;i<bufLen;i++)
	{
       g_ucRevBuffer[g_iRevBufferPoint]=buf[i];
	   g_iRevBufferPoint++;
	   if(g_iRevBufferPoint>=200)
	   {
		   g_iRevBufferPoint=0;
	   }
	}
		///////////////////////////////////////////////
	////////////??????????????????????????
	////////////////////////////////////////////
    //解析缓冲区
	while(g_iRevBufferPoint>=17)
	{
		if(g_ucRevBuffer[0]!=(unsigned char)0x02)
		{
			BufferMove(1);
			continue;
		}
		if(g_ucRevBuffer[1]!=(unsigned char)0x1E)
		{
			BufferMove(1);
			continue;
		}
		if(g_ucRevBuffer[2]!=(unsigned char)0x50)
		{
			BufferMove(1);
			continue;
		}
		if(g_ucRevBuffer[3]!=(unsigned char)0x0C)
		{
			BufferMove(1);
			continue;
		}		
		//校验
		unsigned char ucFCS=0;
		for(i=1;i<16;i++)
		{
			ucFCS^=g_ucRevBuffer[i];
		}
		//如果校验通过,提取数据,并进行存储
			///////////////////////////////////////////////
	////////////??????????????????????????
	////////////////////////////////////////////
		if(ucFCS==g_ucRevBuffer[16])
		{
			unsigned long ulTemp=0;
			unsigned char* pCharTemp=(unsigned char*)&ulTemp;
			pCharTemp[0]=g_ucRevBuffer[4];
			pCharTemp[1]=g_ucRevBuffer[5];
			pCharTemp[2]=g_ucRevBuffer[6];
			pCharTemp[3]=g_ucRevBuffer[7];

			CString strID=_T("");
			CString strTemp=_T("");

			strTemp.Format(_T("%X"),ulTemp);
			strID+=strTemp;

			double dValue=0;
			short isTemp=0;
			unsigned char* pChar=(unsigned char*)&isTemp;
			pChar[0]=g_ucRevBuffer[15];
			pChar[1]=g_ucRevBuffer[14];

			dValue=(double)isTemp;

			//添加到数据库
			HAVEDATA * pData= new HAVEDATA;

			pData->strID=strID;
			pData->dValue=dValue;

			pDlg->SendMessage(UM_HAVEDATA,(WPARAM)pData,(LPARAM)0);

			
		}

		//舍弃该帧
		BufferMove(17);
	}
	delete[] buf;
}


bool CAkyarmDlg::AddNewStudent(REC_STUDENT stu)
{
	CEOID ceOid;
	CEPROPVAL *pProps;
	pProps = new CEPROPVAL[4];
	/*
	//学生学号
	memset(pProps,0,LocalSize(pProps));
	pProps->propid = PID_NO; 
	pProps->val.lpwstr = stu.szNo;
	//学生姓名
	pProps++;
	memset(pProps,0,LocalSize(pProps));
	pProps->propid = PID_NAME ; 
	pProps->val.lpwstr = stu.szName;
	//学生出生日期
	pProps++;
	memset(pProps,0,LocalSize(pProps));
	pProps->propid = PID_BIRTHDAY;
	pProps->val.filetime  = stu.ftBirthday;
	//学生身高
	pProps++;
	memset(pProps,0,LocalSize(pProps));
	pProps->propid = PID_STATURE;
	pProps->val.iVal = stu.iStature;
    
	  pProps = pProps -3 ;
	*/
	

	//学生学号
	memset(pProps,0,sizeof(CEPROPVAL));
	pProps[0].propid = PID_NO; 
	pProps[0].val.lpwstr = stu.szNo;
	//学生姓名
	memset(&pProps[1],0,sizeof(CEPROPVAL));
	pProps[1].propid = PID_NAME ; 
	pProps[1].val.lpwstr = stu.szName;
	//学生出生日期
	memset(&pProps[2],0,sizeof(CEPROPVAL));
	pProps[2].propid = PID_BIRTHDAY;
	pProps[2].val.filetime  = stu.ftBirthday;
	//学生身高
	memset(&pProps[3],0,sizeof(CEPROPVAL));
	pProps[3].propid = PID_STATURE;
	pProps[3].val.dblVal = stu.iStature;
	
	ceOid = CeWriteRecordProps(m_hDB,0,4,pProps);
	
	if (ceOid == 0)
	{
		return false;
	}
	return true;
}

bool CAkyarmDlg::EditStudent(REC_STUDENT stu, CEOID ceOid)
{
	CEOID tmpCeOid;
	CEPROPVAL *pProps;
	pProps = new CEPROPVAL[4];

	/*
	//学生学号
	memset(pProps,0,LocalSize(pProps));
	pProps->propid = PID_NO; 
	pProps->val.lpwstr = stu.szNo;
	//学生姓名
	pProps++;
	memset(pProps,0,LocalSize(pProps));
	pProps->propid = PID_NAME ; 
	pProps->val.lpwstr = stu.szName;
	//学生出生日期
	pProps++;
	memset(pProps,0,LocalSize(pProps));
	pProps->propid = PID_BIRTHDAY;
	pProps->val.filetime  = stu.ftBirthday;
	//学生身高
	pProps++;
	memset(pProps,0,LocalSize(pProps));
	pProps->propid = PID_STATURE;
	pProps->val.dblVal = stu.iStature;
    
    pProps = pProps -3 ;
*/

	//学生学号
	memset(pProps,0,sizeof(CEPROPVAL));
	pProps[0].propid = PID_NO; 
	pProps[0].val.lpwstr = stu.szNo;
	//学生姓名
	memset(&pProps[1],0,sizeof(CEPROPVAL));
	pProps[1].propid = PID_NAME ; 
	pProps[1].val.lpwstr = stu.szName;
	//学生出生日期
	memset(&pProps[2],0,sizeof(CEPROPVAL));
	pProps[2].propid = PID_BIRTHDAY;
	pProps[2].val.filetime  = stu.ftBirthday;
	//学生身高
	memset(&pProps[3],0,sizeof(CEPROPVAL));
	pProps[3].propid = PID_STATURE;
	pProps[3].val.dblVal = stu.iStature;

	tmpCeOid = CeWriteRecordProps(m_hDB,ceOid,4,pProps);
	
	if (tmpCeOid == 0)
	{
		return false;
	}
	return true;
}

int CAkyarmDlg::GetRecordCount(CEGUID *pCeGuid, CEOID ceOid)
{
	int iCount;
	CEOIDINFO oidinfo;
	if (!CeOidGetInfoEx(pCeGuid,ceOid,&oidinfo))
	{
		AfxMessageBox(_T("获取信息失败"));
		return -1;
	}
	iCount = oidinfo.infDatabase.wNumRecords;
	return iCount;
}

void CAkyarmDlg::SaveToFile()
{
	
	FILE *stream;
	stream = _wfopen( STUDENTFILEPATH, _T("w") );
	
	CEOID ceOid;
	WORD wProps;
	DWORD dwRecSize;
	PBYTE pBuff;
	PCEPROPVAL pRecord;
	
	REC_STUDENT * pStudent;
	
	//得到数据库记录数
	int iRecordCount = GetRecordCount(&m_ceGuid,m_ceOid);
	DWORD dwIndex;
	//设置学生列表框标题	
	for (int k=0;k<iRecordCount;k++)
	{
		//移动记录指针
		ceOid = CeSeekDatabase(m_hDB,CEDB_SEEK_BEGINNING,k,&dwIndex);
		
		ASSERT(ceOid !=0);
		pBuff = 0;	
		//读取所有字段值
		ceOid = CeReadRecordProps(m_hDB,CEDB_ALLOWREALLOC,&wProps,NULL,&(LPBYTE)pBuff,&dwRecSize);
		
		ASSERT(ceOid != 0);
		
		pRecord = (PCEPROPVAL)pBuff;
		pStudent = new REC_STUDENT;
		
		for (int i=0;i<wProps;i++)
		{
			switch(pRecord->propid)
			{
			case PID_NO:
				{
					wcscpy(pStudent->szNo,pRecord->val.lpwstr);
					break;
				}
			case PID_NAME:
				{
					wcscpy(pStudent->szName,pRecord->val.lpwstr);
					break;
				}
			case PID_BIRTHDAY:
				{
					pStudent->ftBirthday = pRecord->val.filetime;
					break;
				}
			case PID_STATURE:
				{
					pStudent->iStature = pRecord->val.dblVal;
					break;
				}
			}
			pRecord++;
		}
		LocalFree(pBuff);
		
		//向列表框中添加学生信息
		//添加学生编号
		//添加学生姓名
		//添加学生生日,此处需要做些转换
		SYSTEMTIME systime;
		FileTimeToSystemTime(&(pStudent->ftBirthday),&systime);
		
		//TCHAR szBirthday[11];
		//swprintf(szBirthday,_T("%d-%d-%d"),systime.wYear,systime.wMonth,systime.wDay);
		TCHAR szBirthday[22];
		swprintf(szBirthday,_T("%d-%d-%d %d:%d:%d"),systime.wYear,systime.wMonth,systime.wDay,systime.wHour,systime.wMinute,systime.wSecond);
		
		//添加学生身高,此处需要将数字转换成字符串
		TCHAR szStature[20];
		//_itow(pStudent->iStature,szStature,10);
        swprintf(szStature, _T("%.2f"), pStudent->iStature );
		
		fwprintf( stream, _T("%s "), pStudent->szNo);
		fwprintf( stream, _T("%s "), szBirthday);
		fwprintf( stream, _T("%s\n"), szStature);
		
		delete pStudent;
	}	
	
	fclose( stream );
	
}


bool CAkyarmDlg::ZB_Connect()
{
	m_ceSeries.m_OnSeriesRead = OnSeriesRead;
	if (!m_ceSeries.OpenPort(this,(UINT)1,(UINT)38400,(UINT)4,(UINT)8,(UINT)0))
	{
		AfxMessageBox(L"打开串口失败");
		return false;
	}
	return true;
}

bool CAkyarmDlg::ZB_Disconnect()
{
	m_ceSeries.ClosePort();
	return true;
}

bool CAkyarmDlg::ZB_SendData()
{
    BYTE * buf;
		///////////////////////////////////////////////
	////////////??????????????????????????
	////////////////////////////////////////////
	int bufLen = m_strSend.GetLength()*2;
	buf = new BYTE(bufLen);
	CopyMemory(buf,m_strSend.GetBuffer(m_strSend.GetLength()),bufLen);
	if (!m_ceSeries.WritePort(buf,bufLen))
	{
		AfxMessageBox(L"写入失败");
	}	
	return true;
}

⌨️ 快捷键说明

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