Commit a8f4b0eb396099ebb95940491d04306c144856a5

Authored by aarnaude
1 parent 8992527e

imprimante

Showing 3 changed files with 678 additions and 0 deletions   Show diff stats
Makefile 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +CC = gcc
  2 +CFLAGS = -Wall -Wextra
  3 +
  4 +imprimante :
  5 + $(CC) $(CFLAGS) test_imprimante_v4.c -o imprimante
... ...
test_imprimante_v3.c 0 → 100644
... ... @@ -0,0 +1,278 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <unistd.h>
  4 +#include <termios.h>
  5 +#include <sys/types.h>
  6 +#include <sys/stat.h>
  7 +#include <fcntl.h>
  8 +#include <strings.h>
  9 +
  10 +#define SPEED B9600
  11 +#define PATH "/dev/usb/lp1"
  12 +
  13 +unsigned char ESC=0x1B;
  14 +unsigned char AT = 0x40;//@
  15 +unsigned char CR = 0x0D;
  16 +unsigned char LF = 0x0A;
  17 +unsigned char FF = 0x0C;
  18 +unsigned char OPAR = 0x28;//(
  19 +unsigned char C = 0x43;
  20 +unsigned char c = 0x63;
  21 +unsigned char NUL = 0;
  22 +unsigned char UN = 1;
  23 +unsigned char DX = 2;
  24 +unsigned char TROIS = 3;
  25 +unsigned char QUATRE = 4;
  26 +unsigned char CVS = 0x7F;//127
  27 +unsigned char SQ = 0x40; //64
  28 +unsigned char v = 0x76;
  29 +unsigned char V = 0x56;
  30 +unsigned char U = 0x55;
  31 +unsigned char DOL = 0x24;//$
  32 +unsigned char BSLASH = 0x5C;// "\"
  33 +unsigned char SOIXANTE = 60;
  34 +
  35 +unsigned char q = 0x71;
  36 +unsigned char k = 0x6B;
  37 +unsigned char t = 0x74;
  38 +unsigned char r = 0x72;
  39 +unsigned char X = 0x58;
  40 +unsigned char EQUAL = 0x3D;
  41 +
  42 +unsigned char l = 0x6C;
  43 +unsigned char Q = 0x51;
  44 +
  45 +
  46 +void init_impr(int fd)
  47 +{
  48 + int france=1;
  49 +
  50 + write(fd, &ESC, 1); //ESC
  51 + write(fd, &AT, 1); //@ : ESC @ permet d'initialiser l'imprimante
  52 +
  53 + write(fd,&R,1);
  54 + write(fd,&france,1);
  55 +
  56 + write(fd, &CR, 1);
  57 + write(fd, &LF, 1);
  58 +}
  59 +
  60 +void graphic_mode(int fd)
  61 +{
  62 + write(fd,&ESC,1);
  63 + write(fd,&OPAR,1);
  64 + write(fd,&G,1);
  65 + write(fd,&UN,1);
  66 + write(fd,&NUL,1);
  67 + write(fd,&UN,1);
  68 +}
  69 +
  70 +/*void print_data(int fd, int data)
  71 +{
  72 + //ESC ( ^
  73 +
  74 +
  75 +}*/
  76 +
  77 +void beep(int fd)
  78 +{
  79 + unsigned char BEL = 0x07; //BEL beeper
  80 + write(fd,&BEL,1);
  81 +}
  82 +
  83 +void bold(int fd,int on)
  84 +{
  85 + write(fd,&ESC,1);
  86 + if(on==1){
  87 + write(fd,&E,1);
  88 + }
  89 + else{
  90 + write(fd,&F,1);
  91 + }
  92 +}
  93 +
  94 +void italic(int fd, int on)
  95 +{
  96 + write(fd,&ESC,1);
  97 + if(on==1){
  98 + write(fd,&IQ,1);
  99 + }
  100 + else{
  101 + write(fd,&IC,1);
  102 + }
  103 +}
  104 +
  105 +void double_strike(int fd, int on)
  106 +{
  107 + write(fd,&ESC,1);
  108 + if(on==1){
  109 + write(fd,&G,1);
  110 + }
  111 + else{
  112 + write(fd,&H,1);
  113 + }
  114 +}
  115 +
  116 +void paper_out_detector(int fd, int on)
  117 +{
  118 + write(fd,&ESC,1);
  119 + if(on==1){
  120 + write(fd,&NEUF,1);
  121 + }
  122 + else{
  123 + write(fd,&HUIT,1);
  124 + }
  125 +}
  126 +
  127 +void underline(int fd, int on)
  128 +{
  129 + unsigned char UNDERLINE = 0x2D; // -
  130 + write(fd,&ESC,1);
  131 + write(fd,&UNDERLINE,1);
  132 + write(fd,&on,1);
  133 +}
  134 +
  135 +void hor_tab(int fd)
  136 +{
  137 + unsigned char HT = 0x09;
  138 + write(fd,&HT,1);
  139 +}
  140 +
  141 +void ver_tab(int fd)
  142 +{
  143 + unsigned char VT = 0x0B;
  144 + write(fd,&VT,1);
  145 +}
  146 +
  147 +struct termios saveterm;
  148 +
  149 +int init_serial(char *device,int speed) { // Fonction pour initialiser le port Serie
  150 + struct termios new;
  151 + int fd=open(device,O_RDWR|O_NOCTTY);// On ouvre un descripteur de fichier en mode lecture et ecriture, ici ça sera notre port série
  152 + if(fd<0){perror(device); exit(-1);}
  153 + tcgetattr(fd,&saveterm); // save current port settings
  154 + bzero(&new,sizeof(new));
  155 + new.c_cflag=CLOCAL|CREAD|speed|CS8;
  156 + new.c_iflag=0;
  157 + new.c_oflag=0;
  158 + new.c_lflag=0; // set input mode (non-canonical, no echo,...)
  159 + new.c_cc[VTIME]=0; // inter-character timer unused
  160 + new.c_cc[VMIN]=1; // blocking read until 1 char received
  161 + tcflush(fd, TCIFLUSH);
  162 + tcsetattr(fd,TCSANOW,&new);
  163 + return fd;
  164 +}
  165 +
  166 +void close_serial(int fd) { // fermeture du Port Série
  167 + tcsetattr(fd,TCSANOW,&saveterm);
  168 + close(fd);
  169 +}
  170 +
  171 +void multipoint_print_mode(int fd){
  172 +
  173 +/* Color multipoint font driver P335 */
  174 +
  175 + printf("Début du mode multipoint\n");
  176 +
  177 +/* Step 1 : Start Job */
  178 +
  179 + printf("Initialisation de l'imprimante\n");
  180 + init_impr(fd);
  181 +
  182 +/* Step 2 : Set Specific Configuration */
  183 +
  184 + /* Set units ESC ( U */
  185 + /* A4 8.27 x 11.69 inches */
  186 +
  187 + write(fd,&ESC,1);
  188 + write(fd,&OPAR,1);
  189 + write(fd,&U,1);
  190 + write(fd,&UN,1);
  191 + write(fd,&NUL,1);
  192 + /* Unit is 1/60inch */
  193 + write(fd,&SOIXANTE,1);
  194 +
  195 + /* Assign character table ESC ( t */
  196 +
  197 + write(fd,&ESC,1);
  198 + write(fd,&OPAR,1);
  199 + write(fd,&t,1);
  200 + write(fd,&TROIS,1);
  201 + write(fd,&NUL,1);
  202 + /* Select tab 1 */
  203 + write(fd,&NUL,1);
  204 + /* Canada - French */
  205 + write(fd,&HUIT,1);
  206 + write(fd,&NUL,1);
  207 +
  208 + /* Set page length ESC ( C */
  209 +
  210 + write(fd,&ESC,1);
  211 + write(fd,&OPAR,1);
  212 + write(fd,&C,1);
  213 + write(fd,&DX,1);
  214 + write(fd,&NUL,1);
  215 + unsigned char CQVN = 189;
  216 + write(fd,&CQVN,1);
  217 + write(fd,&DX,1);
  218 +
  219 + /* Set page format ESC ( c */
  220 +
  221 + write(fd,&ESC,1);
  222 + write(fd,&OPAR,1);
  223 + write(fd,&c,1);
  224 + write(fd,&QUATRE,1);
  225 + write(fd,&NUL,1);
  226 + write(fd,&NUL,1);
  227 + write(fd,&CQVN,1);
  228 + write(fd,&DX,1);
  229 +
  230 + /* Set pitch ESC X, ESC P, ESC M, ESC g */
  231 +
  232 + /* Set left and right margins ESC l,Q */
  233 + /* Using default configuration */
  234 +
  235 + /* Set line spacing n/360" */
  236 +
  237 +/* Step 3 : Adjust Vertical Print Position OPTIONNAL */
  238 +
  239 +/* Step 4 : Adjust Horizontal Print Position OPTIONNAL */
  240 +
  241 +
  242 +/* Step 5 : Output Text */
  243 +
  244 + write(fd,&ESC,1);
  245 + write(fd,&t,1);
  246 + write(fd,&NUL,1);
  247 +
  248 +
  249 +/* Final step : End Page & End Job */
  250 +
  251 + /* Send FF */
  252 +
  253 + /* ESC @ */
  254 +
  255 +}
  256 +
  257 +int main(void) {
  258 + int fd = init_serial(PATH, SPEED);
  259 + int cpt = 0,i=0;
  260 + printf("Debut de controle imprimante\n");
  261 +
  262 +
  263 +
  264 +
  265 +/* TIFF compressed mode p182 */
  266 +
  267 +/* write(fd,&ESC,1);
  268 + write(fd,&DOT,1);
  269 + write(fd,&DX,1);
  270 + write(fd,&VINGT,1);
  271 + write(fd,&VINGT,1);
  272 + write(fd,&CQV,1);
  273 + write(fd,&UN,1);
  274 + write(fd,&NUL,1);
  275 + write(fd,&NUL,1);
  276 +*/
  277 +return 0;
  278 +}
