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

📄 manipulate.cpp

📁 这是我给一个做射频卡的公司做的小程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Manipulate.cpp : implementation file
//

#include "stdafx.h"
#include "标签数据录入系统.h"
#include "Manipulate.h"
#include "MainFrm.h"
#include "globals.h"
#include "afxdb.h"
#ifdef _DEBUG


#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CDatabase db;
CString date;
CString temp;
bool flag;
CString rdata;

CString seq[16],seqnum;
CString typeID,typecodeID,partscodeID,repaireID,yearID,monthID,code;
int nPort = 0;
HANDLE hCom = 0;	
CString strUID = _T("");
char UIDBuf[600] ;
	
/////////////////////////////////////////////////////////////////////////////
// Manipulate dialog


extern "C" __declspec(dllexport) HANDLE __stdcall OpenCom(int nPort,long nBaudRate,
												int nParity,
												int nDataBits,int nStopBit,
												int nFlowCtl);

extern "C" __declspec(dllexport) void __stdcall CloseCom(HANDLE hCom);

extern "C" __declspec(dllexport) BOOL __stdcall WriteSingleBlock(HANDLE hCom,
					BYTE Addr, BYTE Mode,BYTE *UID,BYTE BlockAddr, 
					char *WriteData);
extern "C" _declspec(dllexport) BOOL __stdcall DeviceInfo(HANDLE hCom, BYTE Addr,char* Sw_Rev,
						char* D_Rev,char* Hw_Type,
						char* Sw_Type,char* Tr_Type);
extern "C" _declspec(dllexport) BOOL __stdcall WriteSingleBlock(HANDLE hCom,
					BYTE Addr, BYTE Mode,BYTE *UID,BYTE BlockAddr, 
					char *WriteData);
extern "C" _declspec(dllexport) BOOL __stdcall ReadTagData(HANDLE hCom,
						BYTE Addr, BYTE Mode,BYTE *UID, 
						BYTE BlockAddr,BYTE nBlockNum,
						char *ReadData);
extern "C" _declspec(dllexport) void __stdcall CloseCom(HANDLE hCom);
extern "C" _declspec(dllexport) BOOL __stdcall DetectTagUID(HANDLE hCom,BYTE Addr,char* ReadUID,int &LableNum);
extern "C" _declspec(dllexport) BOOL __stdcall SelectTag(HANDLE hCom,
													  BYTE Addr, 
													  BYTE *UID, 
													  BOOL bFlag);
extern "C" _declspec(dllexport) BOOL __stdcall WriteSingleBlock(HANDLE hCom,
					BYTE Addr, BYTE Mode,BYTE *UID,BYTE BlockAddr, 
					char *WriteData);





Manipulate::Manipulate(CWnd* pParent /*=NULL*/)
	: CDialog(Manipulate::IDD, pParent)
{
	//{{AFX_DATA_INIT(Manipulate)
	m_name = _T("");
	m_sequence = _T("");
	m_user = _T("");
	m_record = _T("");
	m_pro = _T("");
	m_xinpian = _T("");
	//}}AFX_DATA_INIT
}


void Manipulate::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(Manipulate)
	DDX_Control(pDX, IDC_DATETIMEPICKER1, m_date);
	DDX_Control(pDX, IDC_COMYEAR, m_year);
	DDX_Control(pDX, IDC_COMPARTCODE, m_partscode);
	DDX_Control(pDX, IDC_COMMATH, m_manth);
	DDX_Control(pDX, IDC_COMTYPECODE, m_typecode);
	DDX_Control(pDX, IDC_COMTYPE, m_type);
	DDX_Control(pDX, IDC_COMREP, m_repaire);
	DDX_CBString(pDX, IDC_COMYEAR, m_cyear);
	DDX_CBString(pDX, IDC_COMPARTCODE, m_cpartscode);
	DDX_CBString(pDX, IDC_COMMATH, m_cmanth);
	DDX_CBString(pDX, IDC_COMTYPECODE, m_ctypecode);
	DDX_CBString(pDX, IDC_COMTYPE, m_ctype);
	DDX_CBString(pDX, IDC_COMREP, m_crepaire);
	DDX_Text(pDX, IDC_NAME, m_name);
	DDX_Text(pDX, IDC_SEQENCE, m_sequence);
	DDX_Text(pDX, IDC_USER, m_user);
	DDX_Text(pDX, IDC_RECORD, m_record);
	DDX_Text(pDX, IDC_PRO, m_pro);
	DDX_Text(pDX, IDC_XINPIAN, m_xinpian);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(Manipulate, CDialog)
	//{{AFX_MSG_MAP(Manipulate)
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER1, OnDatetimechangeDatetimepicker1)
	ON_BN_CLICKED(IDC_WRITE, OnWrite)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_WRITECOPY, OnWritecopy)
	ON_BN_CLICKED(IDC_CHANGE, OnChange)
	ON_BN_CLICKED(IDC_CANCEL, OnCancel)
	ON_BN_CLICKED(IDC_READ, OnRead)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Manipulate message handlers

void Manipulate::OnCancel() 
{
	// TODO: Add extra cleanup here
	//delete pss1;
	CMainFrame *fm; 
	fm = (CMainFrame *)AfxGetMainWnd();
	
	fm->SendMessage(WM_CLOSE);
	//	db.Close();
	CDialog::OnCancel();
}

BOOL Manipulate::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//////打开数据库
		if(!db.Open("桌面数据读写")) 
	{
		AfxMessageBox("打开数据库失败!");	//		
	return false; // 连接数据库
	}

//打开串口

	int iTagCnt = 0;
	char Sw_Rev [50];
	char D_Rev[50]; 
	char Hw_Type[50]; 
	char Sw_Type[50];
	char Tr_Type[50];
	//初始化
	memset(Sw_Rev, 0, 50);
	memset(D_Rev, 0, 50);
	memset(Hw_Type, 0, 50);
	memset(Sw_Type, 0, 50);
	memset(Tr_Type, 0, 50);

	nPort=1;
	hCom = OpenCom(nPort,9600,2,8,0,0);
	if(hCom == INVALID_HANDLE_VALUE)
	{
		AfxMessageBox("打开串口1失败!");
	}
	else
	{
		if( DeviceInfo(hCom,0x00, Sw_Rev,D_Rev,Hw_Type,	Sw_Type,Tr_Type))
		{
		//	AfxMessageBox("读写器(串口1)已经打开!");
	
			int LableNum = 1;
			char readdata[600];
			memset(UIDBuf, 0, 600);
			memset(readdata, 0, 600);

			if(!DetectTagUID(hCom, 0x00,UIDBuf,LableNum))
			{
				AfxMessageBox("没有检测到标签");
			}
			
			else
			{
				CString q;
				q.Format("检测到%d个标签",LableNum);
				AfxMessageBox(q);
			}
		}
		else
		{
			nPort=2;
			hCom = OpenCom(nPort,9600,2,8,0,0);
		
			if(hCom == INVALID_HANDLE_VALUE)
			{
				AfxMessageBox("打开串口2失败!");
			}
			else if( DeviceInfo(hCom,0x00, Sw_Rev,D_Rev,Hw_Type,	Sw_Type,Tr_Type))
			{
			//	AfxMessageBox("读写器(串口2)已经打开!");
			
				int LableNum = 0;
				memset(UIDBuf, 0, 600);
				char readdata[600];
				memset(readdata, 0, 600);
				char writedata[4],wd[16]="woshiyigexue";
				memccpy( writedata, wd, wd[3],4);

				if(!DetectTagUID(hCom, 0x00,UIDBuf,LableNum))
				{
					AfxMessageBox("没有检测到标签");
				}
				else
				{
					CString q;
					q.Format("检测到%d个标签",LableNum);
					AfxMessageBox(q);
				}

			}
				else AfxMessageBox("读写器打开失败!");
		}
	}





	CRecordset rs(&db); // 数据集
	CString str;
	// 设置部门下拉框所取的值为产品分类表表中的值
	rs.Open(CRecordset::forwardOnly, "select 时间 from 保修期");
	while(!rs.IsEOF())
	{
		rs.GetFieldValue("时间", str);
		m_repaire.AddString(str);
		rs.MoveNext();
	}
	rs.Close();




	rs.Open(CRecordset::forwardOnly, "select 产品名称 from 产品分类表");
	while(!rs.IsEOF())
	{
		rs.GetFieldValue("产品名称", str);
		m_typecode.AddString(str);
		rs.MoveNext();
	}
	rs.Close();

	
	
	rs.Open(CRecordset::forwardOnly, "select 部件名称 from 部件类型表");
	while(!rs.IsEOF())
	{
		rs.GetFieldValue("部件名称", str);
		m_partscode.AddString(str);
		rs.MoveNext();
	}
	rs.Close();
		


	rs.Open(CRecordset::forwardOnly, "select 年 from 年份表");
	while(!rs.IsEOF())
	{
		rs.GetFieldValue("年", str);
		m_year.AddString(str);
		rs.MoveNext();
	}
	rs.Close();



	rs.Open(CRecordset::forwardOnly, "select 月份 from 月份表");
	while(!rs.IsEOF())
	{
		rs.GetFieldValue("月份", str);
		m_manth.AddString(str);
		rs.MoveNext();
	}
	rs.Close();


   // m_time.SetFo

	COleDateTime dtMin(1995,1,1,0,0,0);
	COleDateTime dtMax(2005,12,31,23,59,59);
	m_date.SetRange(&dtMin,&dtMax);

