跳至内容

Palm OS/C/串行编程

摘自维基教科书,开放世界的开放书籍

打开串行端口

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

要发送数据

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

要接收数据更加复杂

 // 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();

一旦事件管理器唤醒,许多 API 函数(如果在接收到的数据处理程序中调用)会导致致命异常,即使是那些与事件无关的函数也会如此。

应用程序处理表明已经收到数据的特殊键代码的位置

 UInt8   *receiveBuffer;
 UInt32  bytesReceived;
 
 Err  e = SrmReceiveWindowOpen( serPort, &receiveBuffer, &bytesReceived);
 if ( errNone == e)
 {
   SrmReceiveWindowClose( serPort, bytesReceived);
 }
华夏公益教科书