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

📄 serialport.~cpp

📁 符合移动协议的见空系统,很有使用简直,希望多下载
💻 ~CPP
📖 第 1 页 / 共 2 页
字号:
    if(pt_Rev >= &gRPackData.packed_data[PACK_LENGTH - 1])
    {
      pt_Rev = &gRPackData.packed_data[0];
    }

    if(gb_Direct == false)
      Uart1RX(RXBuff);
    else
    {
      *pt_Read = (INT8U)RXBuff;
      if(pt_Read>&gReceivePackData.packed_data[0] && *pt_Read == 0x7F)
      {
         if(gReceivePackData.packed_data[0] != 0x7E)
         {
            pt_Read = &gReceivePackData.packed_data[0];  //set local send pack
            memset(pt_Read, 0, PACK_LENGTH);
            continue;
         }
         gp_RPack->SetPack(&gReceivePackData);
         UCHAR chk_rslt = gp_RPack->DePack_Data();
         if(*pt_Read == 0x7F && gp_SPack->GetAddressId() != gp_RPack->GetAddressId())
         {
            continue;
         }
         if(*pt_Read != 0x7F && gp_SPack->GetPackId() != gp_RPack->GetPackId())
         {
            SetEvent(gErrEvent);
            continue;
         }
         else if(chk_rslt == ACK_CRC)
      	 {
            MergeObjectInfo("CRC校验错");
            SetEvent(gErrEvent);
            continue;
         }
         else if(*pt_Read != 0x7F && gReceivePackData.depack_data[PRTCL_POS_CMMD] != 0x00)
         {
            continue;
         }
         SetEvent(gOkEvent);
      }
      pt_Read++;
      if(pt_Read >= &ReceivePackData.packed_data[PACK_LENGTH - 1])
      {
        pt_Read = &gReceivePackData.packed_data[0];  //set local send pack
        memset(pt_Read, 0, PACK_LENGTH);
      }
    }
 		LeaveCriticalSection(&m_csCommunicationSync);
	} // end forever loop

}

void Uart1RX(INT8U rcv_data)
{
	static INT8U	i,sms_index_start,sms_sm[2];

	sms_sm[0] = sms_sm[1];
	sms_sm[1] = rcv_data;
	if((sms_sm[0] == 'S')&&(sms_sm[1] == 'M'))
	{
		sms_index_start = TRUE;
		i = 0;
	}
  EnterCriticalSection(&g_csRcv);

	if(sms_index_start == TRUE)
	{
		if(i >= 5)
		{
			sms_index_start = FALSE;
			sms_rcv[i] = rcv_data;
			//////////////////2006.06.23
			/*if(ptRcv_index == &sms_index[0])
			{
				ptRcv_index = ptRcv_index->ptNext;
			}*/
			//////////////////2006.06.23
			if((sms_rcv[4] <= 0x39)&&(sms_rcv[4] >= 0x30))
			{
        g_Rev_Queue.push((sms_rcv[3] - 0x30)  * 10 + sms_rcv[4] - 0x30);
				/*ptRcv_index->sms_index = (sms_rcv[3] - 0x30)  * 10 + sms_rcv[4] - 0x30;
				ptCur_index = ptRcv_index;
				ptRcv_index = ptRcv_index->ptNext;*/
			}
			else if((sms_rcv[3] <= 0x39)&&(sms_rcv[3] >= 0x30))
			{
        g_Rev_Queue.push(sms_rcv[3] - 0x30);
				/*ptRcv_index->sms_index = sms_rcv[3] - 0x30;
				ptCur_index = ptRcv_index;
				ptRcv_index = ptRcv_index->ptNext;*/
			}
			else
			{
			}
			i = 0;
		}
		else
		{
			sms_rcv[i] = rcv_data;
			++i;
		}
	}
  LeaveCriticalSection(&g_csRcv);
}

//
// Write a string to the port
//
void CSerialPort::WriteToPort(char* string)
{		
	assert(m_hComm != 0);
	
	memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
	strcpy(m_szWriteBuffer, string);
	m_nWriteSize=strlen(string);

	// set event for write
	SetEvent(m_hWriteEvent);
}


