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

#include "stdafx.h"
#include "Verbpo.h"
#include "UartDataThread.h"
#include "DataSaveThread.h"
#pragma comment(lib, "WinMM.Lib")  
#include "mmsystem.h"
// UartDataThread

int  In_ECG_Buf[500] = {
	2010, 2, 2012, 0, 2012, 0, 2002, 0, 2010, 0, 2008, 0, 2010, 0, 2010, 0, 2004, 0, 2012, 0, 2010, 0, 2010, 0, 2008, 0, 2002, 0, 2014, 0, 2010, 0, 2012, 0, 2010, 0, 2004, 0, 2012, 0,
	2010, 0, 2012, 0, 2010, 0, 2004, 0, 2014, 0, 2010, 0, 2012, 0, 2012, 0, 2006, 0, 2012, 0, 2010, 0, 2010, 0, 2014, 0, 2004, 0, 2016, 0, 2012, 0, 2012, 0, 2010, 0, 2006, 0, 2012, 0,
	2010, 0, 2012, 0, 2012, 0, 2006, 0, 2012, 0, 2012, 0, 2014, 0, 2010, 0, 2006, 0, 2012, 0, 2010, 0, 2014, 0, 2008, 0, 2004, 0, 2012, 0, 2012, 0, 2014, 0, 2010, 0, 2004, 0, 2012, 0,
	2012, 2, 2012, 0, 2010, 0, 2006, 0, 2014, 0, 2012, 0, 2014, 0, 2012, 0, 2012, 0, 2012, 0, 2020, 0, 2014, 0, 2014, 0, 2012, 0, 2014, 0, 2020, 0, 2012, 0, 2014, 0, 2012, 0, 2012, 0,
	2022, 0, 2014, 0, 2012, 0, 2012, 0, 2014, 0, 2020, 0, 2014, 0, 2012, 0, 2010, 0, 2012, 0, 2018, 0, 2014, 0, 2014, 0, 2012, 0, 2010, 0, 2018, 0, 2012, 0, 2014, 0, 2010, 0, 2014, 0,
	2020, 0, 2012, 0, 2012, 0, 2012, 0, 2012, 0, 2018, 0, 2012, 0, 2012, 0, 2010, 0, 2014, 0, 2018, 0, 2010, 0, 2012, 0, 2014, 0, 2012, 0, 2020, 0, 2012, 0, 2024, 0, 2040, 0, 2054, 0,
	2074, 2, 2080, 0, 2088, 0, 2100, 0, 2108, 0, 2116, 0, 2110, 0, 2114, 0, 2112, 0, 2104, 0, 2102, 0, 2086, 0, 2076, 0, 2064, 0, 2048, 0, 2038, 0, 2018, 0, 2014, 0, 2012, 0, 2012, 0,
	2020, 0, 2012, 0, 2014, 0, 2010, 0, 2014, 0, 2022, 0, 2012, 0, 2012, 0, 2012, 0, 2014, 0, 2018, 0, 2014, 0, 2012, 0, 2014, 0, 2012, 0, 2020, 0, 2012, 0, 2016, 0, 1994, 0, 1968, 0,
	1948, 0, 1918, 0, 2024, 0, 2182, 0, 2344, 0, 2510, 0, 2672, 0, 2838, 0, 2764, 0, 2602, 0, 2446, 0, 2272, 0, 2104, 0, 1934, 0, 1906, 0, 1936, 0, 1968, 0, 2002, 0, 2008, 0, 2014, 0,
	2016, 2, 2012, 0, 2010, 0, 2012, 0, 2010, 0, 2016, 0, 2014, 0, 2014, 0, 2014, 0, 2014, 0, 2020, 0, 2018, 0, 2018, 0, 2020, 1, 2022, 0, 2032, 0, 2028, 0, 2030, 0, 2034, 0, 2038, 0,
	2048, 0, 2044, 0, 2048, 0, 2050, 0, 2054, 0, 2058, 0, 2058, 0, 2060, 0, 2060, 0, 2066, 0, 2074, 0, 2072, 0, 2078, 0, 2080, 0, 2084, 0, 2094, 0, 2092, 0, 2094, 0, 2100, 0, 2110, 0,
	2118, 0, 2120, 0, 2130, 0, 2140, 0, 2150, 0, 2162, 0, 2164, 0, 2166, 0, 2170, 0, 2174, 0, 2178, 0, 2172, 0, 2166, 0, 2160, 0, 2156, 0, 2148, 0, 2134, 0, 2122, 0, 2102, 0, 2082, 0,
	2066, 0, 2042, 0, 2036, 0, 2024, 0, 2018, 0, 2014, 0, 2012, 0, 2010, 0, 2010, 0, 2010, 0,
};

const int In_RESP_Buf[375] = {
	0x7F, 0x81, 0x82, 0x84, 0x86, 0x87, 0x89, 0x8B, 0x8C, 0x8E, 0x90, 0x91, 0x93, 0x95, 0x96, 0x98,
	0x99, 0x9B, 0x9D, 0x9E, 0xA0, 0xA1, 0xA3, 0xA5, 0xA6, 0xA8, 0xA9, 0xAB, 0xAC, 0xAE, 0xAF, 0xB1,
	0xB2, 0xB4, 0xB5, 0xB6, 0xB8, 0xB9, 0xBA, 0xBC, 0xBD, 0xBE, 0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6,
	0xC7, 0xC8, 0xC9, 0xCA, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD3, 0xD4, 0xD5, 0xD6,
	0xD7, 0xD8, 0xD8, 0xD9, 0xDA, 0xDB, 0xDB, 0xDC, 0xDC, 0xDD, 0xDE, 0xDE, 0xDF, 0xDF, 0xE0, 0xE0,
	0xE0, 0xE1, 0xE1, 0xE1, 0xE2, 0xE2, 0xE2, 0xE2, 0xE3, 0xE3, 0xE3, 0xE3, 0xE3, 0xE3, 0xE3, 0xE3,
	0xE3, 0xE3, 0xE3, 0xE3, 0xE2, 0xE2, 0xE2, 0xE2, 0xE2, 0xE1, 0xE1, 0xE1, 0xE0, 0xE0, 0xDF, 0xDF,
	0xDE, 0xDE, 0xDD, 0xDD, 0xDC, 0xDC, 0xDB, 0xDA, 0xD9, 0xD9, 0xD8, 0xD7, 0xD6, 0xD6, 0xD5, 0xD4,
	0xD3, 0xD2, 0xD1, 0xD0, 0xCF, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA, 0xC9, 0xC8, 0xC6, 0xC5, 0xC4, 0xC3,
	0xC2, 0xC0, 0xBF, 0xBE, 0xBC, 0xBB, 0xBA, 0xB8, 0xB7, 0xB6, 0xB4, 0xB3, 0xB1, 0xB0, 0xAE, 0xAD,
	0xAB, 0xAA, 0xA8, 0xA7, 0xA5, 0xA4, 0xA2, 0xA1, 0x9F, 0x9E, 0x9C, 0x9A, 0x99, 0x97, 0x95, 0x94,
	0x92, 0x91, 0x8F, 0x8D, 0x8C, 0x8A, 0x88, 0x87, 0x85, 0x83, 0x82, 0x80, 0x7E, 0x7C, 0x7B, 0x79,
	0x77, 0x76, 0x74, 0x72, 0x71, 0x6F, 0x6D, 0x6C, 0x6A, 0x69, 0x67, 0x65, 0x64, 0x62, 0x60, 0x5F,
	0x5D, 0x5C, 0x5A, 0x59, 0x57, 0x56, 0x54, 0x53, 0x51, 0x50, 0x4E, 0x4D, 0x4B, 0x4A, 0x48, 0x47,
	0x46, 0x44, 0x43, 0x42, 0x40, 0x3F, 0x3E, 0x3C, 0x3B, 0x3A, 0x39, 0x38, 0x36, 0x35, 0x34, 0x33,
	0x32, 0x31, 0x30, 0x2F, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29, 0x28, 0x28, 0x27, 0x26, 0x25, 0x25,
	0x24, 0x23, 0x22, 0x22, 0x21, 0x21, 0x20, 0x20, 0x1F, 0x1F, 0x1E, 0x1E, 0x1D, 0x1D, 0x1D, 0x1C,
	0x1C, 0x1C, 0x1C, 0x1C, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B,
	0x1C, 0x1C, 0x1C, 0x1C, 0x1D, 0x1D, 0x1D, 0x1E, 0x1E, 0x1E, 0x1F, 0x1F, 0x20, 0x20, 0x21, 0x22,
	0x22, 0x23, 0x23, 0x24, 0x25, 0x26, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2B, 0x2C, 0x2D, 0x2E,
	0x2F, 0x30, 0x31, 0x32, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3B, 0x3C, 0x3D, 0x3E, 0x40, 0x41,
	0x42, 0x44, 0x45, 0x46, 0x48, 0x49, 0x4A, 0x4C, 0x4D, 0x4F, 0x50, 0x52, 0x53, 0x55, 0x56, 0x58,
	0x59, 0x5B, 0x5D, 0x5E, 0x60, 0x61, 0x63, 0x65, 0x66, 0x68, 0x69, 0x6B, 0x6D, 0x6E, 0x70, 0x72,
	0x73, 0x75, 0x77, 0x78, 0x7A, 0x7C, 0x7D,
};

