Rockwell Automation 1771-DMC_DMC1_DMC4_DXPS Control Coprocessor User Manual User Manual
Page 116

Chapter 7
Using the Serial Ports
7-8
/* Now set up the options on the port for:
No Echo
No Pause
Backspace Char = 0x7F (DEL)
EOF Char = 0x1A (ctrl-z)
7 data bits, even parity bit, 2 stop bits */
/* Get the options on the path, change the ones which need to be
changed, and then send the option buffer back to the path
descriptor */
sts = _gs_opt ( path, &opts_buffer );
if ( sts == -1 )
{
printf ( “BARCODE::Can’t get port options!\n” );
exit ( errno );
}
opts_buffer.sg_pause = 0x00; /* No pause */
opts_buffer.sg_echo = 0x00; /* No echo */
opts_buffer.sg_bspch = 0x7f; /* DEL for bksp */
opts_buffer.sg_eofch = 0x1a; /*
opts_buffer.sg_parity = 0x27; /* 2 stop, 7 data, even parity */
sts = _ss_opt ( path, &opts_buffer );
if ( sts == -1 )
{
printf ( “BARCODE::Can’t set port options!\n” );
exit ( errno );
}
/* Set the RTS line to the Enabled state... */
sts = _ss_enrts ( path );
/* Now flush the port of any garbage input from power-up. Ditch it
all to the bit bucket.... */
sts = _gs_rdy ( path ); /* number of chars waiting at port... */
for ( i = 0; i < sts; i++ )
read ( path, &c, 1 ); /* throw each char. away */
/* From here on out, we have the port; get whatever comes in from the
bar code reader and parse it for info; then go to the appropriate
code to distribute the data received. */
while ( 1 )
{
/* Read a “line” of characters from the barcode reader.
Since the reader terminates its send with a
readln() function will do the job just fine! */
printf ( “Waiting for a read...\n” );
recd_chars = readln ( path, barc_buf, BARC_BUF_SIZE );
if ( recd_chars <= 0 )
{
/* end of file, or some other error...
go back and grab port again */
printf ( “BARCODE::Bar Code Port EOF or Error.\n” );
printf ( “ Attempting to reopen port...\n” );
close ( path );
goto reopen;
}
/* The last character in the buffer is a carriage return:
overwrite it with a 0 to terminate the string... */
barc_buf[recd_chars-1] = 0;
/* Now handle the received barcode. If some unrecoverable
error, exit the program and tell why... */
printf ( “Tag: %s read...\n”, barc_buf );
sts = handle_barcode ( barc_buf );