void CSerialPort::WriteToPort(char* string,int n)
{		
  if(gb_Direct)
    WaitForSingleObject(m_hEvent, 1000);
  else
    WaitForSingleObject(m_hEvent, 4000);
	memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
	memcpy(m_szWriteBuffer, string, n);
	m_nWriteSize=n;
  ResetEvent(m_hEvent);
	SetEvent(m_hWriteEvent);
}

void CSerialPort::WriteToPort(LPCTSTR string)
{		
	assert(m_hComm != 0);

	memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
	strcpy(m_szWriteBuffer, string);
	m_nWriteSize=strlen(string);

	// set event for write
	SetEvent(m_hWriteEvent);
}

void CSerialPort::WriteToPort(LPCTSTR string,int n)
{		
	assert(m_hComm != 0);

	memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
//	strncpy(m_szWriteBuffer, string, n);
	memcpy(m_szWriteBuffer, string, n);
	m_nWriteSize=n;
	// set event for write
	SetEvent(m_hWriteEvent);
}

//
// Return the device control block
//
DCB CSerialPort::GetDCB()
{
	return m_dcb;
}

//
// Return the communication event masks
//
DWORD CSerialPort::GetCommEvents()
{
	return m_dwCommEvents;
}

//
// Return the output buffer size
//
DWORD CSerialPort::GetWriteBufferSize()
{
	return m_nWriteBufferSize;
}


void CSerialPort::ClosePort()
{
	//SetEvent(m_hShutdownEvent);
  if(m_hComm)
  {
    CloseHandle(m_hComm);
    m_hComm=NULL;
  }
	// Kill this thread.  break is not needed, but makes me feel better.
	TerminateThread(mhSubThread, -1);
  mhSubThread = NULL;
	m_bThreadAlive = FALSE;
}

void __fastcall CSerialPort::StartThread()
{
   mhSubThread	= CreateThread (NULL, 0, StartSerialThread, NULL, 4, NULL);
   ResumeThread(mhSubThread);
}

DWORD WINAPI StartSerialThread(LPVOID pParam)
{
  ((Form1->m_Port).*gpSerialFun)();
  return 0;
}

void CSerialPort::SerialExecute()
{
	// Set the status variable in the dialog class to
	// TRUE to indicate the thread is running.
	m_bThreadAlive = TRUE;

	// Misc. variables
	DWORD Event = 0;
	DWORD CommEvent = 0;
	DWORD dwError = 0;
	COMSTAT comstat;
	BOOL  bResult = TRUE;

	// Clear comm buffers at startup
	if (m_hComm)		// check if the port is opened
		PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

	// begin forever loop.  This loop will run as long as the thread is alive.
	for (;;)
	{

		// Make a call to WaitCommEvent().  This call will return immediatly
		// because our port was created as an async port (FILE_FLAG_OVERLAPPED
		// and an m_OverlappedStructerlapped structure specified).  This call will cause the
		// m_OverlappedStructerlapped element m_OverlappedStruct.hEvent, which is part of the m_hEventArray to
		// be placed in a non-signeled state if there are no bytes available to be read,
		// or to a signeled state if there are bytes available.  If this event handle
		// is set to the non-signeled state, it will be set to signeled when a
		// character arrives at the port.

		// we do this for each port!

		bResult = WaitCommEvent(m_hComm, &Event, &m_ov);

		if (!bResult)
		{
			// If WaitCommEvent() returns FALSE, process the last error to determin
			// the reason..
			switch (dwError = GetLastError())
			{
			case ERROR_IO_PENDING:
				{
					// This is a normal return value if there are no bytes
					// to read at the port.
					// Do nothing and continue
					break;
				}
			case 87:
				{
					// Under Windows NT, this value is returned for some reason.
					// I have not investigated why, but it is also a valid reply
					// Also do nothing and continue.
					break;
				}
			default:
				{
					// All other error codes indicate a serious error has
					// occured.  Process this error.
					ProcessErrorMessage("WaitCommEvent()");
					break;
				}
			}
		}
		else
		{
			bResult = ClearCommError(m_hComm, &dwError, &comstat);
			if (comstat.cbInQue == 0)
				continue;
		}	// end if bResult

		// Main wait function.  This function will normally block the thread
		// until one of nine events occur that require action.
		Event = WaitForMultipleObjects(3, m_hEventArray, FALSE, INFINITE);

		switch (Event)
		{
		case 0:
			{
				// Shutdown event.  This is event zero so it will be
				// the higest priority and be serviced first.
				CloseHandle(m_hComm);
				m_hComm=NULL;
				m_bThreadAlive = FALSE;

				// Kill this thread.  break is not needed, but makes me feel better.
				TerminateThread(mhSubThread, -1);
        mhSubThread = NULL;
        return;
			}
		case 1:	// read event
			{
				GetCommMask(m_hComm, &CommEvent);
				if (CommEvent & EV_RXCHAR)
        {
					// Receive character event from port.
					ReceiveChar(comstat);
        }
				if (CommEvent & EV_CTS)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_BREAK)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_ERR)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_RING)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);

				if (CommEvent & EV_RXFLAG)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);

				break;
			}
		case 2: // write event
			{
				// Write character event from port
				WriteChar();
				break;
			}

		} // end switch

	} // close forever loop

}