const int  In_Oxy_Buf[250] =
{
	0x26, 0x27, 0x28, 0x28, 0x28, 0x28, 0x27, 0x27, 0x26, 0x26, 0x25, 0x25, 0x24, 0x24, 0x23, 0x23,
	0x23, 0x22, 0x21, 0x21, 0x20, 0x1F, 0x1F, 0x1E, 0x1D, 0x1D, 0x1D, 0x1B, 0x1A, 0x19, 0x19, 0x18,
	0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0F, 0x0E, 0x0E, 0x0D,
	0x0B, 0x09, 0x08, 0x08, 0x07, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x08,
	0x09, 0x0A, 0x0C, 0x0E, 0x11, 0x14, 0x17, 0x1A, 0x1E, 0x22, 0x26, 0x2A, 0x2D, 0x31, 0x35, 0x3A,
	0x3D, 0x41, 0x45, 0x48, 0x4A, 0x4C, 0x4D, 0x4E, 0x4F, 0x4E, 0x4E, 0x4D, 0x4C, 0x4A, 0x48, 0x46,
	0x43, 0x41, 0x3E, 0x3A, 0x38, 0x36, 0x34, 0x33, 0x31, 0x2F, 0x2D, 0x2B, 0x2A, 0x28, 0x26, 0x24,
	0x23, 0x23, 0x23, 0x22, 0x23, 0x23, 0x23, 0x24, 0x25, 0x26, 0x27, 0x27, 0x28, 0x29, 0x29, 0x29,
	0x28, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x23, 0x22, 0x22, 0x22, 0x21, 0x21, 0x20, 0x20,
	0x1F, 0x1E, 0x1E, 0x1D, 0x1B, 0x1B, 0x1A, 0x1A, 0x19, 0x18, 0x18, 0x17, 0x16, 0x15, 0x14, 0x14,
	0x13, 0x12, 0x12, 0x12, 0x11, 0x11, 0x10, 0x10, 0x0F, 0x0E, 0x0E, 0x0D, 0x0D, 0x0C, 0x0C, 0x0B,
	0x0A, 0x0A, 0x09, 0x08, 0x07, 0x07, 0x06, 0x06, 0x06, 0x06, 0x07, 0x08, 0x0B, 0x0D, 0x10, 0x14,
	0x17, 0x1B, 0x1F, 0x22, 0x26, 0x2A, 0x2E, 0x32, 0x36, 0x3A, 0x3E, 0x41, 0x44, 0x47, 0x4A, 0x4C,
	0x4D, 0x4E, 0x4F, 0x4F, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A, 0x48, 0x46, 0x43, 0x41, 0x3E, 0x39, 0x37,
	0x34, 0x32, 0x30, 0x2E, 0x2C, 0x2A, 0x28, 0x27, 0x25, 0x24, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23,
	0x23, 0x24, 0x24, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25,
};

IMPLEMENT_DYNCREATE(UartDataThread, CWinThread)
unsigned char buf[100];
UartDataThread::UartDataThread()
{

	m_u16RevBuffR = 0, m_u16RevBuffW = 0, m_u16RevBuffLength = 0;
	m_i32CmdAckFlag = 0;
	m_pUart = new Uart();
	m_i32Exit = 0;
	/*HANDLE hOpenFile = (HANDLE)CreateFile(L"E:\\a.text", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
	if (hOpenFile == INVALID_HANDLE_VALUE)
	{
	hOpenFile = NULL;
	MessageBoxA(NULL, "Can not open the file", "Playwav", MB_OK);
	}*/
	m_i32SCommandR = 0, m_i32SCommandW = 0, m_i32SCommandL = 0;
	u32SendCmdTime = 0;
	m_u8SendCmdLength = 0, m_u8SendCmdType = 0, m_i32SendCurIndex = 0, m_u8Timeout = 0, m_u8SendAckFlag = 1, m_u8SendEndFlag = 1;
}

UartDataThread::~UartDataThread()
{

}

BOOL UartDataThread::InitInstance()
{
	// TODO:    在此执行任意逐线程初始化
	int i = 0;

	for (i = 0; i < 7; i++)
	{
		m_stRevECGData.pECGData[i] = new UINT16[64];
	}
	m_stRevECGData.i32Length = 64;

	m_stRevRespData.pRespData = new UINT8[64];
	m_stRevRespData.i32Length = 64;

	m_stRevOxyData.pPlethData = new UINT8[64];
	m_stRevOxyData.i32Length = 64;

	//打开串口
	m_pUart->Open_Uart();
	return TRUE;
}

