The text of a manual · part 16 of 25
Bulletin 5370 CVIM Configurable Vision Input Module Communications Manual: 5 - Using the RS-232 Ports, pages 125 to 133
Pages 125 to 133 of 202, from Bulletin 5370 CVIM Configurable Vision Input Module Communications Manual
The original document is the PDF (723 KB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.
For the Allen-Bradley 5370-CVIM
Page 125
Chapter 5 Using the RS-232 Ports
/* Print options menu on the screen */ do { printf (”\n\nOperations: \n\n”); printf (”0. Echo ’HELLO’\n”); printf (”1. Trigger Tool Set 1\n”); printf (”2. Read Discrete Results tool set 1\n”); printf (”3. Read Results Block 1, toolset 1\n”); printf (”4. Read gage 1 statistics, tool set 1\n”); printf (”5 Trigger tool set 2\n”); printf (”6 Read discrete results, tool set 2\n”); printf (”7 Read results block 1, tool set 2\n”); printf (”8 Read gage statistics, tool set 2\n”); printf (”9 Read configuration\n”); printf (”10 Write configuration\n”); printf (”11 Change to results page, tool set 1\n”); printf (”12 Page up, tool set 1\n”); printf (”13 Page down, tool set 1\n”); printf (”14 Change to results page, tool set 2\n”); printf (”15 Page up, tool set 2\n”); printf (”16 Page down, tool set 2\n”); printf (”\nEnter operation number (0–16) or –1 to quit: ”); /* Convert user string input to a number */ scanf(”%d”, &op_num ); replen = err = 0; switch (op_num) {
/* Initialize control variables */ /* Determine what user selected */ case 0: /* echo hello */ { err = send_message (portnum,”\001\000\001HELLO”,8); if (!err) err = get_message(portnum,reply,&replen); break; case 1: /* trigger tool set 1 */ { err = send_message (portnum,”\011”/004”,2); /* no reply */ break; case 2: /* read discrete results, tool set 1 */ err = send_message (portnum,”\007\000\001\004”,4); if (!err) err = get_message(portnum,reply,&replen); break;
5–55
Page 126
Chapter 5 Using the RS–232 Ports
DF1 Programming Example (cont’d)
case 3: /* read results block 1, tool set 1 */ err = send_message (portnum, ”\007\000\001\020\001”,5); if (!err) { err = get_message(portnum, results, &reslen); if (!err) { printf (”Results block #1:\n”); */ Display the results block */ for (x=0; x<reslen; x+=2) printf (”%04X ”,results[x]*256 + results[x+1]); printf (”\n”); } } break; case 4: err =send_message(portnum,”\026\000\062\004\000\000\000\000” ”\001\000\000\000\000\000\000\000\000\000\000\000”,20); for (x=0; x<5000 x++);
/* give CVIM time to prepare */ if (!err) { err = send_message(portnum, ”\27\0\1”,3)); if (!err) { err = get_message(portnum, results, & reslen); for (x=0; x<5000 x++); /* give CVIM time to prepare */ if (!err) { printf (”Gage #1 Stats \n”); for (x=0; x<reslen; x+=2); printf (”%04x”, results [x] *256 + results [x+1]); printf (”\n”); } } } break; case 5: /* trigger tool set #2 */ err =send_message(portnum,”\011\005”, 2); /* no reply */ break; case 6: /* read discrete results tool set 2 */ err =send_message(portnum,”\007\000\001\005”,4); if (!err) 5–56
Page 127
Chapter 5 Using the RS-232 Ports err = get_message(portnum,reply, & replen); break; case 7: /* read results block 1 tool set 2 */ err =send_message(portnum,”\007\000\001\025\001”,5); if (!err) { err = get_message(portnum,results, & reslen); if (!err) printf (”Results block #1:\n”); /* Display the results block */ for (x=0; x<reslen; x+=2); printf (”%04x”, results [x] *256 + results [x+1]); printf (”\n”); } } break; case 8: err =send_message(portnum,”\026\000\062\004\000\000\000\000” ”\001\000\000\000\000\000\000\000\000\000\000\000”,20); for (x=0; x<5000 x++); /* give CVIM time to prepare */ if (err) { err = send_message(portnum, ”\27\0\1”, 3); if (err) { err = get_message(portnum,results, & reslen); for (x=0; x<5000 x++); /* give CVIM time to prepare */ if (err) { printf (”Gage #1 Stats \n”); for (x=0; x<reslen; x+=2); printf (”%04x”, results [x] *256 + results [x+1]); printf (”\n”); } } } break; case 9:
/* read configuration */ /* read all config. blocks */ err =send_message(portnum,”\6\7\377\377\377\377\377\377\377” ”\377\377\377\377\377\377\377\377\377\377”,19); if (err) { /* read config blocks 1-135 */ for (x - 0 (x<135) && !err; x++) err = get_message(portnum, config [x] & config [x]); 5–57
Page 128
Chapter 5 Using the RS–232 Ports
DF1 Programming Example (cont’d)
if (err) { /* read first template block */ err = get_message(portnum, template [0] & templen [0]); /* Determine how many template blocks follow */ numblocks = template [0] [2]; for (x=1 (X<numblocks) && !err; x++) { err = get_message(portnum, template [x] & templen [x]); } } } break; case 10: /* write configuration */ /* write entire config. */ err =send_message(portnum,”\13\7\377\377\377\377\377\377\377” ”\377\377\377\377\377\377\377\377\377\377”,19); for (x=0; x<5000 x++); /* give CVIM time to prepare */ if (err) { /* write config blocks 1-135 */ for (x - 0 (x<135) && !err; x++) err = send_message(portnum, config [x] & config [x]); if (err) { /* write all template blocks */ numblocks = template [0] [2]; for (x=0 (X<numblocks) && !err; x++) err = send_message(portnum, template [x] & templen [x]); err = get_message(portnum, template [0] & templen [0]); } } break; case 11: err =send_message(portnum,”\013\002\017”, 3); break; case 12: err =send_message(portnum,”\013\002\022”, 3); break; case 13: err =send_message(portnum,”\013\002\023”, 3); break;
5–58
Page 129
Chapter 5 Using the RS-232 Ports case 14: err =send_message(portnum,”\013\002\031”, 3); break; case 15: err =send_message(portnum,”\013\002\034”, 3); break; case 16: err =send_message(portnum,”\013\002\035”, 3); break; default: break; }
/* End switch (op_num) statement */ if (err) printf (”Error code: %04xn”,err); if (replen) { printf (”Response ”); for (x=0; x<replen; x++) printf (”%02X”, reply [x]); printf (”n”); } } while (op num>=0); */ End do loop */
} /* 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 send_message (portnum, msg, len) int portnum */ Communications port # */ unsigned character *msg */ Pointer to message data */ int len; */ Length of message */ { int x, ch, chksum, err, retry=3; do { /* if an incoming char is waiting, clear it out */ while (_bios_serialcom(_COM_STATUS, portnum, 0) & 256) _bios_serialcom(_COM_RECEIVE, portnum, 0); 5–59
Page 130
Chapter 5 Using the RS–232 Ports
DF1 Programming Example (cont’d)
/* 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<len; 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 */ 5–60
Page 131
Chapter 5 Using the RS-232 Ports
{ int good_string = 0; int message_started = 0; int ch, err, retry=4; int length = 0; unsigned char *msg_start_ptr, df1_bcc; msg_start_ptr = msg; while ( !good_string && retry ) { ch = _bios_serialcom(_COM_RECEIVE,portnum, 0); err |= ch & COMMFLAGS; if (( ch & 0xFF ) == DLE ) { ch = _bios_serialcom(_COM_RECEIVE,portnum,0); err |= ch & COMMFLAGS; switch( ch & 0xFF ) { case STX: message_started = 1; break; case ETX: message_started = 0; ch = _bios_serialcom(_COM_RECEIVE,portnum,0); err |= ch & COMMFLAGS; df1_bcc = –df1_bcc; if (( (ch & 0xFF) == df1_bcc ) && !err ) { _bios_serialcom(_COM_SEND,portnum,DLE); _bios_serialcom(_COM_SEND,portnum,ACK); last responsem = 1; good_string = 1; } else { _bios_serialcom(_COM_SEND,portnum,DLE); _bios_serialcom(_COM_SEND,portnum,NAK); if ( ––retry ) { err = 0; length = 0; df1_bcc = 0; msg = msg_start_ptr; } } break; 5–61
Page 132
Chapter 5 Using the RS–232 Ports
DF1 Programming Example (cont’d)
case DLE: if ( message_started ) { if ( ++length > MAX_BUFFER ) { _bios_serialcom(_COM_SEND,portnum,DLE); _bios_serialcom(_COM_SEND,portnum,NAK); last_response = 0; message_started = 0; err = 0; length = 0; df1_bcc = 0; msg = msg_start_ptr; } else { *msg++ = ( ch & 0xFF ); df1_bcc += ( ch & 0xFF ); } } break; case ENQ: if ( last_response ) { _bios_serialcom(_COM_SEND,portnum,DLE); _bios_serialcom(_COM_SEND,portnum,NAK); } else { _bios_serialcom(_COM_SEND,portnum,DLE); _bios_serialcom(_COM_SEND,portnum,NAK); } break; default: break; } } else if ( message_started ) { if ( ++length > MAX_BUFFER ) { _bios_serialcom(_COM_SEND,portnum,DLE); _bios_serialcom(_COM_SEND,portnum,NAK); last_response = 0; message_started = 0; err = 0;
5–62
Page 133
Chapter 5 Using the RS-232 Ports length = 0; df1_bcc = 0; msg = msg_start_ptr; } else { *msg++ = ( ch & 0xFF ); df1_bcc += ( ch & 0xFF ); } } } *len = length; return (err); }
5–63