September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Palm OS Communications Primer: IR Library

  • August 31, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

We have already covered Serial Manager and Net Library briefly in earlier articles. One more important communication player left untouched is the Infrared Library (IR Library). Actually, you might use IR either with Exchange Manager or Serial Manager (IrCOMM). IR Library gives you low-level access to the IR capabilities of a Palm device. When may you want to deal with IR? Well, when you need to print via the infrared port or transmit data to different devices supporting IrDA are good examples. So, let's start.

Quick introduction

The IR support provided by the Palm OS is compliant with the IrDA specifications. As usual, an IR stack consists of several protocol layers. You will find more information at http://www.IrDA.org/. Here, we only put on a stack diagram taken from PalmOS Help as in figure 1.

In a couple of words, there is a low hardware level (SerialIR/FastIR); then, over it is the IR Link Access Protocol (IrLAP) that provides a data pipe between IrDA devices. IrLMP, the IR Link Management Protocol, manages multiple sessions using the IrLAP. Tiny TP is a lightweight transfer protocol on which some higher-level IrDA layers are built. All these layers are a must.

IrComm and IrLAN are optional; hence, their support is not guaranteed under Palm OS. But OBEX is actually available through the Exchange Manager API. In addition, The Palm OS IR Library supports IAS, a database service devices may use to publish information about themselves and services provided and get the same info about others.

How it all works

After such an academic start, let's consider some practical aspects. Serial communications is the simplest case. All you need to do is specify sysFileCVirtIrComm as the serial port name. The next sample shows all the stuff:

static void DoPDA()
{
   Char szError[128];
   MemSet(szError,sizeof(szError),0);
   Char szBuffer[128];
   StrCopy(szBuffer,"Sample Message");
   Char szAnswer[128];
   MemSet(szAnswer,sizeof(szAnswer),0);

   UInt16 dwSize = StrLen(szBuffer) + 1;

   UInt16 nPortId;
   Err err = SrmOpen(sysFileCVirtIrComm,115200,&nPortId);
   if ( err != errNone )
   {
      StrPrintF(_szInfo,"SrmOpen returned error = 0x%X - %s\n",err,
                GetSerialErrorText(err, szError));
      Log(_szInfo);
      return;
   }

   // Send buffer size
   UInt32 nBytes = SrmSend(nPortId, &dwSize, sizeof(dwSize), &err);
   StrPrintF(_szInfo,"1. SrmSend sent %lu bytes\n",nBytes);
   Log(_szInfo);

   if ( err != errNone )
   {
      StrPrintF(_szInfo,"1. SrmSend returned error = 0x%X - %s\n",
                err,GetSerialErrorText(err, szError));
      Log(_szInfo);
      goto CleanupSer;
   }

   // Receive ACK
   err = SrmReceiveWait(nPortId,3,5 * SysTicksPerSecond());
   if ( err != errNone )
   {
      StrPrintF(_szInfo,"1. SrmReceiveWait returned error = 
                0x%X - %s\n",err,GetSerialErrorText(err, szError));
      Log(_szInfo);
      goto CleanupSer;
   }
   Log("1. SrmReceiveWait is OK\n");

   nBytes = SrmReceive(nPortId, (void*)szAnswer, 3,
                       5 * SysTicksPerSecond(), &err);
   if ( err != errNone )
   {
      StrPrintF(_szInfo,"1. SrmReceive returned error =
                0x%X - %s\n", err,GetSerialErrorText(err, szError));
      Log(_szInfo);
      goto CleanupSer;
   }
   StrPrintF(_szInfo,"1. SrmReceive returned buffer %s\n",szAnswer);
   Log(_szInfo);

   // Send buffer
   nBytes = SrmSend(nPortId, szBuffer, StrLen(szBuffer) + 1, &err);
   StrPrintF(_szInfo,"2. SrmSend sent %lu bytes\n",nBytes);
   Log(_szInfo);

   if ( err != errNone )
   {
      StrPrintF(_szInfo,"2. SrmSend returned error = 0x%X - %s\n",
                err,GetSerialErrorText(err, szError));
      Log(_szInfo);
      goto CleanupSer;
   }

CleanupSer:

   SrmClose(nPortId);
}

That's just a slightly modified example from the first article in this cycle. Using similar code at the 'server' side, we will be able to exchange data over a virtual serial port.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel