IrDA interface with PC - basic steps worth 30 points

3xsms
3xsms used Ask the Experts™
on
Ain't no expert over here :( I've heard of stuff such as IrCOMM... Winsock (for TCP/IP)... in simple words, what are the basic steps of capturing data from an IrDA device. Thx for your attention.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Didier VxSystems Engineer and Finance Analyst

Commented:
With CreateFile function, you have to open the COM port corresponding to the IrDA interface ("COM3:" on Pocket PC), then you can use WriteFile or ReadFile to write/read datas on the IrDA interface.

Author

Commented:
And how do I address such WriteFile / ReadFile commands? Is there anyway it can be made programmable... maybe then the Winsock issue would appear here? I'm kinda lost...

Author

Commented:
I am using the IrDA to allow my cell phone to communicate with the PC. Thx!
Systems Engineer and Finance Analyst
Commented:
Check this out... some adaptation to do.
bye


static int FindIrCommPort(void)
{
  DWORD dwSize, dwData, dwType;
  HKEY hKey;

  *szPort = 0;

  if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("Drivers\\BuiltIn\\IrCOMM"),0,0,&hKey) == ERROR_SUCCESS)
  {
      dwSize = sizeof(dwData);
    if(RegQueryValueEx(hKey,TEXT("Index"),0,&dwType,(BYTE*)&dwData,&dwSize) == ERROR_SUCCESS)
    if(dwData < 10)
            wsprintf(szPort,TEXT("COM%d:"),dwData);
    RegCloseKey(hKey);
    wsprintf(sztmp,TEXT("Found IrComm port at [%s]\r\n"),szPort);
      wcscat(lpszStat, sztmp);
      SendMessage(hText1, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR) lpszStat);
    //MessageBox(g_hWnd,sztmp,_T("dbg"),MB_OK);
    return 0;
  }
  lstrcpy(szPort,TEXT("COM3:"));
  SendMessage(hText1, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR) _T("IrComm port is not found\r\n") );
  //MessageBox(g_hWnd,TEXT("IrComm port is not found."),_T("dbg"),MB_OK);
  return 1;
}



BOOL OpenComm(void)
{
      DWORD ThreadID; DCB PortDCB; COMMTIMEOUTS CommTimeouts;
      DWORD fdwCommMask; BYTE chData;
      if(bCommEnabled) return 0;
      if(FindIrCommPort()) return 0;
      hPort = CreateFile(szPort,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
      if(hPort == INVALID_HANDLE_VALUE)
      {
            wcscat(lpszStat, _T("Can not open IrComm port\r\n"));
            SendMessage(hText1, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR) lpszStat);
            //MessageBox(g_hWnd,TEXT("Can not open IrComm port"),_T("dbg"),MB_OK);
            return -1;
      }
      else
      {
            wcscat(lpszStat, _T("Port opened\r\n"));
            SendMessage(hText1, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR) lpszStat);
            //MessageBox(g_hWnd,TEXT("Port opened"), _T("dbg"), MB_OK);
      }

      // config comm
      PortDCB.DCBlength = sizeof(DCB);
      GetCommState(hPort, &PortDCB);
      PortDCB.BaudRate = CBR_115200;
      PortDCB.fBinary = TRUE;
      PortDCB.fParity = TRUE;
      PortDCB.fOutxCtsFlow = FALSE;    // ignore possible hangups
      PortDCB.fOutxDsrFlow = FALSE;    // don't wait on the DSR line
      PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
      PortDCB.fDsrSensitivity = FALSE;
      PortDCB.fTXContinueOnXoff = FALSE;
      PortDCB.fOutX = FALSE;           // no XON/XOFF control
      PortDCB.fInX = FALSE;
      PortDCB.fErrorChar = FALSE;
      PortDCB.fNull = FALSE;
      PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
      PortDCB.fAbortOnError = FALSE;
      PortDCB.ByteSize = 8;
      PortDCB.Parity = NOPARITY;
      PortDCB.StopBits = ONESTOPBIT;
      SetCommState(hPort, &PortDCB);

      // Set comm timeouts
      GetCommTimeouts(hPort, &CommTimeouts);
      CommTimeouts.ReadIntervalTimeout = 1000;
      CommTimeouts.ReadTotalTimeoutMultiplier = 0;
      CommTimeouts.ReadTotalTimeoutConstant = 3000;
      CommTimeouts.WriteTotalTimeoutMultiplier = 0;
      CommTimeouts.WriteTotalTimeoutConstant = 3000;
      SetCommTimeouts(hPort, &CommTimeouts);

      // set ir mode
      EscapeCommFunction(hPort, SETIR);

      // set RX even mask
      SetCommMask(hPort, EV_RXCHAR);

      MessageBox(g_hWnd,TEXT("Ready?"),TEXT("Hand-snaking"),MB_OK);
      WriteFile(hPort,"X",1,&fdwCommMask,0);
      WaitCommEvent(hPort, &fdwCommMask, 0);

      if(fdwCommMask & EV_RXCHAR)
      {
            ReadFile(hPort, &chData, 1, &fdwCommMask, 0);
            WriteFile(hPort,"X",1,&fdwCommMask,0);
            // Set comm timeouts (nonblocking read, and write waiting for completion).
            GetCommTimeouts(hPort, &CommTimeouts);
            CommTimeouts.ReadIntervalTimeout = 0;
            CommTimeouts.ReadTotalTimeoutMultiplier = 0;
            CommTimeouts.ReadTotalTimeoutConstant = 0;
            CommTimeouts.WriteTotalTimeoutMultiplier = 0;
            CommTimeouts.WriteTotalTimeoutConstant = 0;
            SetCommTimeouts(hPort, &CommTimeouts);

            bStopThread = FALSE;
            CloseHandle(CreateThread(0,0,ReadThread,0,0,&ThreadID));
            bCommEnabled = TRUE;

            return 0;
      }
      else
      {
            wcscat(lpszStat, _T("Wait time out\r\n"));
            SendMessage(hText1, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR) lpszStat);
            //MessageBox(g_hWnd,TEXT("Wait time out"), _T("dbg"), MB_OK);
      }
      return -2;
}