/*void __fastcall TMonitorThread::Execute()
{
	// Set the status variable in the dialog class to
	// TRUE to indicate the thread is running.
	m_Port->m_bThreadAlive = TRUE;

	// Misc. variables
	DWORD Event = 0;
	DWORD CommEvent = 0;
	DWORD dwError = 0;
	COMSTAT comstat;
	BOOL  bResult = TRUE;

	// Clear comm buffers at startup
	if (m_Port->m_hComm)		// check if the port is opened
		PurgeComm(m_Port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

	// begin forever loop.  This loop will run as long as the thread is alive.
	for (;;)
	{

		// Make a call to WaitCommEvent().  This call will return immediatly
		// because our port was created as an async port (FILE_FLAG_OVERLAPPED
		// and an m_OverlappedStructerlapped structure specified).  This call will cause the
		// m_OverlappedStructerlapped element m_OverlappedStruct.hEvent, which is part of the m_hEventArray to
		// be placed in a non-signeled state if there are no bytes available to be read,
		// or to a signeled state if there are bytes available.  If this event handle
		// is set to the non-signeled state, it will be set to signeled when a
		// character arrives at the port.

		// we do this for each port!

		bResult = WaitCommEvent(m_Port->m_hComm, &Event, &m_Port->m_ov);

		if (!bResult)
		{
			// If WaitCommEvent() returns FALSE, process the last error to determin
			// the reason..
			switch (dwError = GetLastError())
			{
			case ERROR_IO_PENDING:
				{
					// This is a normal return value if there are no bytes
					// to read at the port.
					// Do nothing and continue
					break;
				}
			case 87:
				{
					// Under Windows NT, this value is returned for some reason.
					// I have not investigated why, but it is also a valid reply
					// Also do nothing and continue.
					break;
				}
			default:
				{
					// All other error codes indicate a serious error has
					// occured.  Process this error.
					m_Port->ProcessErrorMessage("WaitCommEvent()");
					break;
				}
			}
		}
		else
		{
			bResult = ClearCommError(m_Port->m_hComm, &dwError, &comstat);
			if (comstat.cbInQue == 0)
				continue;
		}	// end if bResult

		// Main wait function.  This function will normally block the thread
		// until one of nine events occur that require action.
		Event = WaitForMultipleObjects(3, m_Port->m_hEventArray, FALSE, INFINITE);

		switch (Event)
		{
		case 0:
			{
				// Shutdown event.  This is event zero so it will be
				// the higest priority and be serviced first.
				CloseHandle(m_Port->m_hComm);
				m_Port->m_hComm=NULL;
				m_Port->m_bThreadAlive = FALSE;

				// Kill this thread.  break is not needed, but makes me feel better.
				this->Terminate();
        return;
			}
		case 1:	// read event
			{
				GetCommMask(m_Port->m_hComm, &CommEvent);
				if (CommEvent & EV_RXCHAR)
        {
					// Receive character event from port.
					m_Port->ReceiveChar(m_Port, comstat);
        }
				if (CommEvent & EV_CTS)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_BREAK)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_ERR)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
				if (CommEvent & EV_RING)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);

				if (CommEvent & EV_RXFLAG)
					;//::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);

				break;
			}
		case 2: // write event
			{
				// Write character event from port
				m_Port->WriteChar(m_Port);
				break;
			}

		} // end switch

	} // close forever loop

}

__fastcall TMonitorThread::TMonitorThread(bool CreateSuspended, CSerialPort *port)
        : TThread(CreateSuspended)
{
  m_Port = port;
}*/


⌨️ 快捷键说明

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