//	CDateTimeCtrl
	
		//初始化
		m_xinpian=_T("");
 	//	m_date=_T("");	
 		m_name=_T("广汉科峰电子有限责任公司");
 		m_pro=_T("");
 		m_user=_T("");
		m_record=_T("");
		m_type.SetCurSel(-1);
		m_typecode.SetCurSel(-1);
		m_partscode.SetCurSel(-1);
		m_manth.SetCurSel(-1);
		m_repaire.SetCurSel(-1);
		UpdateData(FALSE);
	
	return TRUE;  
}

void Manipulate::OnDatetimechangeDatetimepicker1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int year;
	int month;
	int day;
	COleDateTime dtSel;
	m_date.GetTime(dtSel);
	year=dtSel.GetYear();
	month=dtSel.GetMonth();
	day=dtSel.GetDay();
	if(month<10&&day<10)
	{
		date.Format("%d0%d0%d",year,month,day);
	}
	else if(month<10&&day>=10)
	{
		date.Format("%d0%d%d",year,month,day);
	}
	else if(month>9&day<10)
	{
		date.Format("%d%d0%d",year,month,day);
	}
	else
	date.Format("%d%d%d",year,month,day);

	 
	*pResult = 0;
}

void Manipulate::OnWrite() 
{

	/////////////////读取标签的UID


	///读取时间
	int year;
	int month;
	int day;
	COleDateTime dtSel;
	m_date.GetTime(dtSel);
	year=dtSel.GetYear();
	month=dtSel.GetMonth();
	day=dtSel.GetDay();
	if(month<10&&day<10)
	{
		date.Format("%d0%d0%d",year,month,day);
	}
	else if(month<10&&day>=10)
	{
		date.Format("%d0%d%d",year,month,day);
	}
	else if(month>9&day<10)
	{
		date.Format("%d%d0%d",year,month,day);
	}
	else
	date.Format("%d%d%d",year,month,day);

	 
	
int k=0;
CString strUID = _T("");

int LableNum = 0;
char UIDBuf[600];

memset(UIDBuf, 0, 600);


if(DetectTagUID(hCom, 0x00,UIDBuf,LableNum))
{
	m_xinpian.Format(UIDBuf);

}
else
{
	AfxMessageBox("没有检测到标签%d",LableNum);
}


	CString str,strSQL;
	CRecordset rs(&db); // 数据集
	UpdateData(); // 更新数据
		if(m_pro.IsEmpty()) // 制造商为空则返回
	{
		AfxMessageBox("制造商不能为空!");
		return;
	}

			if(m_name.IsEmpty()) // 产品名称为空则返回
	{
		AfxMessageBox("产品名称不能为空!");
		return;
	}


	rs.Open(CRecordset::forwardOnly,
	"select 分类码 from  产品分类表 where 产品名称='" + m_ctypecode +"'");
	rs.GetFieldValue("分类码", typecodeID);
	rs.Close();



	rs.Open(CRecordset::forwardOnly,
	"select 部件类型码 from  部件类型表 where 部件名称='" + m_cpartscode +"'");
	rs.GetFieldValue("部件类型码", partscodeID);
	rs.Close();


	rs.Open(CRecordset::forwardOnly,
	"select 年号 from  年份表 where 年='" + m_cyear +"'");
	rs.GetFieldValue("年号", yearID);
	rs.Close();



	rs.Open(CRecordset::forwardOnly,
	"select 月号 from  月份表 where 月份='" + m_cmanth +"'");
	rs.GetFieldValue("月号", monthID);
	rs.Close();



	rs.Open(CRecordset::forwardOnly,
	"select 保修号 from  保修期 where 时间='" + m_crepaire +"'");
	rs.GetFieldValue("保修号", repaireID);
	rs.Close();

	code.Format("%s%s%s%s%s",m_ctype,typecodeID,partscodeID,yearID,monthID);
	



//******************************从数据库检索并*******************************//
	CString temp;
	CString strtype,strtypecode,strpartscode,stryear,strmanth;

	int shujuliang=0;			//数据库中的总的数据量
	
	int num=1;				//数据库中的重复的数据量

	rs.Open(CRecordset::forwardOnly, "select * from 产品表");

	while(!rs.IsEOF())
	{
		rs.GetFieldValue("产品类别", strtype);
		rs.GetFieldValue("分类码",strtypecode);
		rs.GetFieldValue("部件类型码",strpartscode);
		rs.GetFieldValue("年",stryear);
		rs.GetFieldValue("月",strmanth);
		str=str.Left(13);

		if(strtype==m_ctype&&strtypecode==m_ctypecode&&strpartscode==m_cpartscode&&stryear==m_cyear&&strmanth==m_cmanth)
				num++;

		rs.MoveNext();
		shujuliang++;
	}
	rs.Close();


	if(num<=9)
	{
	temp.Format("00%d",num);
	}
	else if(num<=99)
	{
		temp.Format("0%d%d",num/10,num%10);

	}
	else
	{
		temp.Format("%d%d%d",num/100,(num%10)/10,num%100%10);
	
	}

	code.Format("%s%s%s%s%s%s",m_ctype,typecodeID,partscodeID,yearID,monthID,temp);


	m_sequence.Format("%s",temp);
	UpdateData(FALSE);

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////写如芯片////////////////////////////////////
	//写制造商
	char writedata[4];
	writepro();

	//写产品名称到标签
	writename();
	
	//写产品编码到标签
	writecode();

	//写生产日期到标签
	for(int i24=0;i24<4;i24++)
	writedata[i24]=date[i24];
	if(!WriteSingleBlock(hCom, 0x00, 0x00,NULL,25, writedata))
	{
		flag=false;
		AfxMessageBox("写入标签第25块失败");
	}

	for(int i25=4;i25<8;i25++)
	writedata[i25-4]=date[i25];
	if(!WriteSingleBlock(hCom, 0x00, 0x00,NULL,26, writedata))
	{
		flag=false;
		AfxMessageBox("写入标签第26块失败");
	}

	//写保修期到标签
	int charnum;
	charnum=strlen(m_crepaire)%4;
	if(strlen(m_crepaire)<4)
	{
		for(int i26=0;i26<charnum;i26++)
		writedata[i26]=m_crepaire[i26];
		if(!WriteSingleBlock(hCom, 0x00, 0x00,NULL,27, writedata))
		{
		flag=false;
		AfxMessageBox("写入标签第27块失败");
		}
	}
	else
	{
		for(int i26=0;i26<4;i26++)
					writedata[i26]=m_crepaire[i26];
		if(!WriteSingleBlock(hCom, 0x00, 0x00,NULL,7, writedata))
		{
		flag=false;
		AfxMessageBox("写入标签第27块失败");
		}
	}
	
	//写用户单位到标签
	writeuser();

	//写维修记录到标签
	writerecord();

	UpdateData(FALSE);
	//读出标签UID

	//////////////////////////////////////////////////////////////////////////写入到数据库
			if(flag==true)
		{
			CString str,strSQL;
			strSQL="insert into 产品表 (芯片号,制造商,产品名称\
			产品类别,分类码,部件类型码,年,月,序列号,\
			出厂日期,保修期,用户单位,维修记录)\
			values('"
			+ m_xinpian + "','" // UID
			+ m_pro + "','" // 制造商
			+ m_name + "','" // 产品名称
			+ m_ctype + "','" // 产品编码
			+ typecodeID + "','" // 产品编码
			+ partscodeID + "','" // 产品编码
			+ yearID + "','" // 产品编码+ m_ctype + "','" // 产品编码
			+ monthID + "','" // 产品编码
			+ temp +"','"//序列号
			+ repaireID + "','" // 产品编码
			+ date + "','" // 出厂日期
			+ m_user + "','" // 用户单位
			+ m_record + "')"; // 维修记录
			db.ExecuteSQL(strSQL); // 执行
			AfxMessageBox("添加成功");
			Manipulate::OnClear() ;
			UpdateData(FALSE);

		}
}

	


