How to know data incoming from serial port?

Dear Sir,

I developed my App by serial driver and I can read and write data by serial port ("/dev/ser1"). The physical connection is shown as below:

PC------/dev/ser1-------Modem---------Externel Device(Data Device)

Now my question is: The Data Device will send data to PC randomly, I think the Mode of getting Data by polling the serial port periodically makes on sense and it will take most CPU process time. Now my question is, how do I know data incoming from serial port without polling the serial port? by Interrupt or some Signal else? or something like the code in Microsoft visual basic serial communication event: comRing, ComReceive, ComSend, etc?

Can someone kindly give some example code?

Thanks.

Alex Yu

I use ionotify() with a SIGEV_PULSE_INIT sigevent for notifications that serial data is available:

SIGEV_PULSE_INIT( &dataEvent, connectionId, SIGEV_PULSE_PRIO_INHERIT, 5, 0);

// Re-register for notifications of received data
status = ionotify( comPortFd, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT, dataEvent);
if( status == -1)
{
	Log( LOG_ERRORS, "\tDriverDataRegister() ionotify failed: %s\n", strerror( errno));
}
// If data is currently available
else if( status & _NOTIFY_COND_INPUT)
{
	int bytesWaiting;
	if( (bytesWaiting=tcischars( comPortFd)) > 0)
	{
		Log( LOG_DEV_DATA, "DriverDataRegister() %d bytes available, forwarding notification pulse\n", bytesWaiting);
		// Send a pulse to our channel
		MsgSendPulse( dataEvent->sigev_coid, dataEvent->sigev_priority, 
			  dataEvent->sigev_code, dataEvent->sigev_value.sival_int+1);
	}
	else
	{
		Log( LOG_ERRORS, "\tSerial driver says data is ready, but tcischars() <= 0\n");
	}
	status = 0;
}

Am I missing something here? What’s wrong with just calling read()? It will block on the serial port until their is data available (one char at a time if that is the size of the read). Just make sure the serial port is in raw mode before you call read (otherwise it will block until you have a “line” of data).

Rick…

Rick, what if you don’t want to program to block, handling other events as well. Personnaly I prefer having a thread handle the read. The thread can deal with the protocol, error handling and such. When the data is received the thread notifies another thread.

select() works in that situation as well. Any solution which doesn’t involve polling is a better solution than one which polls. For that matter I also avoid threads, unless it is the simplest solution - just because of the complexity and risk using threads adds.

Rick…