Code Samples

Code samples are provided for the following:

  • PALM OS 3.5 Code (Uses OS 3.5 Libraries; Metrokwerks Codewarrior)
  • Using the win32api calls. This routine compiled and run under Borland C++Builder 4.0.

PALM OS 3.5 Code (Uses OS 3.5 Libraries; Metrokwerks Codewarrior)

void GetData(char command)
{
UInt32 baud=9600; //baud rate
static char msgdata[10], //data to send to PM2+
readbuffer[32]; // receive data buffer
static UInt16 *refNum; //port ID of serial port
Err checkst, //holds the result of whether serial port opened correctly
error;
unsigned short rcvdata; //no. bytes retrieved

msgdata[0]= command; //send query command—could be 0xb0, 0xb1, 0xb2
msgdata[1]= 0x00; //send address of indoor rower, 00 for single connection
checkst= SrmOpen (0x8000,baud, refNum); //open the serial port
// (0x8000 is port address of com1)
if (checkst==0) //make sure port opened
{
SrmReceiveFlush( *refNum, 1); //flush any garbage
SrmSend ( *refNum, &msgdata, 2,&error); //send command bytes
if( SrmReceiveWait( *refNum, 5, 50) == 0) //wait for 5 byte response
{
rcvdata = SrmReceive (*refNum, &readbuffer[0], 5,0,&error); //put data into buffer
}
else
{
HandleTimeout(); //if timeout occurs probably want to take PALM offline
}
SrmClose(*refNum); // close port after use

ProcessData(&readbuffer[0]); //look at data
}
else
{
ErrFatalDisplayIf( 1, "Serial Port can't be opened"); //can't open port
}
return ;
}
void ProcessData(char *readbuffer)
{
char status;
char floatbytearray[4];
float *floatvalue;

status = readbuffer[0];
floatbytearray[0] = readbuffer[4]; //convert from Intel to Motorola byte ordering
floatbytearray[1] = readbuffer[3]; //because PALM uses a Motorola based processor
floatbytearray[2] = readbuffer[2]; //Windows machines don't have to reverse the bytes
floatbytearray[3] = readbuffer[1];

floatvalue = (float*) floatbytearray; //convert 4 byte array into a floating point number
CheckStatus(status);
Display(*floatvalue);
}

Using the win31api Calls

This routine compiled and run under Borland C++Builder 4.0.
void getdistance(void)
{
HANDLE hComm = NULL;
COMMTIMEOUTS ctmoNew = {0}, ctmoOld;
char tempfloatarray[4];
char InBuff[100];
unsigned long int dwBytesRead;
char status;
float *ergdistance;
DCB dcbCommPort;
// OPEN THE COMM PORT.
hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, 0, 0);
if(hComm == INVALID_HANDLE_VALUE)
AppQuit(); //quit on port failure—insert your own routine
// SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 100;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);
// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
// IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
// THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
// ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.
dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
BuildCommDCB("9600,N,8,1", &dcbCommPort);
SetCommState(hComm, &dcbCommPort);
TransmitCommChar(hComm, 0xb0); //transmit distance query
TransmitCommChar(hComm, 0x00); // erg address—always zero
ReadFile(hComm, InBuff, 5, &dwBytesRead, NULL);
//read the serial port
if(dwBytesRead)
{
status = InBuff[0];
tempfloatarray[0] = InBuff[1];
tempfloatarray[1] = InBuff[2];
tempfloatarray[2] = InBuff[3];
tempfloatarray[3] = InBuff[4];
ergdistance = (float*)&tempfloatarray;
}
if(hComm)
{
SetCommTimeouts(hComm, &ctmoOld); //restore old settings
CloseHandle(hComm);
}
}