int UartDataThread::ExitInstance()
{
	// TODO:    在此执行任意逐线程清理
	int i = 0;
	for (i = 0; i < 7; i++)
	{
		delete m_stRevECGData.pECGData[i];
	}
	delete m_stRevRespData.pRespData;
	delete m_stRevOxyData.pPlethData;
	m_pUart->Close_Uart();
	delete m_pUart;
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(UartDataThread, CWinThread)
END_MESSAGE_MAP()



//串口数据解包
int UartDataThread::UnPackData(unsigned char *  p, short int Index, int  length)
{
	unsigned short int head = 0;
	unsigned char   *pack, temp;
	int  i;
	unsigned char checksum;

	if (length < 2)
		return -1;
	pack = (unsigned char *)(p + Index);
	checksum = *(pack);
	if (length > 3)
	{
		head = ((*(unsigned char *)((p + ((1 + Index)&(REV_BUFF_LENGTH - 1))))) & 0x7f) << 7;
		head |= (*(unsigned char *)((p + ((2 + Index)&(REV_BUFF_LENGTH - 1))))) & 0x7f;
		checksum += (*(unsigned char *)((p + ((1 + Index)&(REV_BUFF_LENGTH - 1)))));
		checksum += (*(unsigned char *)((p + ((2 + Index)&(REV_BUFF_LENGTH - 1)))));
		for (i = 1; i < length - 3; i++)
		{
			temp = (*(unsigned char *)((p + ((i + 2 + Index)&(REV_BUFF_LENGTH - 1)))));
			checksum += temp;
			*(unsigned char *)((p + ((i + Index)&(REV_BUFF_LENGTH - 1)))) = (temp & 0x7f) | ((head & 0x1) << 7);
			head >>= 1;
		}
	}
	if ((checksum & 0x7f) != ((*(p + ((length - 1 + Index)&(REV_BUFF_LENGTH - 1)))) & 0x7f))
		return -2;
	return 0;
}

//串口数据组包
int UartDataThread::PackData(unsigned char* pack, int length)
{
	unsigned short int head = 0;
	int i;
	unsigned char checksum;

	if (length < 3)
		return 0;

	checksum = *(pack);
	if (length > 3)
	{
		head = 0;
		for (i = length - 2; i > 2; i--)
		{
			head <<= 1;

			*(pack + i) = *(pack + i - 2) | 0x80;
			checksum += *(pack + i);
			head |= (*(pack + i - 2) & 0x80) >> 7;
		}
	}
	*(pack + 1) = (((head & 0x3F80) >> 7) | 0x80) & 0xff;
	*(pack + 2) = ((head & 0x7F) | 0x80) & 0xff;
	checksum += *(pack + 1);
	checksum += *(pack + 2);
	*(pack + length - 1) = checksum | 0x80;
	return 1;
}


void UartDataThread::Adjust_RevBuff_RLength(unsigned short int l)
{
	m_u16RevBuffR = (m_u16RevBuffR + l) & (REV_BUFF_LENGTH - 1);
	m_u16RevBuffLength -= l;
}


void UartDataThread::err_check()
{
	while (m_u16RevBuffLength > 0)
	{
		if ((((m_u8RevBuff[m_u16RevBuffR]) & 0x80)) == 0)
			break;
		m_u16RevBuffLength--;
		m_u16RevBuffR = (m_u16RevBuffR + 1) & (REV_BUFF_LENGTH - 1);
	}
}

//串口接收到的数据 ，进行解析
void UartDataThread::RevDataAnalyse()
{
	struct ECG_PATTERN a_stECG_PATTERN;


	unsigned short int  a_u16ECGWave[16], i, j, a_u16Temp1, a_u16Temp2, a_u16Temp3, a_u16Temp4, a_u16Temp5, a_u16Temp6, a_u16Temp7, a_u16Temp8, a_u16Temp9, a_u16Temp10, a_u16Temp11, a_u16Temp12;
	INT16 a_i16Temp1, a_i16Temp2, a_i16Temp3, a_i16Temp4;
	INT8 a_i8Temp1;
	short int a_u16I, a_u16II, a_u16III, a_u16AVR, a_u16AVF, a_u16AVL, a_u16V1;
	short int a_u16I2, a_u16II2, a_u16III2, a_u16AVR2, a_u16AVF2, a_u16AVL2, a_u16V2;
	short int a_u16I3, a_u16II3, a_u16III3, a_u16AVR3, a_u16AVF3, a_u16AVL3, a_u16V3;
	UINT8 a_u8RespWave[3], a_u8PlethWave[3];
	UINT8 a_u8Temp = 0, a_u8Temp2 = 0;
	INT8 a_iTemp = 0;


	while (m_u16RevBuffLength >= 1)
	{
		switch (m_u8RevBuff[m_u16RevBuffR])
		{

		case ID_CMD_ACK:
			if (m_u16RevBuffLength >= ID_CMD_ACK_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_CMD_ACK_LENGTH) == 0)
				{
					if ((m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)] == m_u8SendCmdType) &&
						((m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)] == 0) || (m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)] == 5)))
					{
						m_u8SendAckFlag = 1;
					}
					Adjust_RevBuff_RLength(ID_CMD_ACK_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

		case ID_ECGWAVE:
			if (m_u16RevBuffLength >= ID_ECGWAVE_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_ECGWAVE_LENGTH) == 0)
				{
					UINT16 a_u16ECGWave[21];

					//第一个心电点
					a_u16Temp1 = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp2 = m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp3 = m_u8RevBuff[(m_u16RevBuffR + 3)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp4 = m_u8RevBuff[(m_u16RevBuffR + 4)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp5 = m_u8RevBuff[(m_u16RevBuffR + 5)&(REV_BUFF_LENGTH - 1)];


					//III=II-I
					//avR=-(I+II) avL=I-II/2 avF=II-I/2
					//avR=-(I+II)= 2048-((I-2048)+(II-2048))/2
					//avL=I-II/2= 2048+((I-2048)-(II-2048)/2)
					//avF=II-I/2= 2048+((II-2048)-(I-2048)/2)
					a_u16I = a_u16Temp1 + ((a_u16Temp2 & 0x0f) << 8);
					a_u16II = ((a_u16Temp2 & 0xf0) >> 4) + ((a_u16Temp3 & 0xff) << 4);
					a_u16V1 = a_u16Temp4 + ((a_u16Temp5 & 0x0f) << 8);

					//第2个心电点
					a_u16Temp1 = m_u8RevBuff[(m_u16RevBuffR + 5)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp2 = m_u8RevBuff[(m_u16RevBuffR + 6)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp3 = m_u8RevBuff[(m_u16RevBuffR + 7)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp4 = m_u8RevBuff[(m_u16RevBuffR + 8)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp5 = m_u8RevBuff[(m_u16RevBuffR + 9)&(REV_BUFF_LENGTH - 1)];


					a_u16I2 = ((a_u16Temp1 & 0xf0) >> 4) + ((a_u16Temp2 & 0xff) << 4);
					a_u16II2 = a_u16Temp3 + ((a_u16Temp4 & 0x0f) << 8);
					a_u16V2 = ((a_u16Temp4 & 0xf0) >> 4) + ((a_u16Temp5 & 0xff) << 4);

					//第3个心电点
					a_u16Temp1 = m_u8RevBuff[(m_u16RevBuffR + 10)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp2 = m_u8RevBuff[(m_u16RevBuffR + 11)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp3 = m_u8RevBuff[(m_u16RevBuffR + 12)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp4 = m_u8RevBuff[(m_u16RevBuffR + 13)&(REV_BUFF_LENGTH - 1)];
					a_u16Temp5 = m_u8RevBuff[(m_u16RevBuffR + 14)&(REV_BUFF_LENGTH - 1)];


					a_u16I3 = a_u16Temp1 + ((a_u16Temp2 & 0x0f) << 8);
					a_u16II3 = ((a_u16Temp2 & 0xf0) >> 4) + ((a_u16Temp3 & 0xff) << 4);
					a_u16V3 = a_u16Temp4 + ((a_u16Temp5 & 0x0f) << 8);


					a_u16ECGWave[0] = a_u16I;
					a_u16ECGWave[1] = a_u16II;
					a_u16ECGWave[2] = a_u16II - a_u16I;
					a_u16ECGWave[3] = 2048 + ((a_u16II - 2048) - (a_u16I - 2048) / 2);
					a_u16ECGWave[4] = 2048 + ((a_u16I - 2048) - (a_u16II - 2048) / 2);
					a_u16ECGWave[5] = 2048 - ((a_u16I - 2048) + (a_u16II - 2048)) / 2;
					a_u16ECGWave[6] = a_u16V1;

					a_u16ECGWave[7] = a_u16I2;
					a_u16ECGWave[8] = a_u16II2;
					a_u16ECGWave[9] = a_u16II2 - a_u16I2;
					a_u16ECGWave[10] = 2048 + ((a_u16II2 - 2048) - (a_u16I2 - 2048) / 2);
					a_u16ECGWave[11] = 2048 + ((a_u16I2 - 2048) - (a_u16II2 - 2048) / 2);
					a_u16ECGWave[12] = 2048 - ((a_u16I2 - 2048) + (a_u16II2 - 2048)) / 2;
					a_u16ECGWave[13] = a_u16V2;

					a_u16ECGWave[14] = a_u16I3;
					a_u16ECGWave[15] = a_u16II3;
					a_u16ECGWave[16] = a_u16II3 - a_u16I3;
					a_u16ECGWave[17] = 2048 + ((a_u16II3 - 2048) - (a_u16I3 - 2048) / 2);
					a_u16ECGWave[18] = 2048 + ((a_u16I3 - 2048) - (a_u16II3 - 2048) / 2);
					a_u16ECGWave[19] = 2048 - ((a_u16I3 - 2048) + (a_u16II3 - 2048)) / 2;
					a_u16ECGWave[20] = a_u16V3;


					if (a_u16Temp5 & 0x40)
					{
						theApp.m_Data->Add_Beat(1);
					}


					theApp.m_Data->AddECGWave(a_u16ECGWave);
					theApp.m_Data->AddECGWaveSaveData(a_u16ECGWave);


					//theApp.m_Data->AddData2CaptureBuf(&m_stRevECGData);
					//theApp.m_Data->AddData2SaveBuf(&m_stRevECGData);
					//theApp.m_Data->AddData2PrintBuf(&m_stRevECGData);

					Adjust_RevBuff_RLength(ID_ECGWAVE_LENGTH);

				}
				else
				{
					err_check();
				}
			}
			break;

			//心电导联连接和信号信息
		case ID_LEAD_STS:
			if (m_u16RevBuffLength >= ID_LEAD_STS_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_LEAD_STS_LENGTH) == 0)
				{
					a_u8Temp = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];
					a_u8Temp2 = m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_Lead_STS(a_u8Temp, a_u8Temp2);

					Adjust_RevBuff_RLength(ID_LEAD_STS_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

			//心率
		case ID_HR:
			if (m_u16RevBuffLength >= ID_HR_LENGTH_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_HR_LENGTH_LENGTH) == 0)
				{
					a_i16Temp1 = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)] << 8;
					a_i16Temp1 += m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_HR(a_i16Temp1);

					Adjust_RevBuff_RLength(ID_HR_LENGTH_LENGTH);

				}
				else
				{
					err_check();
				}
			}
			break;

		case ID_ECG_PCVs:
			if (m_u16RevBuffLength >= ID_ECG_PCVs_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_ECG_PCVs_LENGTH) == 0)
				{
					a_u8Temp = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_PVCs(a_u8Temp);

					Adjust_RevBuff_RLength(ID_ECG_PCVs_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

		case ID_ECG_CORR_ALARM:
			if (m_u16RevBuffLength >= ID_ECG_CORR_ALARM_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_ECG_CORR_ALARM_LENGTH) == 0)
				{
					a_u8Temp = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_ARRA(a_u8Temp);

					Adjust_RevBuff_RLength(ID_ECG_CORR_ALARM_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

			//ST 值
		case ID_ST:
			if (m_u16RevBuffLength >= ID_ST_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_ST_LENGTH) == 0)
				{
					a_i16Temp1 = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)] << 8;
					a_i16Temp1 += m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)];
					a_i16Temp2 = m_u8RevBuff[(m_u16RevBuffR + 3)&(REV_BUFF_LENGTH - 1)] << 8;
					a_i16Temp2 += m_u8RevBuff[(m_u16RevBuffR + 4)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_ST(a_i16Temp1, a_i16Temp2);

					Adjust_RevBuff_RLength(ID_ST_LENGTH);

				}
				else
				{
					err_check();
				}
			}
			break;

			//ST 模板波形
		case ID_ST_PATTERN:
			if (m_u16RevBuffLength >= ID_ST_PATTERN_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_ST_PATTERN_LENGTH) == 0)
				{
					a_stECG_PATTERN.num = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];
					a_stECG_PATTERN.data[0] = m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)];
					a_stECG_PATTERN.data[1] = m_u8RevBuff[(m_u16RevBuffR + 3)&(REV_BUFF_LENGTH - 1)];
					a_stECG_PATTERN.data[2] = m_u8RevBuff[(m_u16RevBuffR + 4)&(REV_BUFF_LENGTH - 1)];
					a_stECG_PATTERN.data[3] = m_u8RevBuff[(m_u16RevBuffR + 5)&(REV_BUFF_LENGTH - 1)];
					a_stECG_PATTERN.data[4] = m_u8RevBuff[(m_u16RevBuffR + 6)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_Pattern(&a_stECG_PATTERN);

					Adjust_RevBuff_RLength(ID_ST_PATTERN_LENGTH);

				}
				else
				{
					err_check();
				}
			}
			break;

			//呼吸波形数据
		case ID_RESPWAVE:
			if (m_u16RevBuffLength >= ID_RESPWAVE_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_RESPWAVE_LENGTH) == 0)
				{
					a_u8RespWave[0] = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];
					a_u8RespWave[1] = m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)];
					a_u8RespWave[2] = m_u8RevBuff[(m_u16RevBuffR + 3)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->AddRespWave(a_u8RespWave);
					theApp.m_Data->AddRespWaveSaveData(a_u8RespWave);
					Adjust_RevBuff_RLength(ID_RESPWAVE_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

			//呼吸率
		case ID_RR:
			if (m_u16RevBuffLength >= ID_RR_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_RR_LENGTH) == 0)
				{
					a_i16Temp1 = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)] << 8;
					a_i16Temp1 += m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_RR(a_i16Temp1);

					Adjust_RevBuff_RLength(ID_RR_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

			//窒息报警
		case ID_APNEA:
			if (m_u16RevBuffLength >= ID_APNEA_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_APNEA_LENGTH) == 0)
				{
					a_u8Temp = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];
					theApp.m_Data->Add_APNEA(a_u8Temp);

					Adjust_RevBuff_RLength(ID_APNEA_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;


			//体温数据
		case ID_TEMP:
			if (m_u16RevBuffLength >= ID_TEMP_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_TEMP_LENGTH) == 0)
				{
					a_u8Temp = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];
					a_i16Temp1 = m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)] << 8;
					a_i16Temp1 += m_u8RevBuff[(m_u16RevBuffR + 3)&(REV_BUFF_LENGTH - 1)];
					a_i16Temp2 = m_u8RevBuff[(m_u16RevBuffR + 4)&(REV_BUFF_LENGTH - 1)] << 8;
					a_i16Temp2 += m_u8RevBuff[(m_u16RevBuffR + 5)&(REV_BUFF_LENGTH - 1)];
					theApp.m_Data->Add_TempAlm(a_u8Temp);
					theApp.m_Data->Add_Temp(a_i16Temp1);

					Adjust_RevBuff_RLength(ID_TEMP_LENGTH);

				}
				else
				{
					err_check();
				}
			}
			break;

		case ID_SPO2_WAVE:
			if (m_u16RevBuffLength >= ID_SPO2_WAVE_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_SPO2_WAVE_LENGTH) == 0)
				{
					a_u8Temp2 = m_u8RevBuff[(m_u16RevBuffR + 4)&(REV_BUFF_LENGTH - 1)];


					a_u8PlethWave[0] = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];
					a_u8PlethWave[1] = m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)];
					a_u8PlethWave[2] = m_u8RevBuff[(m_u16RevBuffR + 3)&(REV_BUFF_LENGTH - 1)];
					theApp.m_Data->Add_OxyAlm(a_u8Temp2 & 0x90);
					theApp.m_Data->Add_OxyBar(a_u8Temp2 & 0x0f);
					theApp.m_Data->AddOxyWave(a_u8PlethWave);
					theApp.m_Data->AddPlethWaveSaveData(a_u8PlethWave);
					Adjust_RevBuff_RLength(ID_SPO2_WAVE_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

		case ID_SPO2_PR:
			if (m_u16RevBuffLength >= ID_SPO2_PR_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_SPO2_PR_LENGTH) == 0)
				{
					a_u8Temp = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];
					a_i16Temp1 = m_u8RevBuff[(m_u16RevBuffR + 2)&(REV_BUFF_LENGTH - 1)] << 8;
					a_i16Temp1 += m_u8RevBuff[(m_u16RevBuffR + 3)&(REV_BUFF_LENGTH - 1)];
					a_i8Temp1 = m_u8RevBuff[(m_u16RevBuffR + 4)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_Spo2(a_i8Temp1);
					theApp.m_Data->Add_PR(a_i16Temp1);

					Adjust_RevBuff_RLength(ID_SPO2_PR_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;


		case ID_AIROXY_VALUE:
			if (m_u16RevBuffLength >= ID_AIROXY_VALUE_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_AIROXY_VALUE_LENGTH) == 0)
				{
					a_iTemp = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_AirOxy(a_iTemp);
					Adjust_RevBuff_RLength(ID_AIROXY_VALUE_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

		case ID_AIROXY_STS:
			if (m_u16RevBuffLength >= ID_AIROXY_STS_LENGTH)
			{
				if (UnPackData(m_u8RevBuff, m_u16RevBuffR, ID_AIROXY_STS_LENGTH) == 0)
				{
					a_u8Temp = m_u8RevBuff[(m_u16RevBuffR + 1)&(REV_BUFF_LENGTH - 1)];

					theApp.m_Data->Add_AirOxyAlm(a_u8Temp);
					Adjust_RevBuff_RLength(ID_AIROXY_STS_LENGTH);
				}
				else
				{
					err_check();
				}
			}
			break;

		default:
			Adjust_RevBuff_RLength(1);
			break;
		}

	}

}


