User manual

116
Communication Program Examples
Users can write a communication program by using the following
example. For more information, contact the sales or technical
department.
Program Notes
<PLC communication sample code>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#define PC_ID 0xE2
#define time_limit 28
#define retrial_limit 2
#define TRUE 1
#define FALSE 0
#define lower_byte(x) (unsigned int) ((x)& 0x00FF)
#define upper_byte (x) (unsigned int) (((x)& 0xFF00)>>8)
typedef int BOOL;
unsigned int PORTADD,DIVISOR,sending_delay, receiving_delay;
unsigned int sending_frame[262],receiving_frame[262];
unsigned int Crc;
unsigned int card,i,ix,iy,smode;
unsigned int port_number;
unsigned int PlcID,OldID;
BOOL Success;
unsigned int data,JobID,retrialC;
unsigned int Old,New,receiving_Index_max,sending_Index_max,
index,watchdog;
unsigned int M[128],K[128]; /* Example Register */
void RR_occurring(void);
void Trsport(unsigned int);
unsigned int Recport(void);
BOOL sending_occuring(void);
BOOL receiving_occuring (void);
void Crc16(unsigned int);
void Job(void);
unsigned int communication(void);
void Mword_reading(void);
void Kword_writing(void);
void main(void)
{
unsigned int i;
/* Selection of communication port */
clrscr();
printf("PORT : COM1[1]/ COM[2]/ GPC-232[3]/GPC-485[4]/GPC-
Parallel[5] = ");
scanf("%d",&port_number);
if ((port_number < 1) || (port_number > 5)) port_number=5;
/* Selection of Baudrate for Serial communication */
sending_delay=10;
if (port_number != 5)
{
printf("GPC card BAUD-RATE : 9600[1]/ 4800[2]/ 2400[3] = ");
scanf("%d",&i);
if ((i < 1) || (i > 3)) i=1;
if (i == 3) i=4;
if ((port_number == 1) || (port number == 2)) DIVISOR=12 * i;
else DIVISOR=40 * i;
receiving_delay=3 * i + 1;
}
This program was written in Borland C++. It uses the peripheral devices
such as PC to read M000 to M127 words, stores them in the K000 to K127,
and then compares the two registry values and indicates the results on the
screen using the OK or the FAIL notation. The user may read or
manipulate the various communication function codes and the sent/
received information to control the PLC in various ways.
This program consists of a header, the main program, and various
functions. The buffers and variables needed to store the communication
data are set as global variables, so that the main and various other
functions may reference them.
By using the COM1 and COM2 ports of the computer, serial
communication is possible. By using the GPU-300 card, parallel
communication is also enabled.
The Qs, QAs, RRs, and Rs are handled in the job function. If there is any
communication delay or frame breakdown, retry 3 times, then issue a
communication error.
The procedure of the communication, according to the JobID is:
1.Q sending
2.QA receiving
3.RR sending
4.R receiving
When an error occurs in a frame, a retransmission should be made.
<Main operations of the program>
1. Adjusts the initial communication port and the board rate for
communication. Then initializes the variables.
2. Using the communication function codes, reads the data of the M field,
reads the word values of the M0 to M127 word area. The K registers are
the retentive registers.
3. Uses the communication code to read the data of the K area.
4. Compares the values of the M area and the values of the K area, and
indicates OK when the values are the same.
Beginning of the main program
Select the port of the peripheral device for the communication
:
Serial 9 pins, 25 pins
Parallel GPU-300 parallel port
Select board rate:
9600 bps (max):
4800 bps
2400 bps
Set the communication environment (delay time) for the selected ports.
Note: GPC-300 card port address = 0x0300