... ...
test_imprimante_v4.c 0 → 100644
... ... @@ -0,0 +1,395 @@
  1 +/* code contrôle de l'imprimante
  2 + *
  3 + * EPSON LQ 570 +
  4 + * P406
  5 + * Buffer size = 8kB
  6 + *
  7 + * Commandes indisponible pour ce modèle :
  8 + * ESC r : color*/
  9 +
  10 +
  11 +#include <stdio.h>
  12 +#include <stdlib.h>
  13 +#include <unistd.h>
  14 +#include <termios.h>
  15 +#include <sys/types.h>
  16 +#include <sys/stat.h>
  17 +#include <fcntl.h>
  18 +#include <strings.h>
  19 +
  20 +#define SPEED B9600
  21 +#define PATH "/dev/usb/lp0"
  22 +
  23 +unsigned char ESC=0x1B;
  24 +unsigned char AT = 0x40;//@
  25 +unsigned char CR = 0x0D;
  26 +unsigned char LF = 0x0A;
  27 +unsigned char FF = 0x0C;
  28 +unsigned char OPAR = 0x28;//(
  29 +unsigned char C = 0x43;
  30 +unsigned char c = 0x63;
  31 +unsigned char NUL = 0;
  32 +unsigned char UN = 1;
  33 +unsigned char DX = 2;
  34 +unsigned char TROIS = 3;
  35 +unsigned char QUATRE = 4;
  36 +unsigned char NEUF = 0x39;
  37 +unsigned char HUIT = 0x38;
  38 +unsigned char VINGT = 20;
  39 +unsigned char CVS = 0x7F;//127
  40 +unsigned char SQ = 0x40; //64
  41 +unsigned char v = 0x76;
  42 +unsigned char V = 0x56;
  43 +unsigned char U = 0x55;
  44 +unsigned char DOL = 0x24;//$
  45 +unsigned char BSLASH = 0x5C;// "\"
  46 +unsigned char SOIXANTE = 60;
  47 +unsigned char DOT = 0x2E;// .
  48 +
  49 +unsigned char q = 0x71;
  50 +unsigned char k = 0x6B;
  51 +unsigned char t = 0x74;
  52 +unsigned char r = 0x72;
  53 +unsigned char X = 0x58;
  54 +unsigned char EQUAL = 0x3D;
  55 +
  56 +unsigned char l = 0x6C;
  57 +unsigned char Q = 0x51;
  58 +
  59 +
  60 +void init_impr(int fd)
  61 +{
  62 + int france=1;
  63 + unsigned char R = 0x52;
  64 + write(fd, &ESC, 1); //ESC
  65 + write(fd, &AT, 1); //@ : ESC @ permet d'initialiser l'imprimante
  66 +
  67 + write(fd,&R,1);
  68 + write(fd,&france,1);
  69 +
  70 + write(fd, &CR, 1);
  71 + write(fd, &LF, 1);
  72 +}
  73 +
  74 +void beep(int fd)
  75 +{
  76 + unsigned char BEL = 0x07; //BEL beeper
  77 + write(fd,&BEL,1);
  78 +}
  79 +
  80 +void bold(int fd,int on)
  81 +{
  82 +
  83 + unsigned char E = 0x45;
  84 + unsigned char F = 0x46;
  85 + write(fd,&ESC,1);
  86 + if(on==1){
  87 + write(fd,&E,1);
  88 + }
  89 + else{
  90 + write(fd,&F,1);
  91 + }
  92 +}
  93 +
  94 +void italic(int fd, int on)
  95 +{
  96 + unsigned char IQ = 0x34;
  97 + unsigned char IC = 0x35;
  98 + write(fd,&ESC,1);
  99 + if(on==1){
  100 + write(fd,&IQ,1);
  101 + }
  102 + else{
  103 + write(fd,&IC,1);
  104 + }
  105 +}
  106 +
  107 +void double_strike(int fd, int on)
  108 +{
  109 +
  110 + unsigned char G = 0x47;
  111 + unsigned char H = 0x48;
  112 + write(fd,&ESC,1);
  113 + if(on==1){
  114 + write(fd,&G,1);
  115 + }
  116 + else{
  117 + write(fd,&H,1);
  118 + }
  119 +}
  120 +
  121 +void paper_out_detector(int fd, int on)
  122 +{
  123 + write(fd,&ESC,1);
  124 + if(on==1){
  125 + write(fd,&NEUF,1);
  126 + }
  127 + else{
  128 + write(fd,&HUIT,1);
  129 + }
  130 +}
  131 +
  132 +void underline(int fd, int on)
  133 +{
  134 + unsigned char UNDERLINE = 0x2D; // -
  135 + write(fd,&ESC,1);
  136 + write(fd,&UNDERLINE,1);
  137 + write(fd,&on,1);
  138 +}
  139 +
  140 +void hor_tab(int fd)
  141 +{
  142 + unsigned char HT = 0x09;
  143 + write(fd,&HT,1);
  144 +}
  145 +
  146 +void ver_tab(int fd)
  147 +{
  148 + unsigned char VT = 0x0B;
  149 + write(fd,&VT,1);
  150 +}
  151 +
  152 +struct termios saveterm;
  153 +
  154 +int init_serial(char *device,int speed) { // Fonction pour initialiser le port Serie
  155 + struct termios new;
  156 + int fd=open(device,O_RDWR|O_NOCTTY);// On ouvre un descripteur de fichier en mode lecture et ecriture, ici ça sera notre port série
  157 + if(fd<0){perror(device); exit(-1);}
  158 + tcgetattr(fd,&saveterm); // save current port settings
  159 + bzero(&new,sizeof(new));
  160 + new.c_cflag=CLOCAL|CREAD|speed|CS8;
  161 + new.c_iflag=0;
  162 + new.c_oflag=0;
  163 + new.c_lflag=0; // set input mode (non-canonical, no echo,...)
  164 + new.c_cc[VTIME]=0; // inter-character timer unused
  165 + new.c_cc[VMIN]=1; // blocking read until 1 char received
  166 + tcflush(fd, TCIFLUSH);
  167 + tcsetattr(fd,TCSANOW,&new);
  168 + return fd;
  169 +}
  170 +
  171 +void close_serial(int fd) { // fermeture du Port Série
  172 + tcsetattr(fd,TCSANOW,&saveterm);
  173 + close(fd);
  174 +}
  175 +
  176 +void multipoint_print_mode(int fd){
  177 +
  178 + /* Color multipoint font driver P335 */
  179 +
  180 + printf("Début du mode multipoint\n");
  181 +
  182 + /* Step 1 : Start Job */
  183 +
  184 + printf("Initialisation de l'imprimante\n");
  185 + init_impr(fd);
  186 +
  187 + /* Step 2 : Set Specific Configuration */
  188 +
  189 + /* Set units ESC ( U */
  190 + /* A4 8.27 x 11.69 inches */
  191 +
  192 + write(fd,&ESC,1);
  193 + write(fd,&OPAR,1);
  194 + write(fd,&U,1);
  195 + write(fd,&UN,1);
  196 + write(fd,&NUL,1);
  197 + /* Unit is 1/60inch */
  198 + write(fd,&SOIXANTE,1);
  199 +
  200 + /* Assign character table ESC ( t */
  201 +
  202 + write(fd,&ESC,1);
  203 + write(fd,&OPAR,1);
  204 + write(fd,&t,1);
  205 + write(fd,&TROIS,1);
  206 + write(fd,&NUL,1);
  207 + /* Select tab 1 */
  208 + write(fd,&NUL,1);
  209 + /* Canada - French */
  210 + write(fd,&HUIT,1);
  211 + write(fd,&NUL,1);
  212 +
  213 + /* Set page length ESC ( C */
  214 +
  215 + write(fd,&ESC,1);
  216 + write(fd,&OPAR,1);
  217 + write(fd,&C,1);
  218 + write(fd,&DX,1);
  219 + write(fd,&NUL,1);
  220 + unsigned char CQVN = 189;
  221 + write(fd,&CQVN,1);
  222 + write(fd,&DX,1);
  223 +
  224 + /* Set page format ESC ( c */
  225 +
  226 + write(fd,&ESC,1);
  227 + write(fd,&OPAR,1);
  228 + write(fd,&c,1);
  229 + write(fd,&QUATRE,1);
  230 + write(fd,&NUL,1);
  231 + write(fd,&NUL,1);
  232 + write(fd,&CQVN,1);
  233 + write(fd,&DX,1);
  234 +
  235 + /* Set pitch ESC X, ESC P, ESC M, ESC g */
  236 +
  237 + /* Set left and right margins ESC l,Q */
  238 + /* Using default configuration */
  239 +
  240 + /* Set line spacing n/360" */
  241 +
  242 + /* Step 3 : Adjust Vertical Print Position OPTIONNAL */
  243 +
  244 + /* Step 4 : Adjust Horizontal Print Position OPTIONNAL */
  245 +
  246 +
  247 + /* Step 5 : Output Text */
  248 + //table 0
  249 + write(fd,&ESC,1);
  250 + write(fd,&t,1);
  251 + write(fd,&NUL,1);
  252 +
  253 + int i=0,cpt_max=100;
  254 + unsigned char data=0x69;
  255 + for(i=0;i<cpt_max;i++)
  256 + write(fd,&data,1);
  257 + write(fd,&CR,1);
  258 +
  259 + data=0x55;
  260 + for(i=0;i<cpt_max;i++)
  261 + write(fd,&data,1);
  262 + write(fd,&CR,1);
  263 +
  264 + data=0x2A;
  265 + for(i=0;i<cpt_max;i++)
  266 + write(fd,&data,1);
  267 + write(fd,&CR,1);
  268 +
  269 + data=0xA2;
  270 + for(i=0;i<cpt_max;i++)
  271 + write(fd,&data,1);
  272 + write(fd,&CR,1);
  273 + //table 1
  274 + write(fd,&ESC,1);
  275 + write(fd,&t,1);
  276 + write(fd,&UN,1);
  277 +
  278 + data=0x69;
  279 + for(i=0;i<cpt_max;i++)
  280 + write(fd,&data,1);
  281 + write(fd,&CR,1);
  282 +
  283 + data=0x55;
  284 + for(i=0;i<cpt_max;i++)
  285 + write(fd,&data,1);
  286 + write(fd,&CR,1);
  287 +
  288 + data=0x2A;
  289 + for(i=0;i<cpt_max;i++)
  290 + write(fd,&data,1);
  291 + write(fd,&CR,1);
  292 +
  293 + data=0xA2;
  294 + for(i=0;i<cpt_max;i++)
  295 + write(fd,&data,1);
  296 + write(fd,&CR,1);
  297 +
  298 + double_strike(fd,1);
  299 + data=0x69;
  300 + for(i=0;i<cpt_max;i++)
  301 + write(fd,&data,1);
  302 + write(fd,&CR,1);
  303 +
  304 + data=0x55;
  305 + for(i=0;i<cpt_max;i++)
  306 + write(fd,&data,1);
  307 + write(fd,&CR,1);
  308 +
  309 + data=0x2A;
  310 + for(i=0;i<cpt_max;i++)
  311 + write(fd,&data,1);
  312 + write(fd,&CR,1);
  313 +
  314 + data=0xA2;
  315 + for(i=0;i<cpt_max;i++)
  316 + write(fd,&data,1);
  317 + write(fd,&CR,1);
  318 +
  319 +
  320 + /* Final step : End Page & End Job */
  321 +
  322 + /* Send FF */
  323 + write(fd,&FF,1);
  324 + /* ESC @ */
  325 + write(fd,&ESC,1);
  326 + write(fd,&AT,1);
  327 +}
  328 +
  329 +/* RLE Compressed Raster Graphics p.336 */
  330 +void rle_mode(int fd){
  331 +
  332 + /* Step 1 : Start Job */
  333 + write(fd,&ESC,1);
  334 + write(fd,&AT,1);
  335 +
  336 + /* Step 2 : Enter Raster Graphics Mode */
  337 + write(fd,&ESC,1);
  338 + write(fd,&OPAR,1);
  339 + unsigned char G = 0x47;
  340 + write(fd,&G,1);
  341 + write(fd,&UN,1);
  342 + write(fd,&NUL,1);
  343 + write(fd,&UN,1);
  344 + /* Step 3 : Specific Configuration */
  345 + // default
  346 +
  347 + /* Step 4 : Horizontal & Vertical Print Position */
  348 + // default
  349 +
  350 + /* Step 5 : Output Raster Graphics */
  351 + write(fd,&ESC,1);
  352 + write(fd,&BSLASH,1);
  353 + write(fd,&NUL,1);
  354 + write(fd,&NUL,1);
  355 +
  356 + write(fd,&ESC,1);
  357 + write(fd,&DOT,1);
  358 + write(fd,&NUL,1); // Full graphics mode (no RLE)
  359 + write(fd,&VINGT,1);
  360 + write(fd,&VINGT,1);
  361 + write(fd,&UN,1); // rows of dot graphics 1,8 or 24
  362 + write(fd,&NUL,1); // columns of dot graphics
  363 + write(fd,&QUATRE,1); // horizontal dot count = 1024
  364 + // k = 128
  365 + int cpt = 0,data=0x47;
  366 + for(cpt=0;cpt<127;cpt++)
  367 + write(fd,&data,1);
  368 + data=0x6E;
  369 + for(cpt=0;cpt<127;cpt++)
  370 + write(fd,&data,1);
  371 +
  372 + /* END */
  373 + write(fd,&CR,1);
  374 + write(fd,&FF,1);
  375 + write(fd,&ESC,1);
  376 + write(fd,&AT,1);
  377 +}
  378 +
  379 +int main(void) {
  380 + int mode = 1;
  381 + int fd = init_serial(PATH, SPEED);
  382 + printf("Debut de controle imprimante\n");
  383 + printf("Choisir un mode d'impression :\n 1 = Multipoint \t 2 = Compressed Raster Graphics\n");
  384 + scanf("%d",&mode);
  385 + switch(mode){
  386 + case 1 :
  387 + multipoint_print_mode(fd);
  388 + break;
  389 + case 2 :
  390 + rle_mode(fd);
  391 + break;
  392 + }
  393 + close_serial(fd);
  394 + return 0;
  395 +}
... ...