Programming for Palm OS/C/Serial

Open the serial port:

 UInt16  serPort;
 Err  e = SrmOpen( serPortCradlePort, 9600, &serPort);
 ErrFatalDisplayIf( e != errNone, "could not open the serial port");

To send data:

 Err   e;
 char  toSend = '\n';
 SrmSend( serPort, &toSend, sizeof(toSend), &e);

To receive data is more involved:

 // This fragment is used in two places
 //
 void prime_receive_handler()
 {
   UInt16  minBytes = 1;
 
   SrmPrimeWakeupHandler( serPort, minBytes);
 }
 
 #define  MAGIC_KEYCODE  0xa43d
 
 // This method is invoked by Palm OS when data is received
 //
 void data_received( UInt32 appData)
 {
   // This method is invoked as an interrupt handler.  Send a "special" key code
   // to the application to indicate that it should read the receive buffer
   WChar   ascii     = 0;
   UInt16  keyCode   = MAGIC_KEYCODE;
   UInt16  modifiers = 0;
 
   EvtEnqueueKey( ascii, keyCode, modifiers);
   EvtWakeup();
   // EvtWakeupWithoutNilEvent is unavailable on Palm OS 3.5
 
   // Indicate that the handler is ready for more
   prime_receive_handler();
 }
 
 // Indicate the code that should run when data is received
 UInt32  appData = NULL;  // or whatever you wish passed
 SrmSetWakeupHandler( serPort, data_received, appData);
 prime_receive_handler();

Quite a lot of API functions (if they are invoked within the received data handler) will cause fatal exceptions once the event manager wakes up, even functions that appear unrelated to events.

Where the application handles the special key code that indicates data has been received:

 UInt8   *receiveBuffer;
 UInt32  bytesReceived;
 
 Err  e = SrmReceiveWindowOpen( serPort, &receiveBuffer, &bytesReceived);
 if ( errNone == e)
 {
   SrmReceiveWindowClose( serPort, bytesReceived);
 }