// MainPage.cpp : 实现文件
//

#include "stdafx.h"
#include "R2kDemo.h"
#include "MainPage.h"
#include "R2kDemo.h"

CMainPage* pMainPage = NULL;
static int tagCount = 0; // 限在本文件内使用
static int dataCount = 0;
CFile DataFile;
bool bFileOpened = FALSE;

typedef struct _Tag_data
{
	CString data;
	CString antNo;
	CString deviceNo;
	int count;
}Tag_Data;

CArray<Tag_Data, Tag_Data> tag;

// CMainPage 对话框

IMPLEMENT_DYNAMIC(CMainPage, CPropertyPage)

CMainPage::CMainPage()
	: CPropertyPage(CMainPage::IDD)
	, m_CommMode(TRUE)
	, m_tcpip(_T(""))
	, m_ant1(FALSE)
	, m_ant2(FALSE)
	, m_ant3(FALSE)
	, m_ant4(FALSE)
	, m_mulline(FALSE)
{
}

CMainPage::~CMainPage()
{
}

void CMainPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Radio(pDX, IDC_COM, m_CommMode);
	DDX_Text(pDX, IDC_EDIT_TCP, m_tcpip);
	DDX_Control(pDX, IDC_COMBO_COM, m_com);
	DDX_Check(pDX, IDC_CHK_ANT1, m_ant1);
	DDX_Check(pDX, IDC_CHK_ANT2, m_ant2);
	DDX_Check(pDX, IDC_CHK_ANT3, m_ant3);
	DDX_Check(pDX, IDC_CHK_ANT4, m_ant4);
	DDX_Control(pDX, IDC_COMBO_WT1, m_wt1);
	DDX_Control(pDX, IDC_COMBO_WT2, m_wt2);
	DDX_Control(pDX, IDC_COMBO_WT3, m_wt3);
	DDX_Control(pDX, IDC_COMBO_WT4, m_wt4);
	DDX_Control(pDX, IDC_COMBO_PWR1, m_pwr1);
	DDX_Control(pDX, IDC_COMBO_PWR2, m_pwr2);
	DDX_Control(pDX, IDC_COMBO_PWR3, m_pwr3);
	DDX_Control(pDX, IDC_COMBO_PWR4, m_pwr4);
	DDX_Control(pDX, IDC_LST_TAG, m_list);
	DDX_Check(pDX, IDC_MULLINE, m_mulline);
	DDX_Control(pDX, IDC_CHK_AUTOSAVE, m_ChkAutoSave);
}


BEGIN_MESSAGE_MAP(CMainPage, CPropertyPage)
	ON_EN_CHANGE(IDC_EDIT_TCP, &CMainPage::OnEnChangeEditTcp)
	ON_BN_CLICKED(IDC_COM, &CMainPage::OnBnClickedCom)
	ON_BN_CLICKED(IDC_TCPIP, &CMainPage::OnBnClickedTcpip)
	ON_BN_CLICKED(IDC_BTN_CONNECT, &CMainPage::OnBnClickedBtnConnect)
	ON_BN_CLICKED(IDC_BTN_READANT, &CMainPage::OnBnClickedBtnReadant)
	ON_BN_CLICKED(IDC_BTN_DISCONNECT, &CMainPage::OnBnClickedBtnDisconnect)
	ON_BN_CLICKED(IDC_CHK_ANT1, &CMainPage::OnBnClickedChkAnt1)
	ON_BN_CLICKED(IDC_CHK_ANT2, &CMainPage::OnBnClickedChkAnt2)
	ON_BN_CLICKED(IDC_CHK_ANT3, &CMainPage::OnBnClickedChkAnt3)
	ON_BN_CLICKED(IDC_CHK_ANT4, &CMainPage::OnBnClickedChkAnt4)
	ON_BN_CLICKED(IDC_BTN_GETANT, &CMainPage::OnBnClickedBtnGetant)
	ON_BN_CLICKED(IDC_BTN_INVONCE, &CMainPage::OnBnClickedBtnInvonce)
	ON_BN_CLICKED(IDC_BTN_INV, &CMainPage::OnBnClickedBtnInv)
	ON_BN_CLICKED(IDC_BTN_STOP, &CMainPage::OnBnClickedBtnStop)
	ON_BN_CLICKED(IDC_BTN_CLEAR, &CMainPage::OnBnClickedBtnClear)
	ON_BN_CLICKED(IDC_SORTASC, &CMainPage::OnBnClickedSortasc)
	ON_BN_CLICKED(IDC_SORT_DESC, &CMainPage::OnBnClickedSortDesc)
	ON_BN_CLICKED(IDC_BTN_RESET, &CMainPage::OnBnClickedBtnReset)
	ON_BN_CLICKED(IDC_MULLINE, &CMainPage::OnBnClickedMulline)
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_LST_TAG, &CMainPage::OnLvnColumnclickLstTag)
END_MESSAGE_MAP()


