[Gllug] RS232 Programming

Jackson, Harry HJackson at colt-telecom.com
Tue Aug 21 16:37:30 UTC 2001


Hi all

	How hard would it be to write something that would read data from a
serial port to save in a database. In particular data from a Concept II
rowing machine. I am able to get the specifications for it and I would like
something that you can take to the gym on a palmtop so that you could record
your progress etc. The following is a couple of bits that have been written
but I cannot make head nor tail of them. 

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 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 prob. 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);
}
 
An example using the win32api 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);
	}
}


Regards;
Harry Jackson.


     



**********************************************************************
COLT Telecommunications
Registered in England No. 2452736
Registered Office: Bishopsgate Court, 4 Norton Folgate, London E1 6DQ
Tel. 020 7390 3900

This message is subject to and does not create or vary any contractual
relationship between COLT Telecommunications, its subsidiaries or 
affiliates ("COLT") and you. Internet communications are not secure
and therefore COLT does not accept legal responsibility for the
contents of this message.  Any view or opinions expressed are those of
the author. The message is intended for the addressee only and its
contents and any attached files are strictly confidential. If you have
received it in error, please telephone the number above. Thank you.


**********************************************************************

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list