beautypg.com

Rs232_write() – Echelon i.LON SmartServer 2.0 User Manual

Page 242

background image

228

Appendix A - Programmer’s Reference

int nBytesRead = ReadBytes(RS232_fd, rxBuf, MAX_RX_BYTE_SIZE);

//check whether something has been read
if (nBytesRead >= 1)

{

printf ("Read %c from RS232\n", Line1);

//if something has been read, write it to display device

rs232_write(RS232_fd,

(Byte

*)rxBuf,

nBytesRead);

}

}

}


int CRs232Driver::ReadBytes(HANDLE handle, Byte* buffer,

int bytesToRead) const

{
fd_set readFds;

FD_ZERO(&readFds);
FD_SET(handle, &readFds);

Timeval readTimeout;
readTimeout.tv_sec = 0;
readTimeout.tv_usec = 0; //return immediately if no data is

//available

int nResult = select(handle+1, &readFds, NULL, NULL,

&readTimeout);


if(!nResult || nResult == ERROR)
return nResult;

if(!BytesReadyForRead(handle))
return ERROR;

return rs232_read(handle, buffer, bytesToRead );
}

int CRs232Driver::BytesReadyForRead(HANDLE handle)const
{
int bytesAvailable;
ioctl(handle, FIONREAD, (int) &bytesAvailable );

return bytesAvailable;
}

rs232_write()

You can use the rs232_write()method in the OnTimer()routine to write data to the RS-232
interface.

SYNTAX

int rs232_write(int fd, unsigned char * buf, int length);

The fd parameter specifies the file handle returned when the RS-232 interface was opened with
the rs232_open() method.