Palm OS/C/序列程式設計
外觀
< 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);
}