Rockwell Automation 1771-DMC_DMC1_DMC4_DXPS Control Coprocessor User Manual User Manual

Page 122

Advertising
background image

Chapter 7

Using the Serial Ports

7-14

* NOTE: Always use a _gs_rdy() call to make sure there are

* is enough data to read in the input buffer before making

* this read call. Otherwise the function will appear to

* ’hang’, because it is waiting for the number of characters

* it was told to read.

* SYNOPSIS int read_485(path, buffer, count)

* int path; - Path number from opened port

* char *buffer; - Pointer to buffer for read

* int count; - minimum size of buffer

* EXAMPLE: int path;

* char in_data[10];

* int cnt = 5;

* path = open(“/t1”, (S_IREAD | S_IWRITE));

* init_485(path);

* status = read_485(path, in_data, cnt)

* if (status == -1)

* exit();

****************************************************************************/

int read_485(path, buffer, count, timeout)

int path;

char *buffer;

int count;

int timeout;

{

int status;

int tmp_count=0;

/*** Make sure there is enough data in input buffer before reading ***/

while (((tmp_count = _gs_rdy(path)) < count) && timeout--)

;

/*** Ooops, timeout. No data. Returning. ***/

if (timeout == -1)

{

fprintf(stderr, “**** ERROR timeout in read_485() function! ***\n”);

return(-1);

}

/*** Do the read since there is data there ***/

status = read(path, buffer, count);

return(status);

}

/*****************************************************************************

* w r i t e _ 4 8 5 . c

* PURPOSE: Write characters to a serial port configured for RS-485.

* REVISION LOG: 4/12/94 Original release of program

* USAGE: This function writes characters to a port configured for RS-485.

* Because doing serial write commands over RS-485 requires special

* techniques to complete the write, this function was created to

* take care of the details of transmitter control and the clearing

* of the input buffer.

* Function returns the number of bytes actually written. A -1 is

* returned if an error occurs. The error code is placed in the

* variable ’errno’. The function will no return until all the

* characters are physically transmitted out of the port.

* SYNOPSIS int write_485(path, buffer, count)

* int path; - Path number from opened port

* char *buffer; - Pointer to write buffer

* int count; - minimum size of buffer

Advertising