// CMainPage 消息处理程序

void CMainPage::OnEnChangeEditTcp()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CPropertyPage::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
}

BOOL CMainPage::OnInitDialog(void)
{
	CPropertyPage::OnInitDialog();
	pMainPage = this;
	GetSerialPortList(&m_com);
	m_com.SetCurSel(1);
	for (int index = 0; index <= 50; ++index)
	{
		CString wt;
		wt.Format("%d", index * 100);
		m_wt1.AddString(wt);
		m_wt2.AddString(wt);
		m_wt3.AddString(wt);
		m_wt4.AddString(wt);
	}
	m_wt1.SetCurSel(20);
	m_wt2.SetCurSel(20);
	m_wt3.SetCurSel(20);
	m_wt4.SetCurSel(20);
	for (int index = 30; index >= 20; --index )
	{
		CString power;
		power.Format("%d", index);
		m_pwr1.AddString(power);
		m_pwr2.AddString(power);
		m_pwr3.AddString(power);
		m_pwr4.AddString(power);
	}
	 m_pwr1.SetCurSel(0);
	 m_pwr2.SetCurSel(0);
	 m_pwr3.SetCurSel(0);
	 m_pwr4.SetCurSel(0);

	 m_list.InsertColumn(0, _T("编号"), LVCFMT_CENTER, 40);
	 m_list.InsertColumn(1, _T("数据"), LVCFMT_CENTER, 240);
	 m_list.InsertColumn(2, _T("次数"), LVCFMT_CENTER, 40);
	 m_list.InsertColumn(3, _T("天线"), LVCFMT_CENTER, 40);
	 m_list.InsertColumn(4, _T("设备"), LVCFMT_CENTER,40 );

	 SetDlgItemText(IDC_STA_COUNT, _T("标签数:0  寻卡次数: 0   "));

	GetDlgItem(IDC_EDIT_TCP)->ShowWindow(SW_NORMAL);
	GetDlgItem(IDC_COMBO_COM)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);
	//GetDlgItem(IDC_BTN_RESET)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_GETANT)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_READANT)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_INV)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_INVONCE)->EnableWindow(FALSE);	
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);
	CFile file;   
	char* pBuf = NULL;
	file.Open(".\\config.ini",  CFile::modeReadWrite);
	DWORD dwFileLen = file.GetLength();
	pBuf = new char[dwFileLen + 1]; 
	pBuf[dwFileLen] = 0;
	file.Read(pBuf, dwFileLen);  
	file.Close();
	int position = 0;
	//position = strcspn("server=",  pBuf);
	//char* ipstart = pBuf + position + 7;
	while (pBuf[position++] !=  '='); // 找到=号，退出循环时，position为下一个位置
	char* ipstart = pBuf + position ;
	m_tcpip = CString(ipstart);
	UpdateData(FALSE);
	delete[] pBuf;
	pBuf = NULL;

	if (DataFile.Open(_T(".//epc.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate))
	{
		bFileOpened = TRUE;
		DataFile.SeekToEnd();
	}
	else
	{
		MessageBox(_T("打开文件错误"),  _T("错误"), 0);
	}
	return TRUE;
}

void CMainPage::OnBnClickedCom()
{
	UpdateData();
	GetSerialPortList(&m_com);  
	m_com.SetCurSel(1);
	GetDlgItem(IDC_EDIT_TCP)->ShowWindow(m_CommMode);
	GetDlgItem(IDC_COMBO_COM)->ShowWindow(!m_CommMode);
}

void CMainPage::OnBnClickedTcpip()
{
	UpdateData();
	GetDlgItem(IDC_EDIT_TCP)->ShowWindow(m_CommMode);
	GetDlgItem(IDC_COMBO_COM)->ShowWindow(!m_CommMode);
}

void CMainPage::OnBnClickedBtnConnect()
{
	OnBnClickedBtnReset();
	UpdateData();
	if (m_CommMode)  // tcp/ip
	{
		if (R_OK != deviceInit(m_tcpip.GetBuffer()))
		{
			GetDlgItem(IDC_STA_RESULT)->SetWindowText("初始化失败.");
			return;
		}
	}
	else			// com
	{
		char comName[8] = {0};
		m_com.GetLBText( m_com.GetCurSel(), comName);
		if (R_OK != deviceInit("", atoi(&comName[3]), 115200))
		{
			GetDlgItem(IDC_STA_RESULT)->SetWindowText("初始化失败");
			return;
		}
	}
	if (R_OK != deviceConnect())
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("连接失败.");
		return;
	}
	OnBnClickedBtnReadant();
	unsigned char Version[20] = {0};
	if (R_OK == GetDevVersion(&Version[0]))
	{	
		CString strRes = CString("读卡器版本号:") + CString(Version);
		GetDlgItem(IDC_STA_RESULT)->SetWindowText(strRes);
	}
	// 根据逻辑关系设置按钮状态	
	GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(TRUE);
	//GetDlgItem(IDC_BTN_RESET)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_GETANT)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_READANT)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_INV)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_INVONCE)->EnableWindow(TRUE);	
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);

	// 连接成功后，禁用通讯模式控件
	GetDlgItem(IDC_COM)->EnableWindow(FALSE);
	GetDlgItem(IDC_TCPIP)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_TCP)->EnableWindow(FALSE);
	GetDlgItem(IDC_COMBO_COM)->EnableWindow(FALSE);

	((CR2kDemoApp*)AfxGetApp())->m_connected = TRUE;

	//把IP地址写入配置文件
	CFile file;   
	char* pBuf = NULL;
	file.Open(".\\config.ini",  CFile::modeReadWrite);
	DWORD dwFileLen = file.GetLength();
	pBuf = new char[dwFileLen + 1]; 
	pBuf[dwFileLen] = 0;
	file.Read(pBuf, dwFileLen);  
	int position = 0;
	while (pBuf[position++] !=  '='); // 找到=号，退出循环时，position为下一个位置
	char* ipstart = pBuf + position ;
	strncpy(ipstart, m_tcpip.GetBuffer(),  m_tcpip.GetLength());
	dwFileLen = strlen(pBuf);
	file.SeekToBegin(); // 调整文件指针到开头位置
	file.Write(pBuf,  dwFileLen);
	file.Close();
	delete[] pBuf;
	pBuf = NULL;
}

