POSTNET Programs

BASIC Version

1 ' Jiffy-Ya Zipcoder Program by Marshall Plann
10 WIDTH "lpt1:",255
20 K2 = 6 ' Thickness of the stripes
30 K1 = 5 ' Thickness of the gaps
40 SUM = 0
50 PRINT "Enter Zip Code : "
60 INPUT A$ : L = LEN(A$)
70 ' initialize Printer and print first long bar
80 GOSUB 250 : GOSUB 370
90 ' process each digit
100 FOR I = 1 TO L: Z$ = MID$(A$,I,1) : GOSUB 190: NEXT I
110 ' calculate and print check sum
120 IF (SUM < 10) GOTO 130 ELSE SUM = SUM - 10 : GOTO 120
130 IF NOT (SUM = 0) THEN SUM = 10 - SUM
140 Z$ = CHRS$(SUM + ASC("0")) : GOSUB 190
150 ' print last long bar
160 GOSUB 370
170 LPRINT : LPRINT
180 END
190 IF (Z$ = "0") THEN GOSUB 570
200 IF (Z$ = "-") THEN RETURN  ' Ignore dashes (-)
210 DIGIT = ASC(Z$) - ASC("0") : SUM = SUM + DIGIT
220 ' Case Statement for each digit 1-9
230 ON DIGIT GOSUB 390,410,430,450,470,490,510,530,550
240 RETURN
250 ' Initialize the printer for the correct number of  bytes
260 OPEN "lpt1:" AS #1
270 N = 5 * (K1 + K2) ' Set width of a digit in dots
280 RETURN
290 ' Print a long Bar then a space
300 FOR J = 1 TO K1: PRINT #1, CHR$(255); : NEXT J
310 FOR J = 1 TO K2: PRINT #1, CHR$(0); : NEXT J : RETURN
320 ' Print a Short Bar then a space
330 FOR J = 1 TO K1: PRINT #1, CHR$(7); : NEXT J
340 FOR J = 1 TO K2: PRINT #1, CHR$(0); : NEXT J : RETURN
350 ' TELL PRINTER TO RECEIVE ENOUGH BYTES FOR A DIGIT
360 PRINT #1, CHR$(27) + "Z" + CHR$(N) + CHR$(0); : RETURN
370 ' PRINT A LONG ALONE
380 PRINT #1, CHR$(27) + "Z" + CHR$(K1 + K2) + CHR$(0); : GOSUB 290 : RETURN
390 ' PRINT A 1
400 GOSUB 350 : GOSUB 320 : GOSUB 320 : GOSUB 320 : GOSUB 290 : GOSUB 290 : RETURN
410 ' PRINT A 2
420 GOSUB 350 : GOSUB 320 : GOSUB 320 : GOSUB 290 : GOSUB 320 : GOSUB 290 : RETURN
430 ' PRINT A 3
440 GOSUB 350 : GOSUB 320 : GOSUB 320 : GOSUB 290 : GOSUB 290 : GOSUB 320 : RETURN
450 ' PRINT A 4
460 GOSUB 360 : GOSUB 320 : GOSUB 290 : GOSUB 320 : GOSUB 320 : GOSUB 290 : RETURN
470 ' PRINT A 5
480 GOSUB 350 : GOSUB 320 : GOSUB 290 : GOSUB 320 : GOSUB 290 : GOSUB 320 : RETURN
490 ' PRINT A 6
500 GOSUB 350 : GOSUB 320 : GOSUB 290 : GOSUB 290 : GOSUB 320 : GOSUB 320 : RETURN
510 ' PRINT A 7
520 GOSUB 350 : GOSUB 290 : GOSUB 320 : GOSUB 320 : GOSUB 320 : GOSUB 290 : RETURN
530 ' PRINT A 8
540 GOSUB 350 : GOSUB 290 : GOSUB 320 : GOSUB 320 : GOSUB 290 : GOSUB 320 : RETURN
550 ' PRINT A 9
560 GOSUB 350 : GOSUB 290 : GOSUB 320 : GOSUB 290 : GOSUB 320 : GOSUB 320 : RETURN
570 ' PRINT A 0
580 GOSUB 350 : GOSUB 290 : GOSUB 290 : GOSUB 320 : GOSUB 320 : GOSUB 320 : RETURN