void Manipulate::OnClear() 
{
	// TODO: Add your control notification handler code here
	
			m_xinpian=_T("");
 	//	m_date=_T("");	
 		m_name=_T("广汉科峰电子有限责任公司");
 		m_pro=_T("");
 		m_user=_T("");
		m_record=_T("");
		m_type.SetCurSel(-1);
		m_ctype=_T("");
		m_typecode.SetCurSel(-1);
		m_ctypecode=_T("");
		m_partscode.SetCurSel(-1);
		m_cpartscode=_T("");
		m_manth.SetCurSel(-1);
		m_year.SetCurSel(-1);
		m_cyear=_T("");
		m_cmanth=_T("");
		m_repaire.SetCurSel(-1);
		m_crepaire=_T("");
		m_sequence=_T("");
		UpdateData(FALSE);
	
//	return TRUE; 
}

void Manipulate::OnWritecopy() 
{

	///读取时间
	int year;
	int month;
	int day;
	COleDateTime dtSel;
	m_date.GetTime(dtSel);
	year=dtSel.GetYear();
	month=dtSel.GetMonth();
	day=dtSel.GetDay();
	if(month<10&&day<10)
	{
		date.Format("%d0%d0%d",year,month,day);
	}
	else if(month<10&&day>=10)
	{
		date.Format("%d0%d%d",year,month,day);
	}
	else if(month>9&day<10)
	{
		date.Format("%d%d0%d",year,month,day);
	}
	else
	date.Format("%d%d%d",year,month,day);
//	AfxMessageBox(date);
	 
	
int k=0;
CString strUID = _T("");

int LableNum = 0;
char UIDBuf[600];

memset(UIDBuf, 0, 600);


if(DetectTagUID(hCom, 0x00,UIDBuf,LableNum))
{
	m_xinpian.Format(UIDBuf);

⌨️ 快捷键说明

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