void CMainPage::OnBnClickedBtnReadant()
{
	if (R_OK == GetAnt(&m_ant))
	{
		CString wt, power;
		//ant1
		m_ant1 = m_ant.antEnable[0];
		wt.Format("%d",m_ant.dwell_time[0]);m_wt1.SetWindowText(wt);
		power.Format("%d", m_ant.power[0]/10);m_pwr1.SetWindowText(power);
		m_wt1.EnableWindow(m_ant1);m_pwr1.EnableWindow(m_ant1);

		//ant2
		m_ant2 = m_ant.antEnable[1];
		wt.Format("%d",m_ant.dwell_time[1]);m_wt2.SetWindowText(wt);
		power.Format("%d", m_ant.power[1]/10);m_pwr2.SetWindowText(power);		
		m_wt2.EnableWindow(m_ant2);m_pwr2.EnableWindow(m_ant2);

		//ant3
		wt.Format("%d",m_ant.dwell_time[2]);m_wt3.SetWindowText(wt);
		power.Format("%d", m_ant.power[2]/10);m_pwr3.SetWindowText(power);
		m_ant3 = m_ant.antEnable[2];m_wt3.EnableWindow(m_ant3);m_pwr3.EnableWindow(m_ant3);

		//ant4
		wt.Format("%d",m_ant.dwell_time[3]);m_wt4.SetWindowText(wt);
		power.Format("%d", m_ant.power[3]/10);m_pwr4.SetWindowText(power);
		m_ant4 = m_ant.antEnable[3];m_wt4.EnableWindow(m_ant4);m_pwr4.EnableWindow(m_ant4);

		GetDlgItem(IDC_STA_RESULT)->SetWindowText(_T("获取天线参数成功."));
		UpdateData(FALSE);
	}
	else
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText(_T("获取天线参数失败."));
	}	
}

