Rockwell Automation 5370 Color CVIM Communications Manual User Manual

Page 134

Advertising
background image

Chapter 5
Using the RS–232 Ports

5–66

} while (op_num >= 0); /* End do loop */

}

int send_message (portnum, msg, len)

int portnum; /* Communications port # */

unsigned char *msg; /* Pointer to message data */

int len; /* Length of message */

/* Transmits the message pointed to by msg, consisting of len characters.

If the message is not acknowledged, re–transmits it up to 2 more

times.

Returns zero if successful or nonzero if an error occurs. */

{

int x=0, ch=0, chksum=0, err=0, retry=3;

do {

/* if an incoming char is waiting, clear it out */

while (_bios_serialcom(_COM_STATUS, portnum–1, 0) & 256)

_bios_serialcom(_COM_RECEIVE, portnum–1, 0);

/* send DLE STX to initiate message transfer */

_bios_serialcom(_COM_SEND,portnum–1,DLE);

_bios_serialcom(_COM_SEND,portnum–1,STX);

/* Send all bytes of the selected command & compute checksum */

for (x=chksum=0; x<len; x++)

{

_bios_serialcom(_COM_SEND,portnum–1,msg[x]);

chksum += msg[x];

/* If data within the message was 0x10 (a DLE), send another

DLE so the receiver knows it is data, not a control

character.

(But don’t include the second DLE in the checksum) */

if (msg[x] == DLE)

_bios_serialcom(_COM_SEND,portnum–1,msg[x]);

}

/* send DLE ETX and CHKSUM */

_bios_serialcom(_COM_SEND,portnum–1,DLE);

_bios_serialcom(_COM_SEND,portnum–1,ETX);

_bios_serialcom(_COM_SEND,portnum–1,(–chksum)&0xFF);

/* check for DLE ACK */

ch = _bios_serialcom(_COM_RECEIVE,portnum–1,0);

err = ch & COMMFLAGS;

if (err || ((ch & 0xFF) != DLE)) /* Mask character to 8 bits */

Advertising