int UartDataThread::GetData()
{
	unsigned char buf[64];
	unsigned short int l = 0, i = 0;
	//	l = m_pUart->Rev(buf, 64);
	if (l)
	{
		for (i = 0; i < l; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += l;
	}
	return 0;
}


void UartDataThread::do_send_data()
{

}

int UartDataThread::SendData(INT *state)
{
	unsigned short int a_u16CmdBuffLength = 0;
	UINT32 a_u32NowTime = 0;

	a_u32NowTime = GetTickCount();

	m_csSendCmd.Lock();
	/** * @b 判断当前命令是否发送完毕*/
	if ((m_u8SendEndFlag == 1))
	{
		/** * @b 当前命令发送完毕*/
		if (m_u8SendAckFlag == 0)
		{
			/** * @b 当前命令没有收到采集盒回复数据包*/
			if (a_u32NowTime - u32SendCmdTime >= 500)
			{
				/** * @b 等待采集盒回复数据包超时*/
				if (m_u8Timeout < 3)
				{
					/** * @b 超时的次数小于3次*/
					u32SendCmdTime = a_u32NowTime;
					m_u8Timeout++;
					m_u8SendEndFlag = 0;
					m_i32SendCurIndex = 0;

					*state = 0xee;
					//st->iSendTime = 0;
				}
				else
				{
					/** * @b 超时的次数5次，将会重新发起蓝牙连接*/
					*state = 0xff;
					m_u8Timeout = 0;
				}
			}
		}
		else
		{


			/** * @b 当前命令收到采集盒回复数据包*/
			/** * @b 则可以向采集盒发送下一条命令了*/
			/** * @b 向采集盒发送命令的优先级为，设置命令、获得状态命令、获得实时数据命令*/
			m_u8Timeout = 0;

			if (m_i32SCommandL)
			{
				/** * @b
				设置命令的缓冲器非空，并且距上次发送已经间隔一定时间
				则将发送"设置命令"

				*/
				/** * @b 保存本次发送时间*/
				u32SendCmdTime = a_u32NowTime;
				/** * @b 保存本次发送数据包类型2*/
				//st->sU2SType2 = 3;
				/** * @b 保存本次发送数据包的长度*/
				m_u8SendCmdLength = m_stSCommand[m_i32SCommandR].Length;
				/** * @b 将本次发送数据包复制到发送缓冲区中*/
				memcpy(m_u8SendCmdBuf, m_stSCommand[m_i32SCommandR].Data, m_u8SendCmdLength);
				/** * @b 保存本次发送数据包类型*/
				m_u8SendCmdType = m_stSCommand[m_i32SCommandR].Type;
				/** * @b 设置发送缓冲区的读指针*/
				m_i32SendCurIndex = 0;
				/** * @b 当前命令是否发送完毕标志复位*/
				m_u8SendEndFlag = 0;
				u32SendCmdTime = a_u32NowTime;
				m_u8Timeout = 0;
				/** * @b 当前命令是否收到采集盒回复数据包标志复位*/
				m_u8SendAckFlag = 0;
				m_i32SCommandL--;
				m_i32SCommandR = (m_i32SCommandR + 1) % SCOMMAND_LENGTH;

				*state = 1;
			}

		}
	}
	m_csSendCmd.Unlock();

	return 0;
}

//向下位机发送命令
int UartDataThread::SendCmd(unsigned char *buff, unsigned char type, int length)
{
	int a_i32SCommandW, a_i32SCommandL;

	m_csSendCmd.Lock();
	if (m_i32SCommandL < SCOMMAND_LENGTH)
	{
		SCommand* p_st;

		//如果缓冲区未满
		p_st = &(m_stSCommand[m_i32SCommandW]);
		//将命令拷贝的缓冲区
		memcpy(p_st->Data, buff, length);
		//保存命令长度
		p_st->Length = length;
		//保存命令类型
		p_st->Type = type;
		//更新写指针
		m_i32SCommandW = (m_i32SCommandW + 1) % SCOMMAND_LENGTH;
		m_i32SCommandL++;
		m_csSendCmd.Unlock();

		return 0;
	}
	m_csSendCmd.Unlock();


	return 1;
}


int UartDataThread::PutECGData()
{
	unsigned char a_buf[400];
	int i = 0;
	int a_i32Temp = 0;
	UINT16 a_u16HR = 0;
	INT16 a_i16ST = 0, a_i32Beat = 0, a_i32Pace = 0;
	UINT8 a_u8LeadOff = 0, m_u8ARRA = 0;
	static int index = 0, timer = 0;
	static int a = 0;


	for (i = 0; i < 8; i++)
	{
		a_buf[i * ID_ECGWAVE_LENGTH + 0] = ID_ECGWAVE;
		a_i32Temp = In_ECG_Buf[index * 2];
		//a_i32Temp = a;
		a += 4;
		if (a == 2048)
			a = 0;
		//I II
		a_buf[i * ID_ECGWAVE_LENGTH + 1] = a_i32Temp & 0xff;
		a_buf[i * ID_ECGWAVE_LENGTH + 2] = ((a_i32Temp >> 8) & 0x0f) | ((a_i32Temp & 0x0f) << 4);
		a_buf[i * ID_ECGWAVE_LENGTH + 3] = (a_i32Temp >> 4) & 0xff;
		//V1 
		a_buf[i * ID_ECGWAVE_LENGTH + 4] = a_i32Temp & 0xff;
		a_buf[i * ID_ECGWAVE_LENGTH + 5] = ((a_i32Temp >> 8) & 0x0f);

		if (In_ECG_Buf[index * 2 + 1] == 1)
			a_i32Beat |= 0x40;



		index = (index + 1) % (249);
		a_i32Temp = In_ECG_Buf[index * 2];

		a_buf[i * ID_ECGWAVE_LENGTH + 5] |= ((a_i32Temp & 0x0f) << 4);
		a_buf[i * ID_ECGWAVE_LENGTH + 6] = (a_i32Temp >> 4) & 0xff;
		a_buf[i * ID_ECGWAVE_LENGTH + 7] = (a_i32Temp)& 0xff;
		a_buf[i * ID_ECGWAVE_LENGTH + 8] = ((a_i32Temp >> 8) & 0x0f) | ((a_i32Temp & 0x0f) << 4);
		a_buf[i * ID_ECGWAVE_LENGTH + 9] = (a_i32Temp >> 4) & 0xff;

		if (In_ECG_Buf[index * 2 + 1] == 1)
			a_i32Beat |= 0x40;



		

		a_i32Temp = In_ECG_Buf[index * 2];
		//I II
		a_buf[i * ID_ECGWAVE_LENGTH + 10] = a_i32Temp & 0xff;
		a_buf[i * ID_ECGWAVE_LENGTH + 11] = ((a_i32Temp >> 8) & 0x0f) | ((a_i32Temp & 0x0f) << 4);
		a_buf[i * ID_ECGWAVE_LENGTH + 12] = (a_i32Temp >> 4) & 0xff;
		//V1 
		a_buf[i * ID_ECGWAVE_LENGTH + 13] = a_i32Temp & 0xff;
		a_buf[i * ID_ECGWAVE_LENGTH + 14] = ((a_i32Temp >> 8) & 0x0f);

		if (In_ECG_Buf[index * 2 + 1] == 1)
			a_i32Beat |= 0x40;
		a_buf[i * ID_ECGWAVE_LENGTH + 14] |= a_i32Beat;


		index = (index + 1) % (249);
		//if (timer % 10 == 0)
		//	a_buf[i * ID_ECGWAVE_LENGTH + 14] = 0x40;

		PackData(a_buf + i * ID_ECGWAVE_LENGTH, ID_ECGWAVE_LENGTH);

	}
	for (i = 0; i < ID_ECGWAVE_LENGTH * 8; i++)
	{
		m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
		m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
	}
	m_u16RevBuffLength += ID_ECGWAVE_LENGTH * 8;

	if (++timer % 20 == 0)
	{
		theApp.m_CSCom.Lock();
		a_u16HR = theApp.m_i32HR;
		a_i16ST = theApp.m_i32ST1;
		a_u8LeadOff = theApp.m_u8LeadOff, m_u8ARRA = theApp.m_u8ARRA;
		theApp.m_CSCom.Unlock();


		a_buf[0] = ID_LEAD_STS;
		a_buf[1] = theApp.m_u8LeadOff;
		a_buf[2] = 0;
		PackData(a_buf, ID_LEAD_STS_LENGTH);
		for (i = 0; i < ID_LEAD_STS_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_LEAD_STS_LENGTH;


		a_buf[0] = ID_HR;
		a_buf[1] = a_u16HR >> 8;
		a_buf[2] = a_u16HR & 0xff;
		PackData(a_buf, ID_HR_LENGTH_LENGTH);
		for (i = 0; i < ID_HR_LENGTH_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_HR_LENGTH_LENGTH;

		a_buf[0] = ID_ST;
		a_buf[1] = a_i16ST >> 8;
		a_buf[2] = a_i16ST & 0xff;
		a_buf[3] = a_i16ST >> 8;
		a_buf[4] = a_i16ST & 0xff;
		PackData(a_buf, ID_ST_LENGTH);
		for (i = 0; i < ID_ST_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_ST_LENGTH;

		a_buf[0] = ID_LEAD_STS;
		a_buf[1] = a_u8LeadOff;
		a_buf[2] = 0;
		PackData(a_buf, ID_LEAD_STS_LENGTH);
		for (i = 0; i < ID_LEAD_STS_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_LEAD_STS_LENGTH;

		a_buf[0] = ID_ECG_CORR_ALARM;
		a_buf[1] = m_u8ARRA;
		a_buf[2] = 0;
		PackData(a_buf, ID_ECG_CORR_ALARM_LENGTH);
		for (i = 0; i < ID_ECG_CORR_ALARM_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_ECG_CORR_ALARM_LENGTH;
	}

	return 0;
}


int UartDataThread::PutRespData()
{
	unsigned char a_buf[200];
	int i = 0;
	int a_i32Temp = 0;
	UINT16 a_u16RR = 0;
	UINT8 a_u8APNEA = 0;
	static int index = 0, timer = 0;
	static int a = 0;
	for (i = 0; i < 2; i++)
	{
		a_buf[i * ID_RESPWAVE_LENGTH + 0] = ID_RESPWAVE;
		a_i32Temp = In_RESP_Buf[index];
		//a_i32Temp = a;
		if (++a == 256)
			a = 0;
		//I II
		a_buf[i * ID_RESPWAVE_LENGTH + 1] = a_i32Temp & 0xff;
		index = (index + 1) % (375);

		a_i32Temp = In_RESP_Buf[index];
		a_buf[i * ID_RESPWAVE_LENGTH + 2] = a_i32Temp & 0xff;
		index = (index + 1) % (375);

		a_i32Temp = In_RESP_Buf[index];
		a_buf[i * ID_RESPWAVE_LENGTH + 3] = a_i32Temp & 0xff;
		index = (index + 1) % (375);

		PackData(a_buf + i * ID_RESPWAVE_LENGTH, ID_RESPWAVE_LENGTH);
	}
	for (i = 0; i < 2 * ID_RESPWAVE_LENGTH; i++)
	{
		m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
		m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
	}
	m_u16RevBuffLength += 2 * ID_RESPWAVE_LENGTH;


	if (++timer % 20 == 0)
	{
		theApp.m_CSCom.Lock();
		a_u16RR = theApp.m_i16RR;
		a_u8APNEA = theApp.m_u8APNEA;
		theApp.m_CSCom.Unlock();

		a_buf[0] = ID_RR;
		a_buf[1] = a_u16RR >> 8;
		a_buf[2] = a_u16RR & 0xff;
		PackData(a_buf, ID_RR_LENGTH);
		for (i = 0; i < ID_RR_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_RR_LENGTH;


		a_buf[0] = ID_APNEA;
		a_buf[1] = a_u8APNEA;
		a_buf[2] = 0;
		a_buf[3] = 0;
		PackData(a_buf, ID_APNEA_LENGTH);
		for (i = 0; i < ID_APNEA_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_APNEA_LENGTH;
	}


	return 0;
}


int UartDataThread::PutOxyData()
{
	unsigned char a_buf[200];
	int i = 0;
	int a_i32Temp = 0;
	UINT16 a_u16PR = 0;
	UINT8	a_u8Spo2 = 0, a_u8SPO2Alm = 0;
	static int index = 0, timer = 0;
	static int a = 0;

	theApp.m_CSCom.Lock();
	a_u8SPO2Alm = theApp.m_u8SPO2Alm;
	theApp.m_CSCom.Unlock();

	for (i = 0; i < 2; i++)
	{
		a_buf[i * ID_SPO2_WAVE_LENGTH + 0] = ID_SPO2_WAVE;
		a_i32Temp = In_Oxy_Buf[index];
		//a_i32Temp = a;
		if (++a == 250)
			a = 0;
		//I II
		a_buf[i * ID_SPO2_WAVE_LENGTH + 1] = a_i32Temp & 0xff;
		index = (index + 1) % (250);

		a_i32Temp = In_Oxy_Buf[index];
		a_buf[i * ID_SPO2_WAVE_LENGTH + 2] = a_i32Temp & 0xff;
		index = (index + 1) % (250);

		a_i32Temp = In_Oxy_Buf[index];
		a_buf[i * ID_SPO2_WAVE_LENGTH + 3] = a_i32Temp & 0xff;
		index = (index + 1) % (250);

		if (a_i32Temp > 96)
			a_buf[i * ID_SPO2_WAVE_LENGTH + 4] = 15;
		else
			a_buf[i * ID_SPO2_WAVE_LENGTH + 4] = a_i32Temp / 6;
		a_buf[i * ID_SPO2_WAVE_LENGTH + 4] |= (a_u8SPO2Alm & 0xf0);
		PackData(a_buf + i * ID_SPO2_WAVE_LENGTH, ID_SPO2_WAVE_LENGTH);

	}
	for (i = 0; i < 2 * ID_SPO2_WAVE_LENGTH; i++)
	{
		m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
		m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
	}
	m_u16RevBuffLength += 2 * ID_SPO2_WAVE_LENGTH;

	if (++timer % 20 == 0)
	{

		theApp.m_CSCom.Lock();
		a_u16PR = theApp.m_i32PR;
		a_u8Spo2 = theApp.m_i32SPO2;
		theApp.m_CSCom.Unlock();

		a_buf[0] = ID_SPO2_PR;
		a_buf[1] = 0;
		a_buf[2] = a_u16PR >> 8;
		a_buf[3] = a_u16PR & 0xff;
		a_buf[4] = a_u8Spo2;
		PackData(a_buf, ID_SPO2_PR_LENGTH);
		for (i = 0; i < ID_SPO2_PR_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_SPO2_PR_LENGTH;

	}


	return 0;

}

int UartDataThread::PutTempData()
{
	unsigned char a_buf[32];
	int i = 0;
	UINT16 a_u16Temp = 0;
	UINT8 a_u8TempAlm = 0;
	static int  timer = 0;


	if (++timer % 25 == 0)
	{
		theApp.m_CSCom.Lock();
		a_u16Temp = theApp.m_i16Temp;
		a_u8TempAlm = theApp.m_u8TempAlm;
		theApp.m_CSCom.Unlock();

		a_buf[0] = ID_TEMP;
		a_buf[1] = a_u8TempAlm;
		a_buf[2] = a_u16Temp >> 8;
		a_buf[3] = a_u16Temp & 0xff;
		a_buf[4] = a_u16Temp >> 8;
		a_buf[5] = a_u16Temp & 0xff;
		PackData(a_buf, ID_TEMP_LENGTH);
		for (i = 0; i < ID_TEMP_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_TEMP_LENGTH;
	}

	return 0;
}


int UartDataThread::PutNIBPData()
{

	return 0;
}


int UartDataThread::PutAirOxyData()
{
	unsigned char a_buf[32];
	int i = 0;
	INT8 a_i8Temp = 0;
	UINT8 a_u8Alm = 0;
	static int  timer = 0;


	if (++timer % 25 == 0)
	{
		theApp.m_CSCom.Lock();
		a_i8Temp = theApp.m_i8AirOxy;
		a_u8Alm = theApp.m_i8AirOxyAlm;
		theApp.m_CSCom.Unlock();

		a_buf[0] = ID_AIROXY_VALUE;
		a_buf[1] = a_i8Temp;

		PackData(a_buf, ID_AIROXY_VALUE_LENGTH);
		for (i = 0; i < ID_AIROXY_VALUE_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_AIROXY_VALUE_LENGTH;


		a_buf[0] = ID_AIROXY_STS;
		a_buf[1] = a_u8Alm;

		PackData(a_buf, ID_AIROXY_STS_LENGTH);
		for (i = 0; i < ID_AIROXY_STS_LENGTH; i++)
		{
			m_u8RevBuff[m_u16RevBuffW] = a_buf[i];
			m_u16RevBuffW = (m_u16RevBuffW + 1)&(REV_BUFF_LENGTH - 1);
		}
		m_u16RevBuffLength += ID_AIROXY_STS_LENGTH;

	}

	return 0;

}


int UartDataThread::init_net(CString ip, UINT port)
{
	/*m_csIP = ip;
	m_u32Port = port;
	if (m_Conn.Create())
	{

	return 1;
	}
	else*/
	return 0;



}


int UartDataThread::net_connect()
{
	/*if (m_Conn.Connect(m_csIP, m_u32Port))
	{
	return 0;
	}
	else*/
	return 1;
}



int UartDataThread::net_close()
{
	//	m_Conn.Close();

	return 0;
}



int UartDataThread::test_net_connect(struct CMD_TEST_NET *st)
{
	return 0;
}



int UartDataThread::do_test_net_connect()
{
	int a_i32CMD_TEST_NET;
	m_CSCMD_TEST_NET.Lock();
	a_i32CMD_TEST_NET = m_i32CMD_TEST_NET;
	m_CSCMD_TEST_NET.Unlock();


	if (m_i32NetFlag == NET_CNT)
	{
		if (a_i32CMD_TEST_NET == 1)
		{
			m_CSCMD_TEST_NET.Lock();
			m_i32CMD_TEST_NET = 2;
			m_CSCMD_TEST_NET.Unlock();
		}

		else if (a_i32CMD_TEST_NET == 2)
		{
			//if (m_stCMD_TEST_NET.pWnd)
			//	m_stCMD_TEST_NET.pWnd->PostMessage(WM_MONITOR_STATUS, CMD_PAT_UPDATE_SUCC, 0);
		}
	}


	return 0;
}



int UartDataThread::submit_net_file()
{
	return 0;
}



int UartDataThread::do_submit_net_file()
{
	return 0;
}



int UartDataThread::close_submit_net_file()
{
	return 0;
}

//病人类型设置 设置病人类型为成人、儿童或新生儿
int UartDataThread::Send_CMD_PatType(UINT8 data)
{
	UINT8 a_u8Buf[16];

	a_u8Buf[0] = CMD_PAT_TYPE;
	a_u8Buf[1] = data;
	//组包
	PackData(a_u8Buf, CMD_PAT_TYPE_LENGTH);
	//发送命令
	SendCmd(a_u8Buf, CMD_PAT_TYPE, CMD_PAT_TYPE_LENGTH);

	return 0;
}

//3 / 5 导联设置 设置 ECG 导联为 5 导联或 3 导联模式
int UartDataThread::Send_CMD_LeadSystem(UINT8 data)
{
	UINT8 a_u8Buf[16];

	a_u8Buf[0] = CMD_LEADSYSTEM;
	a_u8Buf[1] = data;
	//组包
	PackData(a_u8Buf, CMD_LEADSYSTEM_LENGTH);
	//发送命令
	SendCmd(a_u8Buf, CMD_LEADSYSTEM, CMD_LEADSYSTEM_LENGTH);

	return 0;
}

//导联方式设置设置通道 1 或通道 2 的 ECG 导联：
//I、 II、 III、 AVL、 AVR、 AVF、 V
int UartDataThread::Send_CMD_LeadType(UINT8 data)
{
	UINT8 a_u8Buf[16];

	a_u8Buf[0] = CMD_LEADTYPE;
	a_u8Buf[1] = data;
	//组包
	PackData(a_u8Buf, CMD_LEADTYPE_LENGTH);
	//发送命令
	SendCmd(a_u8Buf, CMD_LEADTYPE, CMD_LEADTYPE_LENGTH);

	return 0;
}

//心电滤波方式设置设置通道 1 或通道 2 的 ECG 滤波方
//式：诊断、监护、手术
int UartDataThread::Send_CMD_FilterMode(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_FILTER_MODE;
	a_Buff[1] = data;

	PackData(a_Buff, CMD_FILTER_MODE_LENGTH);
	SendCmd(a_Buff, CMD_FILTER_MODE, CMD_FILTER_MODE_LENGTH);
	return 0;
}

//心电增益设置 ECG 增益：× 0.25、× 0.5、× 1、× 2
int UartDataThread::Send_CMD_ECGGain(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_ECG_GAIN;
	a_Buff[1] = data;

	PackData(a_Buff, CMD_ECG_GAIN_LENGTH);
	SendCmd(a_Buff, CMD_ECG_GAIN, CMD_ECG_GAIN_LENGTH);
	return 0;
}

//心电校准设置 ECG 波形为 1Hz 的校准信号
int UartDataThread::Send_CMD_ECGCal(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_ECG_CAL;
	a_Buff[1] = data;

	PackData(a_Buff, CMD_ECG_CAL_LENGTH);
	SendCmd(a_Buff, CMD_ECG_CAL, CMD_ECG_CAL_LENGTH);

	return 0;
}

//工频干扰抑制开关设置 关/50Hz / 60Hz 工频干扰抑制的开关
int  UartDataThread::Send_CMD_ECGTrap(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_ECG_TRAP;
	a_Buff[1] = data;

	PackData(a_Buff, CMD_ECG_TRAP_LENGTH);
	SendCmd(a_Buff, CMD_ECG_TRAP, CMD_ECG_TRAP_LENGTH);
	return 0;
}

//起搏分析开关 设置起搏分析的开关
int UartDataThread::Send_CMD_ECGPace(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_ECG_PACE;
	a_Buff[1] = data;

	PackData(a_Buff, CMD_ECG_PACE_LENGTH);
	SendCmd(a_Buff, CMD_ECG_PACE, CMD_ECG_PACE_LENGTH);
	return 0;
}

//ST 测量的 ISO、 ST点 设置 ST 计算的 ISO 和 ST 点
int UartDataThread::Send_CMDSTISO(UINT16 iso, UINT16 st)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_ST_ISO;
	a_Buff[1] = ((iso >> 8) & 0xff);
	a_Buff[2] = ((iso >> 0) & 0xff);
	a_Buff[3] = ((st >> 8) & 0xff);
	a_Buff[4] = ((st >> 0) & 0xff);

	PackData(a_Buff, CMD_ST_ISO_LENGTH);
	SendCmd(a_Buff, CMD_ST_ISO, CMD_ST_ISO_LENGTH);
	return 0;
}

//心率计算通道 选择心率计算为I II III V avr avf
int UartDataThread::Send_CMD_ECGChannel(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_ECG_CHANNEL;
	a_Buff[1] = data;

	PackData(a_Buff, CMD_ECG_CHANNEL_LENGTH);
	SendCmd(a_Buff, CMD_ECG_CHANNEL, CMD_ECG_CHANNEL_LENGTH);
	return 0;
}

//心率重新计算 重新计算心率
int UartDataThread::Send_CMD_ECGLearn()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_ECG_LEARN;

	PackData(a_Buff, CMD_ECG_LEARN_LENGTH);
	SendCmd(a_Buff, CMD_ECG_LEARN, CMD_ECG_LEARN_LENGTH);
	return 0;
}

//呼吸增益 设置呼吸增益为：× 0.25、× 0.5、×1、× 2、× 4
int UartDataThread::Send_CMD_RESPGain(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_RESP_GAIN;
	a_Buff[1] = data;

	PackData(a_Buff, CMD_RESP_GAIN_LENGTH);
	SendCmd(a_Buff, CMD_RESP_GAIN, CMD_RESP_GAIN_LENGTH);
	return 0;
}

//窒息报警时间 设 置 呼 吸 窒 息 的 报 警 延 迟 时 间 ：10s～40s
int UartDataThread::Send_CMD_APNEAAlm(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_APNEA_ALM;
	a_Buff[1] = data;

	PackData(a_Buff, CMD_APNEA_ALM_LENGTH);
	SendCmd(a_Buff, CMD_APNEA_ALM, CMD_APNEA_ALM_LENGTH);
	return 0;
}

//体温探头类型 设置体温探头的类型： YSI / CY - F1
int UartDataThread::Send_CMD_TempType(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_TEMP_TYPE;

	PackData(a_Buff, CMD_TEMP_TYPE_LENGTH);
	SendCmd(a_Buff, CMD_TEMP_TYPE, CMD_TEMP_TYPE_LENGTH);
	return 0;
}

//SPO2 设置  设置 SPO2 的测量灵敏度
int UartDataThread::Send_CMD_SPO2(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_SPO2;

	PackData(a_Buff, CMD_SPO2_LENGTH);
	SendCmd(a_Buff, CMD_SPO2, CMD_SPO2_LENGTH);
	return 0;
}

//NBP 启动手动 / 自动测量
//启动一次血压手动测量或自动测量
int UartDataThread::Send_CMD_NBPStart()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_START;

	PackData(a_Buff, CMD_NBP_START_LENGTH);
	SendCmd(a_Buff, CMD_NBP_START, CMD_NBP_START_LENGTH);
	return 0;
}

// NBP 中止测量 结束当前的测量
int UartDataThread::Send_CMD_NBPEnd()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_END;

	PackData(a_Buff, CMD_NBP_END_LENGTH);
	SendCmd(a_Buff, CMD_NBP_END, CMD_NBP_END_LENGTH);
	return 0;
}

// NBP 测量周期 设置血压自动测量的周期
int UartDataThread::Send_CMD_NBPPeriod(UINT8 data)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_PERIOD;

	PackData(a_Buff, CMD_NBP_PERIOD_LENGTH);
	SendCmd(a_Buff, CMD_NBP_PERIOD, CMD_NBP_PERIOD_LENGTH);
	return 0;
}

//NBP 校准  血压进入校准状态
int UartDataThread::Send_CMD_NBPCal()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_CAL;

	PackData(a_Buff, CMD_NBP_CAL_LENGTH);
	SendCmd(a_Buff, CMD_NBP_CAL, CMD_NBP_CAL_LENGTH);
	return 0;
}

//NBP 复位  软件复位血压模块
int UartDataThread::Send_CMD_NBPReset()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_RESET;

	PackData(a_Buff, CMD_NBP_RESET_LENGTH);
	SendCmd(a_Buff, CMD_NBP_RESET, CMD_NBP_RESET_LENGTH);
	return 0;
}

//NBP 漏气检测 血压气路进行漏气检测
int UartDataThread::Send_CMD_NBPPneumatic()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_PNEUMATIC;

	PackData(a_Buff, CMD_NBP_PNEUMATIC_LENGTH);
	SendCmd(a_Buff, CMD_NBP_PNEUMATIC, CMD_NBP_PNEUMATIC_LENGTH);
	return 0;
}

//NBP 查询状态  查询血压模块的状态
int UartDataThread::Send_CMD_NBPPollState()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_POLL_STATE;

	PackData(a_Buff, CMD_NBP_POLL_STATE_LENGTH);
	SendCmd(a_Buff, CMD_NBP_POLL_STATE, CMD_NBP_POLL_STATE_LENGTH);
	return 0;
}

//NBP 设置初次充气压力  设置下次血压测量的首次充气压力
int UartDataThread::Send_CMD_NBPPrePressure(UINT8 type, UINT8 press)
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_PRE_PRESSURE;
	a_Buff[1] = type;
	a_Buff[2] = press;
	PackData(a_Buff, CMD_NBP_PRE_PRESSURE_LENGTH);
	SendCmd(a_Buff, CMD_NBP_PRE_PRESSURE, CMD_NBP_PRE_PRESSURE_LENGTH);
	return 0;
}

//NBP 启动 STAT 测量 开始 5 分钟的 STAT 血压测量
int UartDataThread::Send_CMD_NBPContiune()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_CONTINUE;

	PackData(a_Buff, CMD_NBP_CONTINUE_LENGTH);
	SendCmd(a_Buff, CMD_NBP_CONTINUE, CMD_NBP_CONTINUE_LENGTH);
	return 0;
}