C Version

/* zipbar.c for surf-mail */
/* by Marshall Plann      */
/* compiled fine in TC++  */
/* 12/91                  */

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>

#define PRINTER_PORT  "lpt1"
#define ESC      27
#define LONG     255
#define SHORT    7
#define SPACE    0
#define K1       7              /* width of a space */
#define K2       4              /* width of a bar */

void write_bars();
void bar_code();
int code_digit();

unsigned char digit_bits[] = { 24, 3, 5, 6, 9, 10, 12, 17, 18, 20 };

main(argc, argv)
int argc;
char *argv[];
{
  int printer;                  /* file to write to */
  char string[256];

  /* first parameter is the zip code or else exit */
  if (argc < 2) {
    printf("Usage: %s zipcode\n" argv[0]);
    exit(-1);
  }

  /* open the printer port */
  if ((printer = open(PRINTER_PORT, O_WRONLY)) == -1) {
    printf("Error opening %s\n", PRINTER_PORT);
    exit(1);
  }

  strcpy(string, argv[1]);

  bar_code(printer, string);    /* print the bar code */
  write(printer, "\n", 1);      /* print a new line */

  close(printer);
  return 0;
}

void bar_code(printer, str)
int printer;
char str[];
{

  char out_str[256];
  int i;
  int digit;
  int count = 0;
  int sum = 0;
  int len = strlen(str);
/* add leading bar */
  count += code_end(&(out_str[count]));
  /* go through the string and  create codes for digits */
  for (i = 0; i < len; i++) {
    if (isdigit(str[i])) {      /* character is a digit */
      digit = str[i] - '0';
      sum += digit;             /* accumulate for checksum */
      if (count > 128) {        /* dump every 128 bytes or so to the printer */
        out_str[count++] = '\0';
        write_bars(printer, out_str, count);
        count = 0;
      }                         /* end if */
      /* code the next digit */

      count += code_digit(digit, &(out_str[count]));
    }                           /* end if */
  }                             /* end for */

  /* generate the checksum     */
  if (sum > 0) {
    count += code_digit((10 - (sum % 10)) % 10, &(out_str[count]));
  }
  /* add trailing bar */
  count += code_end(&(out_str[count]));
  out_str[count++] = '\0';
  write_bars(printer, out_str, count);
}

void write_bars(printer, str, count)
int printer;
char str[];
int count;
{
  char out_str[64];
  int num = 0;
  out_str[num++] = ESC;
  out_str[num++] = '2';
  out_str[num++] = count;
  out_str[num++] = 0;
  out_str[num] = '\0';
  write(printer, out_str, num); /* prepare printer for data */
  write(printer, str, count);   /* write data to printer    */
}

int code_digit(digit, str)
int digit;
char str[];
{
  int i, j, k;
  for (i = 4, k = 0; i >= 0; i--) {

/* use digit_bits as a template
for the bar codes.
If a bit is on then add a long bar.
add a short bar otherwise.
*/

    if ((digit_bits[digit] >> i) & 1) {
      for (j = 0; j < K2; str[k++] = LONG, j++);
    } else {
      for (j = 0; j < K2; str[k++] = SHORT, j++);
    }
    for (j = 0; j < K1; str[k++] = SPACE, j++);
  }
  return k;                     /* number of bytes added */
}

/* adds beginning or trailing bar */
int code_end(str)
char str[];
{
  int j, k = 0;
  for (j = 0; j < K2; str[k++] = LONG, j++);
  for (j = 0; j < K1; str[k++] = SPACE, j++);
  return k;                     /* number of bytes added */
}

Code: zipbar.bas

Code: zipbar.c

Return to $2600 Index