Rockwell Automation 1771-DMC_DMC1_DMC4_DXPS Control Coprocessor User Manual User Manual

Page 79

Advertising
background image

Chapter 5

Developing Programs

5-19

Important: The CC_INIT function is used in the following program.
Call the CC_INIT function first and once only in your program.

/**********************************************************************
* bt_w_r.c
* This program uses both the BPI_WRITE and BPI_READ functions. It
* continuously triggers the PLC to do block transfer writes to the
* coprocessor. The coprocessor write the data to the user screen
* and then copies the first five words to the out_buffer that will be
* sent back to the PLC as part of this loopback test.
***********************************************************************/
#include <stdio.h>

#include <copro.h>

#define CLEAR_SCREEN() printf(“\33[2J”) /* Clear screen macro */

#define MOVE(x,y) printf(“\33[%d;%dH”, y, x) /* Move cursor macro */

#define TIMEOUT 4

#define R_LENGTH 10

#define W_LENGTH 5

#define R_TRIG 0x0100

#define W_TRIG 0x0200

main()
{
register unsigned ret_val; /* private variable declarations */
int x, y = 0;
unsigned short in_buffer[10];
unsigned short out_buffer[5];
for (x=0; x < 10; x++) /* Initialize in buffer */
in_buffer[x] = 0;
x = 0; /* Reinitialize for later */
CC_INIT(); /* initialize the coprocessor */
CLEAR_SCREEN();
while (1){
/* Trigger the BTW from the PLC with this BPI_READ function */
ret_val = BPI_READ (R_LENGTH, in_buffer, TIMEOUT, R_TRIG);
if (ret_val != DTL_SUCCESS){
print_error (ret_val);
exit(10); /* Print error and exit */
}
for (x=0; x < 5; x++) /* Copy first 5 words of */
out_buffer[x] = in_buffer[x]; /* input to output buffer */
/* Print the results of the block transfer continuously on the screen */
MOVE(0,2); /* Move cursor to top of screen */
for (x=0; x < 10; x++)
printf (“Word %d of the PLC BTW = %d \n”, x, in_buffer[x]);
printf (“\n\nFree running timer showing communication activity %d\n”, y++);
/* Now trigger the PLC to do a BTR with this BPI_WRITE function */
ret_val = BPI_WRITE (W_LENGTH, out_buffer, TIMEOUT, W_TRIG);
if (ret_val != DTL_SUCCESS){
print_error (ret_val);
exit(20); /* Print error and exit */
}
} /* End of while(1) loop */
} /* End of main */
print_error (err) /* process error code */
int err;
{
char *errptr;
errptr = (char *) CC_ERROR (err); /* get pointer to string */
printf (“\n Return Value = %4d - %s \n”, err, errptr); /* print it */
printf (“\n\nIf you got a time out error, check that you are in RUN mode\n”);
}

Use CC_INIT

first and once in

every program

Advertising