src/main.c

Wed, 01 Apr 2009 22:11:05 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 01 Apr 2009 22:11:05 +0100
changeset 0
0eef8cf74b80
child 3
4aec27d9d4da
permissions
-rw-r--r--

initial commit

philpem@0 1 #include <stdio.h>
philpem@0 2 #include <stdlib.h>
philpem@0 3 #include "hexdump.h"
philpem@0 4
philpem@0 5 #define ESC 0x1b
philpem@0 6
philpem@0 7 int main(int argc, char **argv)
philpem@0 8 {
philpem@0 9 FILE *prn;
philpem@0 10
philpem@0 11 // check command line args
philpem@0 12 if (argc < 2) {
philpem@0 13 printf("ERROR: must specify device name\n");
philpem@0 14 return -1;
philpem@0 15 }
philpem@0 16
philpem@0 17 // open printer device
philpem@0 18 if ((prn = fopen(argv[1], "r+b")) == NULL) {
philpem@0 19 printf("ERROR: couldn't open printer device '%s'\n", argv[1]);
philpem@0 20 return -1;
philpem@0 21 }
philpem@0 22
philpem@0 23 // INITIALISE
philpem@0 24 fprintf(prn, "%c%c", ESC, '@');
philpem@0 25
philpem@0 26 // REQUEST STATUS
philpem@0 27 fprintf(prn, "%c%c%c", ESC, 'i', 'S');
philpem@0 28
philpem@0 29 // Read status buffer from printer
philpem@0 30 unsigned char buf[32];
philpem@0 31 int timeout = 128;
philpem@0 32 do {
philpem@0 33 fread(buf, 1, 32, prn);
philpem@0 34 } while ((buf[0] != 0x80) && (timeout-- > 0));
philpem@0 35
philpem@0 36 if (timeout > 0) {
philpem@0 37 printf("Printer status:\n");
philpem@0 38 hex_dump(buf, 32);
philpem@0 39 } else {
philpem@0 40 printf("TIMEOUT\n");
philpem@0 41 return -1;
philpem@0 42 }
philpem@0 43
philpem@0 44 // Close the printer stream
philpem@0 45 fclose(prn);
philpem@0 46 return 0;
philpem@0 47 }