void CMainPage::OnBnClickedBtnDisconnect()
{
	OnBnClickedBtnClear();
	deviceDisconnect();
	deviceUnInit();
	m_ant1 = FALSE;
	m_ant2 = FALSE;
	m_ant3 = FALSE;
	m_ant4 = FALSE;
	UpdateData(FALSE);
	GetDlgItem(IDC_STA_RESULT)->SetWindowText("");

	GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);
	//GetDlgItem(IDC_BTN_RESET)->EnableWindow(FALSE);

	GetDlgItem(IDC_BTN_GETANT)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_READANT)->EnableWindow(FALSE);

	GetDlgItem(IDC_BTN_INV)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_INVONCE)->EnableWindow(FALSE);	
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);

	// 通讯模式控件启用
	GetDlgItem(IDC_COM)->EnableWindow(TRUE);
	GetDlgItem(IDC_TCPIP)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_TCP)->EnableWindow(TRUE);
	GetDlgItem(IDC_COMBO_COM)->EnableWindow(TRUE);
	// 标识连接状态的变量置无效，用以通知其它页面
	((CR2kDemoApp*)AfxGetApp())->m_connected = FALSE; 
}

void CMainPage::OnBnClickedChkAnt1()
{
	UpdateData();
	if (m_ant1)
	{
		m_wt1.SetCurSel(10);// 设置默认值
		m_pwr1.SetCurSel(0);
	}
	m_wt1.EnableWindow(m_ant1);
	m_pwr1.EnableWindow(m_ant1);
}

void CMainPage::OnBnClickedChkAnt2()
{
	UpdateData();
	if (m_ant2)
	{
		m_wt2.SetCurSel(10);// 设置默认值
		m_pwr2.SetCurSel(0);
	}
	m_wt2.EnableWindow(m_ant2);
	m_pwr2.EnableWindow(m_ant2);
}

void CMainPage::OnBnClickedChkAnt3()
{
	UpdateData();
	if (m_ant3)
	{
		m_wt3.SetCurSel(10);// 设置默认值
		m_pwr3.SetCurSel(0);
	}
	m_wt3.EnableWindow(m_ant3);
	m_pwr3.EnableWindow(m_ant3);
}

void CMainPage::OnBnClickedChkAnt4()
{
	UpdateData();
	if (m_ant4)
	{
		m_wt4.SetCurSel(10);// 设置默认值
		m_pwr4.SetCurSel(0);
	}
	m_wt4.EnableWindow(m_ant4);
	m_pwr4.EnableWindow(m_ant4);
}

void CMainPage::OnBnClickedBtnGetant()
{
	UpdateData();// 更新控件变量

	CString strWorkTime, strPower;

	// 取ant1的设置值	
	m_ant.antEnable[0] = m_ant1; 	
	m_wt1.GetWindowText(strWorkTime);// 获取ComboBox控件的文本
	m_pwr1.GetWindowText(strPower);
	m_ant.dwell_time[0] = atoi(strWorkTime.GetBuffer());
	m_ant.power[0] = atoi(strPower.GetBuffer()) * 10;

	// 取ant2的设置值
	m_ant.antEnable[1] = m_ant2;
	m_wt2.GetWindowText(strWorkTime);
	m_pwr2.GetWindowText(strPower);
	m_ant.dwell_time[1] = atoi(strWorkTime.GetBuffer());
	m_ant.power[1] = atoi(strPower.GetBuffer())* 10;

	// 取ant3的设置值
	m_ant.antEnable[2] = m_ant3;
	m_wt3.GetWindowText(strWorkTime);
	m_pwr3.GetWindowText(strPower);
	m_ant.dwell_time[2] = atoi(strWorkTime.GetBuffer());
	m_ant.power[2] = atoi(strPower.GetBuffer())* 10;

	// 取ant4的设置值
	m_ant.antEnable[3] = m_ant4;
	m_wt4.GetWindowText(strWorkTime);
	m_pwr4.GetWindowText(strPower);
	m_ant.dwell_time[3] = atoi(strWorkTime.GetBuffer());
	m_ant.power[3] = atoi(strPower.GetBuffer())* 10;

	if (R_OK == SetAnt(&m_ant))
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText(_T("设置天线参数成功."));
	}
	else
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText(_T("设置天线参数失败."));
	}
}

void tag_sort(CArray<Tag_Data, Tag_Data> &tagArray,  bool bAsc) // 选择排序
{
	int size = tagArray.GetSize();
	int i, j , min;
	for( i = 0; i < size - 1; ++i)
	{
		min = i;
		for (j = i + 1; j < size; ++j)
		{
			if (bAsc) // 升序
			{
				if (tagArray[min].data > tagArray[j].data) 
					min = j;
			}
			else // 降序
			{
				if (tagArray[min].data < tagArray[j].data) 
					min = j;
			}
		}
		if (min != i) // 交换两个元素的值
		{
			Tag_Data t;
			t.data = tagArray[min].data;
			t.antNo = tagArray[min].antNo;
			t.deviceNo = tagArray[min].deviceNo;
			t.count = tagArray[min].count;

			tagArray[min].data = tagArray[i].data;
			tagArray[min].antNo = tagArray[i].antNo;
			tagArray[min].deviceNo = tagArray[i].deviceNo;
			tagArray[min].count = tagArray[i].count;

			tagArray[i].data = t.data;
			tagArray[i].antNo =t.antNo;
			tagArray[i].deviceNo =t.deviceNo;
			tagArray[i].count =	t.count;
		}
	}
}