void CloseComm(void)
{
      if(bCommEnabled)
      {
            bStopThread = TRUE;
            CloseHandle(hPort);
            Sleep(1000);
            bCommEnabled = FALSE;
      }
}


void WriteComm(BYTE ch)
{
      DWORD n;
      WriteFile(hPort,&ch,1,&n,0);
}


static DWORD WINAPI ReadThread(LPVOID pvarg)
{
      BYTE byte;
      DWORD num;
      DWORD fdwCommMask;

      while(!bStopThread)
      {
            WaitCommEvent(hPort, &fdwCommMask, 0);
            if(fdwCommMask & EV_RXCHAR)
            {
                  do
                  {
                        TCHAR lpszTmp[128];
                        wsprintf(lpszTmp, _T("%d\r\n"), byte);
                        wcscat(lpszStat, lpszTmp);
                        SendMessage(hText1, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR) lpszStat);
                        ReadFile(hPort, &byte, 1, &num, 0);
                        if(num == 1) ProcessRX(byte);
                  } while (num == 1);
            }
      }
      ExitThread(0);
      return 0;
}


void ProcessRX(BYTE c)
{
      wcscat(lpszStat, _T("Receiving datas\r\n") );
      SendMessage(hText1, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR) lpszStat);
/*  HDC hdc = GetDC(hMainWnd);
  HFONT holdfont = (HFONT)SelectObject(hdc,hScreenFont);

  switch( c )
  {
    case 0x08:      //BS
      if( rxcol > 1 )
        if(rxcol>0)
          OutCh( hdc, rxrow, --rxcol, (TCHAR)' ' );
      break;

    case 0x0D:
      rxcol=0;
      rxrow++;
      break;

    default:
      OutCh(hdc,rxrow,rxcol++,c);
  }

  if( rxcol >= maxcol )
  {
    rxcol = 0;
    rxrow++;
  }

  if( rxrow >= rxmaxrow )
  {
    ScrollScreenUp( hdc, 0, rxmaxrow );
    rxrow--;
    rxcol=0;
  }

  SelectObject(hdc,holdfont);
  ReleaseDC(hMainWnd,hdc);
*/
}

void ProcessTX(BYTE c)
{
      wcscat(lpszStat, _T("Sending datas\r\n"));
      SendMessage(hText1, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR) lpszStat);
/*  HDC hdc = GetDC(hMainWnd);
  HFONT holdfont = (HFONT)SelectObject(hdc,hScreenFont);
  WriteComm( c );
  switch( c )
  {
    case 0x08:      //BS
      if( txcol > 0 )
        OutCh( hdc, txrow+rxmaxrow, --txcol, (TCHAR)' ');
      break;
    case 0x0d:      //Enter
      txcol=0;
      txrow++;
      break;
    default:
      OutCh( hdc, txrow+rxmaxrow, txcol++, (TCHAR)c);
  }

  if( txcol >= maxcol )
  {
    txcol=0;
    txrow++;
  }

  if( txrow >= rxmaxrow )
  {
    ScrollScreenUp( hdc, rxmaxrow, maxrow );
    txrow--;
    txcol = 0;
  }

  SelectObject(hdc,holdfont);
  ReleaseDC(hMainWnd,hdc);
*/
}

Author

Commented:
Thx 4 your hint. Any other useful info I should know about this source code? thx once again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial