Cover V10, I05

may2001.tar


Listing 1 bc_demo.c

 * demo program for controlling bar code printers
 * Author: Ed Schaefer
 */
#include    <stdio.h>
#include    <string.h>

/* function declarations */
int bc_tcpip(char *net_addr, int port, int choice);
int bc_serial(char *device,  int baudrate, int choice);
int write_load_commands(char *load_lines[], int slpr_fd);
int write_port_data(char lprdata[], int tlpr_fd);
int set_terminal(int slpr_fd, char *baudrate);
int open_network_device(char *network_address, int port_number);

void main(int argc, char *argv[])
{
   char *ptr;
   int  i;

   struct demo_programs
      {
      char *keyword;    /* what type printer and type*/
      int (*fptr)(char *, int, int); /* function pointer */
      char *lpath;  /* argument 1, device or network address */
      int rate;     /* argument 2, baud rate or port number */
      int choice;   /* argument 3, 1=intermec, 2=zebra */
      };

   static struct demo_programs printer_choice[] =
      {
      "intermectcpip",  bc_tcpip,  "intermec.eds.com", 3001,  1,
      "zebratcpip",       bc_tcpip,  "zebra.eds.com",   9100,  2,
      "intermecserial", bc_serial, "/dev/term/a",        9600,  1, 
      "zebraserial",      bc_serial,  "/dev/term/b",       9600,  2,
      (char *) NULL
      };

   if(argc == 1)
      {
      printf("Usage: ");
      printf("bc_demo intermectcpip intermecserial \
zebra_tcpip zebraserial \n");
      exit(1);
      } 

   /* process all command line parameters  and choose a printer*/
   while(--argc)
      {
      ptr = *++argv; 
      for(i=0; printer_choice[i].keyword; i++) 
         if(strcmp(printer_choice[i].keyword, ptr)==0)
            (*(program[i].fptr))(printer_choice[i].lpath, printer_choice[i].rate, 
               printer_choice[i].choice);
      }
   exit(0);
}

/*
 * This function writes a string, a character at a time,
 * low level to a previously opened TCP/IP or serial device.
 * lprdata char string must be null terminated.
 */
int write_port_data(char lprdata[], int tlpr_fd)
{
   int i,n;

   i=0;
   while(lprdata[i] != '\0')
      {
      n=write(tlpr_fd,&lprdata[i++],1);
      if(n != 1) /* error if can't write */
        return 0;
      }
   return 1;
} 

/*
 * This function writes out an array of character pointers-
 * to-strings to previously opened TCP/IP or serial device.
 * Make sure the array is terminated with (char *) NULL
 */
int write_load_commands(char *load_lines[], int slpr_fd)
{
   int i;

   for(i=0; load_lines[i]; i++)
      if(!write_port_data(load_lines[i], slpr_fd)) 
         {
         printf("Can NOT write to printer\n"); 
         exit(1);
         }
} /* end bc_demo.c */

/*