void CALLBACK HandleData(int cmdID, unsigned char* pData, int length)
{
	CString strCount, strTagCount;

	if (!pMainPage)
		return;
	if (length> 1 && length < 17)// 去除无效包
	{
			dataCount++; // 读取次数加1，并显示
			if (dataCount == (0xFFFFFF - 1))
			{
				dataCount = 0;
			}
			strCount.Format(_T("%d"), dataCount);
			pMainPage->SetDlgItemText(IDC_DATACOUNT,  strCount);

		CString data = CString("");
		CString temp;
		for(int i = 0; i < length-4; ++i)
		{
			temp.Format("%02X ", pData[i]);
			data += CString(temp);
		}
		// 如果文件打开且选择了保存文件
		if ( bFileOpened && pMainPage->m_ChkAutoSave.GetCheck())
		{
			DataFile.Write(data.GetBuffer(), data.GetLength());
			char enter[2] = {0x0d, 0x0a};
			DataFile.Write(&enter[0], 2);
		}
		int ItemCount = (pMainPage->m_list).GetItemCount();// 取原有项目数做新序号

		CString antNo, deviceNo;
		antNo.Format("%d", pData[15] + 1);
		deviceNo.Format("%d", pData[14]);

		//	检索数组，如果有相同数据，只更新计数（含列表）
		//	否则，增加新项目
		bool bIsNewTag =	TRUE;  // 假设是新标签
		for(int index = 0; index < ItemCount; ++index)
		{
			bool bCompResult = (data  == tag[index].data) ; // 先检查标签数据是否相同
			if (bCompResult)	// 标签相同
			{
				bIsNewTag = FALSE; // 不是新标签
				if (pMainPage->m_mulline)// 设置了不同天线显示多行, 则比较结果由天线是否相同决定
				{						
						bCompResult = (antNo == tag[index].antNo);
				}
			}
			if (bCompResult)  // 是重复数据
			{
				tag[index].count++;
				tag[index].antNo = antNo;
				strCount.Format("%d", tag[index].count);
				pMainPage->m_list.SetItemText(index,2, strCount);
				pMainPage->m_list.SetItemText(index, 3, antNo);
				return;
			}
		}
		// 没有找到相同的数据,加入数组，并在列表中显示

		if (bIsNewTag) 
			tagCount++;  // 不同标签计数	
			strTagCount.Format(_T("%d"),  tagCount);
		pMainPage->SetDlgItemText(IDC_TAGCOUNT, strTagCount);
		Tag_Data newElement;
		newElement.data = data;
		newElement.antNo = antNo;
		newElement.deviceNo = deviceNo;
		newElement.count = 1; // 次数初始化为1
		tag.Add(newElement);

		strCount.Format("%d", tag[ItemCount].count);

		CString item;
		item.Format("%d", ItemCount + 1);
		pMainPage->m_list.InsertItem(ItemCount,item);
		pMainPage->m_list.SetItemText(ItemCount, 1, data);
		pMainPage->m_list.SetItemText(ItemCount,2, strCount);
		pMainPage->m_list.SetItemText(ItemCount, 3, antNo);
		pMainPage->m_list.SetItemText(ItemCount, 4, deviceNo);

	}	
}

void CMainPage::OnBnClickedBtnInvonce()
{
	BeginOnceInv((HANDLE_FUN)HandleData);
}

void CMainPage::OnBnClickedBtnInv()
{
	BeginMultiInv((HANDLE_FUN)HandleData);
	GetDlgItem(IDC_CHK_AUTOSAVE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_INV)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_INVONCE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);
}

void CMainPage::OnBnClickedBtnStop()
{
	StopInv();
	GetDlgItem(IDC_CHK_AUTOSAVE)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_INV)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_INVONCE)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(TRUE);
}

