beautypg.com

Df1 programming example (cont’d) – Rockwell Automation 5370 CVIM Communications Manual User Manual

Page 130

background image

Chapter 5

Using the RS–232 Ports

5–60

/* send DLE STX to initiate message transfer */

_bios_serialcom(_COM_SEND,portnum,DLE);

_bios_serialcom(_COM_SEND,portnum,STX);

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

for (x=chksum=0; x

{

_bios_serialcom(_COM_SEND,portnum, 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,msg[x]);

}

/* send DLE ETX and CHKSUM */

_bios_serialcom(_COM_SEND,portnum,DLE);

_bios_serialcom(_COM_SEND,portnum,ETX);

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

/* check for DLE ACK */

ch = _bios_serialcom(_COM_RECEIVE,portnum,0);

err = ch & COMMFLAGS;

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

err |= 0x01; /* no DLE on send */

if (!err)

{

ch = _bios_serialcom(_COM_RECEIVE,portnum,0);

err = ch & COMMFLAGS;

/* Mask character to 8 bits

if (err || ((ch & 0xFF) != ACK)) */

err |= 0x02; /* no ACK on send */

}

} while (err && (––retry > 0)); /* if any error, retry */

return (err);

}

/* Receives a message into buffer pointed to by msg & places

length in *len. If the checksum is invalid or a timeout occurs,

sends NAK and attempts to re–receive up to 2 more times.

Returns zero on success or nonzero if an error occurs. */

int get_message (portnum, msg, len)

int portnum; /* Serial port number */

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

int *len; /* Pointer to return length of message */

DF1 Programming
Example (cont’d)