Rockwell Automation 1771-DMC_DMC1_DMC4_DXPS Control Coprocessor User Manual User Manual

Page 123

Advertising
background image

Chapter 7

Using the Serial Ports

7-15

* EXAMPLE: int path;

* char out_data[3];

* int cnt = 3;

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

* init_485(path);

* status = write_485(path, out_data, cnt)

* if (status == -1)

* exit();

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

int write_485(path, buffer, count)

int path;

char *buffer;

int count;

{

int tmp_count; /* Temporary count variable */

int status; /* Status variable */

int size; /* Size of leftover stuff in input buffer */

int watchdog; /* Watchdog counter variable */

char tmp_buff[256]; /* Temporary buffer to clear input port */

char buff; /* Character buffer */

int sent; /* Actual number of characters sent */

/*** Make sure buffer is empty ***/

if ((size = _gs_rdy(path)) != -1) /* Is it empty? */

{

while (size--)

if ((status = read(path, &buff, 1)) == -1) /* Clear it out */

{

fprintf(stderr, “**** ERROR reading input buffer! ***\n”);

return(-1);

}

}

/*** Enable the transmitter ***/

if ((status = _ss_dsrts(path)) == -1)

{

fprintf(stderr, “**** ERROR turning on transmitter ***\n”);

return(-1);

}

/*** Send the data ***/

if ((sent = write(path, buffer, count)) == -1)

{

fprintf(stderr, “**** ERROR writing data ***\n”);

return(-1);

}

/*** Watch the data going out. When the input buffer has the same ***/

/*** amount as sent, then the transmitter can be turned off ***/

tmp_count = 0;

watchdog = 50000; /* This is worst case, it may be reduced based */

/* on baud rate and number of characters sent. */

while ((tmp_count < sent) && watchdog--)

tmp_count = _gs_rdy(path);

Advertising