《電子技術(shù)應(yīng)用》
您所在的位置:首頁 > 模擬設(shè)計(jì) > 設(shè)計(jì)應(yīng)用 > WINCE上實(shí)現(xiàn)UDP通信(100%可行?。?/span>
WINCE上實(shí)現(xiàn)UDP通信(100%可行?。?
摘要: 首先介紹一下,開發(fā)平臺:WINCE4.2(WINCE5.0也適用,因?yàn)槭褂昧遂o態(tài)編譯MFC動(dòng)態(tài)鏈接庫);開發(fā)工具:VS2005。
關(guān)鍵詞: 接口IC WinCE UDP 通信
Abstract:
Key words :

  首先介紹一下,開發(fā)平臺:WINCE 4.2 (WINCE 5.0也適用,因?yàn)槭褂昧遂o態(tài)編譯MFC動(dòng)態(tài)鏈接庫);開發(fā)工具:VS2005。

  接下來開始修改代碼:

  view plaincopy to clipboardprint?

  1.修改UDP_CE.CPP的SendData函數(shù)為:

  bool CUDP_CE::SendData(const char *buf, int len)

  {

  int nBytes = 0;

  int nErrorCode;

  nBytes = sendto(m_UDPSocket,buf,len,0,(sockaddr*)&m_RemoteAddr,sizeof(m_RemoteAddr));

  if (nBytes==SOCKET_ERROR )

  {

  nErrorCode = WSAGetLastError();

  m_OnUdpError(m_pOwnerWnd,nErrorCode);

  return false;

  }

  return true;

  }

  當(dāng)然,不要忘記修改UDP_CE.h的SendData函數(shù)聲明為:bool SendData(const char *buf, int len);

  2.修改UDP_CE.CPP的RecvThread函數(shù)為:

  。..。..。..。..。..

  if (ret 》 0)

  {

  if (FD_ISSET(pSocket-》m_UDPSocket,&fdRead))

  {

  tmpAddr.sin_family=AF_INET;

  tmpAddr.sin_port = htons(pSocket-》m_RemoteAddr.sin_port);

  tmpAddr.sin_addr.s_addr =INADDR_ANY;

  tmpRecvLen = sizeof(tmpAddr);

  recvBuf = new char[1024];

  recvedBuf = new char[1024];

  ZeroMemory(recvBuf,1024);

  ZeroMemory(recvedBuf,1024);

  recvLen = recvfrom(pSocket-》m_UDPSocket,recvBuf, 1024,0,(SOCKADDR*)&tmpAddr,&tmpRecvLen);

  if (recvLen == SOCKET_ERROR)

  {

  iErrorCode = WSAGetLastError();

  pSocket-》m_OnUdpError(pSocket-》m_pOwnerWnd,iErrorCode);

  break;

  }

  else if (recvLen == 0)

  {

  iErrorCode = WSAGetLastError();

  pSocket-》m_OnUdpError(pSocket-》m_pOwnerWnd,iErrorCode);

  break;

  }

  else

  {

  //此處添加解析程序,將接收到的數(shù)據(jù)解析后,

  pSocket-》m_OnUdpRecv(pSocket-》m_pOwnerWnd,recvBuf,recvedBufLen,(SOCKADDR*)&tmpAddr);

  delete []recvBuf;

  recvBuf = NULL;

  delete []recvedBuf;

  recvedBuf = NULL;

  }

  }

  3.修改UDPDlg.cpp.CPP的OnUdpCERecv函數(shù)為:

  CUDPDlg * pDlg;

  pDlg = (CUDPDlg*)pWnd;

  CEdit *pRecvStrEdit = (CEdit*)pDlg-》GetDlgItem(IDC_EDIT_RECVSTR);

  ASSERT(pRecvStrEdit != NULL);

  CString strRecv (buf);

  pRecvStrEdit-》SetWindowText(strRecv);

  4.刪除UDP_CE.H,不是必須,但可以節(jié)省多余的代碼

  typedef struct CUDPData

  {

  char FrameHead[4]; //幀頭

  DWORD DataPackageLen; //幀長度

  char * Buf; //緩沖區(qū)數(shù)據(jù)

  char FrameTail[4]; //幀尾

  }UDPData;

  1.修改UDP_CE.CPP的SendData函數(shù)為:

  bool CUDP_CE::SendData(const char *buf, int len)

  {

  int nBytes = 0;

  int nErrorCode;

  nBytes = sendto(m_UDPSocket,buf,len,0,(sockaddr*)&m_RemoteAddr,sizeof(m_RemoteAddr));

  if (nBytes==SOCKET_ERROR )

  {

  nErrorCode = WSAGetLastError();

  m_OnUdpError(m_pOwnerWnd,nErrorCode);

  return false;

  }

  return true;

  }

  當(dāng)然,不要忘記修改UDP_CE.h的SendData函數(shù)聲明為:bool SendData(const char *buf, int len);

  2.修改UDP_CE.CPP的RecvThread函數(shù)為:

  。..。..。..。..。..

  if (ret 》 0)

  {

  if (FD_ISSET(pSocket-》m_UDPSocket,&fdRead))

  {

  tmpAddr.sin_family=AF_INET;

  tmpAddr.sin_port = htons(pSocket-》m_RemoteAddr.sin_port);

  tmpAddr.sin_addr.s_addr =INADDR_ANY;

  tmpRecvLen = sizeof(tmpAddr);

  recvBuf = new char[1024];

  recvedBuf = new char[1024];

  ZeroMemory(recvBuf,1024);

  ZeroMemory(recvedBuf,1024);

  recvLen = recvfrom(pSocket-》m_UDPSocket,recvBuf, 1024,0,(SOCKADDR*)&tmpAddr,&tmpRecvLen);

  if (recvLen == SOCKET_ERROR)

  {

  iErrorCode = WSAGetLastError();

  pSocket-》m_OnUdpError(pSocket-》m_pOwnerWnd,iErrorCode);

  break;

  }

  else if (recvLen == 0)

  {

  iErrorCode = WSAGetLastError();

  pSocket-》m_OnUdpError(pSocket-》m_pOwnerWnd,iErrorCode);

  break;

  }

  else

  {

  //此處添加解析程序,將接收到的數(shù)據(jù)解析后,

  pSocket-》m_OnUdpRecv(pSocket-》m_pOwnerWnd,recvBuf,recvedBufLen,(SOCKADDR*)&tmpAddr);

  delete []recvBuf;

  recvBuf = NULL;

  delete []recvedBuf;

  recvedBuf = NULL;

  }

  }

  3.修改UDPDlg.cpp.CPP的OnUdpCERecv函數(shù)為:

  CUDPDlg * pDlg;

  pDlg = (CUDPDlg*)pWnd;

  CEdit *pRecvStrEdit = (CEdit*)pDlg-》GetDlgItem(IDC_EDIT_RECVSTR);

  ASSERT(pRecvStrEdit != NULL);

  CString strRecv (buf);

  pRecvStrEdit-》SetWindowText(strRecv);

  4.刪除UDP_CE.H,不是必須,但可以節(jié)省多余的代碼

  typedef struct CUDPData

  {

  char FrameHead[4]; //幀頭

  DWORD DataPackageLen; //幀長度

  char * Buf; //緩沖區(qū)數(shù)據(jù)

  char FrameTail[4]; //幀尾

  }UDPData;還可以刪除窗體上發(fā)送、接收數(shù)字的編輯框等,因?yàn)閷?shí)際應(yīng)用中用不上。

此內(nèi)容為AET網(wǎng)站原創(chuàng),未經(jīng)授權(quán)禁止轉(zhuǎn)載。