//NBP 查询测量结果
int UartDataThread::Send_CMD_NBPGetResult()
{
	UINT8 a_Buff[16];

	a_Buff[0] = CMD_NBP_GET_RESULT;

	PackData(a_Buff, CMD_NBP_GET_RESULT_LENGTH);
	SendCmd(a_Buff, CMD_NBP_GET_RESULT, CMD_NBP_GET_RESULT_LENGTH);
	return 0;
}




int UartDataThread::Run()
{
	UINT a_u32TimeDelay, a_u32TimeDelay2, a_u32TimeDelayFlag = 0;
	UINT a_u32SendTick = 0, a_u32SendTickNow = 0, a_u32SoundTick=0;
	INT a_i132SendStatus = 0;
	a_u32TimeDelay = GetTickCount();
	a_u32SendTick = a_u32TimeDelay;
	a_u32SoundTick = a_u32TimeDelay;
	while (1)
	{
		a_u32SendTickNow = GetTickCount();
		a_u32TimeDelay2 = a_u32SendTickNow;
		
		if (a_u32TimeDelayFlag == 0)
		{
			//a_u32TimeDelay2 = GetTickCount();
			if (a_u32TimeDelay2 - a_u32TimeDelay > 15 * 1000)
			{
				theApp.m_Data->m_u32Status &= ~(1 << STATUS_TIME_DELAY);
				a_u32TimeDelayFlag = 1;
			}
		}
		//		GetData();
		//		if (theApp.m_i32Connect==1)
		if (a_u32SendTickNow - a_u32SendTick >= 48)
		{
			a_u32SendTick +=48 ;
			PutECGData();
			PutRespData();
			PutOxyData();
			PutTempData();
			PutAirOxyData();
		}
		
		RevDataAnalyse();
		SendData(&a_i132SendStatus);
		if ((a_i132SendStatus == 0xee) || (a_i132SendStatus == 1))
		{
			do_send_data();
		}
		if (m_i32Exit)
		{
			return ExitInstance();
		}
		Sleep(4);
	}
	return 0;
}


// UartDataThread 消息处理程序