void CMainPage::OnBnClickedBtnClear()
{
	m_list.DeleteAllItems();
	tag.RemoveAll();
	tagCount = 0;
	dataCount = 0;
	SetDlgItemText(IDC_DATACOUNT, _T("0"));
	SetDlgItemText(IDC_TAGCOUNT, _T("0"));
}

void CMainPage::OnBnClickedSortasc()
{
	tag_sort(tag,  TRUE);
	m_list.DeleteAllItems();
	int size = tag.GetSize();
	CString strTemp;
	for (int index = 0; index < size;  ++index)
	{
		strTemp.Format("%d", index + 1);
		m_list.InsertItem(index, strTemp);
		m_list.SetItemText(index, 1, tag[index].data); 
		strTemp.Format("%d", tag[index].count);
		m_list.SetItemText(index, 2, strTemp);
		m_list.SetItemText(index, 3, tag[index].antNo);
		m_list.SetItemText(index, 4, tag[index].deviceNo);
	}
}

void CMainPage::OnBnClickedSortDesc()
{
	tag_sort(tag,  FALSE);
	m_list.DeleteAllItems();
	int size = tag.GetSize();
	CString strTemp;
	for (int index = 0; index < size;  ++index)
	{
		strTemp.Format("%d", index + 1);
		m_list.InsertItem(index, strTemp);
		m_list.SetItemText(index, 1, tag[index].data); 
		strTemp.Format("%d", tag[index].count);
		m_list.SetItemText(index, 2, strTemp);
		m_list.SetItemText(index, 3, tag[index].antNo);
		m_list.SetItemText(index, 4, tag[index].deviceNo);
	}
}

void CMainPage::OnBnClickedBtnReset()
{
	WSADATA wsaData;
	if (0 != WSAStartup(MAKEWORD(2, 2), &wsaData))
		return;
	SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
	if (INVALID_SOCKET == s)
		return;
	SOCKADDR_IN addrServer;
	addrServer.sin_family = AF_INET;
	addrServer.sin_port = htons(20058);
	UpdateData();
	addrServer.sin_addr.S_un.S_addr = inet_addr(m_tcpip.GetBuffer());

	if(SOCKET_ERROR == connect(s, (SOCKADDR*)&addrServer,  sizeof(SOCKADDR)))
		return;
	unsigned char cmd_stop[] = {0x43, 0x4d, 0x2B, 0, 0, 0, 0};

	for (int i = 0; i < 10; ++i)
	{
		send(s, (char*)&cmd_stop[0], 7, 0);	
		//Sleep(100);
	}
	shutdown(s, SD_BOTH);
	closesocket(s);
	WSACleanup();
}

void CMainPage::OnBnClickedMulline()
{
	UpdateData();
}
void CMainPage::GetSerialPortList(CComboBox* pComboBox)
{
	//HANDLE h = INVALID_HANDLE_VALUE;
	pComboBox->ResetContent();
	//for(int i=1; i <= 16 ; i++)
	//{
	//	CString  comName;
	//	comName.Format(_T("COM%d"),  i);
	//	h = ::CreateFile(comName.GetBuffer(),
	//		GENERIC_READ | GENERIC_WRITE,  0, 0,
	//		OPEN_EXISTING,
	//		FILE_ATTRIBUTE_NORMAL, 	0);
	//	if ( h != INVALID_HANDLE_VALUE )
	//	{
	//		pComboBox->AddString(comName);
	//	}
	//	::CloseHandle(h);
	//}
	HKEY hKey ;  //打开串口注册表对应的键值
	int res = 0;
	if ((res = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
											_T("Hardware\\DeviceMap\\SerialComm"), 
											NULL,  KEY_READ, &hKey))
											== ERROR_SUCCESS)
	  {
	      int i=0;  
	    TCHAR portName[256],commName[256];    
	     DWORD dwLong,dwSize;    
	     while(1)    
	   {    
	           dwLong   =   dwSize   =   sizeof(portName);    
	        if( ::RegEnumValue(hKey,    
	              i,    
				 portName,    
				&dwLong,    
	              NULL,    
	             NULL,    
	            (PUCHAR)commName,    
	              &dwSize   )   ==   ERROR_NO_MORE_ITEMS )//   枚举串口    
	              break; 
	        m_com.AddString(  commName   );   // commName就是串口名字    
	        i++;  
	      }  
/*	       if(  m_com.GetCount()   ==   0   )    
	     {    
	         ::AfxMessageBox(_T("找不到串口!!!"));    
	       }  */  
	     RegCloseKey(hKey);    
	  }  
}

void CMainPage::OnLvnColumnclickLstTag(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}
