test_imprimante_v4.c 8.72 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
/* code contrôle de l'imprimante 
 *
 * EPSON LQ 570 + 
 * P406
 * Buffer size = 8kB
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>

#define SPEED B9600
#define PATH "/dev/usb/lp0"
#define LINE_LENGTH 16

unsigned char ESC=0x1B;
unsigned char AT = 0x40;//@
unsigned char CR = 0x0D;
unsigned char LF = 0x0A;
unsigned char FF = 0x0C;
unsigned char OPAR = 0x28;//(
unsigned char C = 0x43;
unsigned char c = 0x63;
unsigned char NUL = 0;
unsigned char UN = 1;
unsigned char DX = 2;
unsigned char TROIS = 3;
unsigned char QUATRE = 4;
unsigned char NEUF = 0x39;
unsigned char HUIT = 0x38;
unsigned char VINGT = 20;
unsigned char CVS = 0x7F;//127
unsigned char SQ = 0x40; //64
unsigned char v = 0x76;
unsigned char V = 0x56;
unsigned char U = 0x55;
unsigned char DOL = 0x24;//$
unsigned char BSLASH = 0x5C;// "\"
unsigned char SOIXANTE = 60;
unsigned char DOT = 0x2E;// .

unsigned char q = 0x71;
unsigned char k = 0x6B;
unsigned char t = 0x74;
unsigned char r = 0x72;
unsigned char X = 0x58;
unsigned char EQUAL = 0x3D;

unsigned char l = 0x6C;
unsigned char Q = 0x51;


void init_impr(int fd)
{
	int france=1;
	unsigned char R = 0x52;
	write(fd, &ESC, 1); //ESC
	write(fd, &AT, 1); //@ : ESC @ permet d'initialiser l'imprimante

	write(fd,&R,1);
	write(fd,&france,1);

	write(fd, &CR, 1);
	write(fd, &LF, 1);
}

void beep(int fd)
{
	unsigned char BEL = 0x07; //BEL beeper
	write(fd,&BEL,1);
}

void bold(int fd,int on)
{

	unsigned char E = 0x45;
	unsigned char F = 0x46;
	write(fd,&ESC,1);
	if(on==1){
		write(fd,&E,1);
	}
	else{
		write(fd,&F,1);
	}
}

void italic(int fd, int on)
{
	unsigned char IQ = 0x34;
	unsigned char IC = 0x35;
	write(fd,&ESC,1);
	if(on==1){
		write(fd,&IQ,1);
	}
	else{
		write(fd,&IC,1);
	}
}

void double_strike(int fd, int on)
{

	unsigned char G = 0x47;
	unsigned char H = 0x48;
	write(fd,&ESC,1);
	if(on==1){
		write(fd,&G,1);
	}
	else{
		write(fd,&H,1);
	}
}

void paper_out_detector(int fd, int on)
{
	write(fd,&ESC,1);
	if(on==1){
		write(fd,&NEUF,1);
	}
	else{
		write(fd,&HUIT,1);
	}
}

void underline(int fd, int on)
{
	unsigned char UNDERLINE = 0x2D; // -
	write(fd,&ESC,1);
	write(fd,&UNDERLINE,1);
	write(fd,&on,1);
}

void hor_tab(int fd)
{
	unsigned char HT = 0x09;
	write(fd,&HT,1);
}

void ver_tab(int fd)
{
	unsigned char VT = 0x0B;
	write(fd,&VT,1);
}

struct termios saveterm;

int init_serial(char *device,int speed) {			// Fonction pour initialiser le port Serie
	struct termios new;
	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 
	if(fd<0){perror(device); exit(-1);}
	tcgetattr(fd,&saveterm); // save current port settings 
	bzero(&new,sizeof(new));
	new.c_cflag=CLOCAL|CREAD|speed|CS8;
	new.c_iflag=0;
	new.c_oflag=0;
	new.c_lflag=0;  // set input mode (non-canonical, no echo,...) 
	new.c_cc[VTIME]=0; // inter-character timer unused 
	new.c_cc[VMIN]=1; // blocking read until 1 char received 
	tcflush(fd, TCIFLUSH);
	tcsetattr(fd,TCSANOW,&new);
	return fd;
}

void close_serial(int fd) { // fermeture du Port Série
	tcsetattr(fd,TCSANOW,&saveterm);
	close(fd);
}

void multipoint_print_mode(int fd){

	/* Color multipoint font driver P335 */

	printf("Début du mode multipoint\n");

	/* Step 1 : Start Job */

	printf("Initialisation de l'imprimante\n");
	init_impr(fd);

	/* Step 2 : Set Specific Configuration */

	/* Set units ESC ( U */
	/* A4 8.27 x 11.69 inches */

	write(fd,&ESC,1);
	write(fd,&OPAR,1);
	write(fd,&U,1);
	write(fd,&UN,1);
	write(fd,&NUL,1);
	/* Unit is 1/60inch */
	write(fd,&SOIXANTE,1);

	/* Assign character table ESC ( t */

	write(fd,&ESC,1);
	write(fd,&OPAR,1);
	write(fd,&t,1);
	write(fd,&TROIS,1);
	write(fd,&NUL,1);
	/* Select tab 1 */
	write(fd,&NUL,1);
	/* Canada - French */
	write(fd,&HUIT,1);
	write(fd,&NUL,1);

	/* Set page length ESC ( C */

	write(fd,&ESC,1);
	write(fd,&OPAR,1);
	write(fd,&C,1);
	write(fd,&DX,1);
	write(fd,&NUL,1);
	unsigned char CQVN = 189;
	write(fd,&CQVN,1);
	write(fd,&DX,1);

	/* Set page format ESC ( c */

	write(fd,&ESC,1);
	write(fd,&OPAR,1);
	write(fd,&c,1);
	write(fd,&QUATRE,1);
	write(fd,&NUL,1);
	write(fd,&NUL,1);
	write(fd,&CQVN,1);
	write(fd,&DX,1);

	/* Set pitch ESC X, ESC P, ESC M, ESC g */

	/* Set left and right margins ESC l,Q */
	/* Using default configuration */

	/* Set line spacing n/360" */

	/* Step 3 : Adjust Vertical Print Position OPTIONNAL */

	/* Step 4 : Adjust Horizontal Print Position OPTIONNAL */


	/* Step 5 : Output Text */
	//table 0
	write(fd,&ESC,1);
	write(fd,&t,1);
	write(fd,&NUL,1);

	int i=0,cpt_max=100;
	unsigned char data=0x69;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0x55;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0x2A;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0xA2;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);
	//table 1
	write(fd,&ESC,1);
	write(fd,&t,1);
	write(fd,&UN,1);

	data=0x69;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0x55;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0x2A;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0xA2;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	double_strike(fd,1);
	data=0x69;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0x55;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0x2A;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);

	data=0xA2;
	for(i=0;i<cpt_max;i++)
		write(fd,&data,1);
	write(fd,&CR,1);


	/* Final step : End Page & End Job */

	/* Send FF */
	write(fd,&FF,1);
	/* ESC @ */
	write(fd,&ESC,1);
	write(fd,&AT,1);
}

/* RLE Compressed Raster Graphics p.336 */
void rle_mode(int fd){

	/* Step 1 : Start Job */
	write(fd,&ESC,1);
	write(fd,&AT,1);

	/* Step 2 : Enter Raster Graphics Mode */
	write(fd,&ESC,1);
	write(fd,&OPAR,1);
	unsigned char G = 0x47;
	write(fd,&G,1);
	write(fd,&UN,1);
	write(fd,&NUL,1);
	write(fd,&UN,1);
	/* Step 3 : Specific Configuration */
	// default

	/* Step 4 : Horizontal & Vertical Print Position */
	// default

	/* Step 5 : Output Raster Graphics */
	write(fd,&ESC,1);
	write(fd,&BSLASH,1);
	write(fd,&NUL,1);
	write(fd,&NUL,1);

	write(fd,&ESC,1);
	write(fd,&DOT,1);
	write(fd,&NUL,1); // Full graphics mode (no RLE)
	write(fd,&VINGT,1);
	write(fd,&VINGT,1);
	write(fd,&UN,1); // rows of dot graphics 1,8 or 24
	write(fd,&NUL,1); // columns of dot graphics
	write(fd,&QUATRE,1); // horizontal dot count = 1024
	// k = 128
	int cpt = 0,data=0x47;
	for(cpt=0;cpt<127;cpt++)
		write(fd,&data,1);
	data=0x6E;
	for(cpt=0;cpt<127;cpt++)
		write(fd,&data,1);

	/* END */
	write(fd,&CR,1);
	write(fd,&FF,1);
	write(fd,&ESC,1);
	write(fd,&AT,1);	
}


//P297
void bit_image(int fd){

	unsigned int VHUIT = 8;
	unsigned char STAR = 0x2A;
	unsigned char PLUS = 0x2B;
	unsigned int QH = 48;
	unsigned int data1 = 0x5E;
	unsigned int data2 = 0x7F;
	unsigned int data3 = 0x43;
	unsigned int data4 = 0x86;

	unsigned char G = 0x47;
	int i = 0;

	//P176
	//Entering Graphic mode	
	write(fd,&ESC,1);
	write(fd,&OPAR,1);
	write(fd,&G,1);
	write(fd,&UN,1);
	write(fd,&NUL,1);
	write(fd,&UN,1);

	// Set line spacing with ESC + 48
	write(fd,&ESC,1);
	write(fd,&PLUS,1);
	write(fd,&QH,1);

	//Go to starting line
	write(fd,&CR,1);
	write(fd,&LF,1);	
	
	// ESC * for each line of dots P184
	write(fd,&ESC,1);
	write(fd,&STAR,1);
	write(fd,&NUL,1); // m = 0
	write(fd,&VHUIT,1);
	write(fd,&NUL,1);
	/* 8 dot columns, 1 byte per column */
	for(i=0;i<LINE_LENGTH;i++){
		write(fd,&data1,1);
		write(fd,&data2,1);
		write(fd,&data3,1);
		write(fd,&data4,1);
	}

	// CR LF
	write(fd,&CR,1);
	write(fd,&LF,1);	
	
	//New line
	write(fd,&ESC,1);
	write(fd,&STAR,1);
	write(fd,&NUL,1); // m = 0
	write(fd,&VHUIT,1);
	write(fd,&NUL,1);

	data1=0xF1;
	data2=0xF2;
	data3=0xF4;
	data4=0xF8;
	for(i=0;i<LINE_LENGTH;i++){
		write(fd,&data1,1);
		write(fd,&data2,1);
		write(fd,&data3,1);
		write(fd,&data4,1);
	}
	write(fd,&CR,1);
	write(fd,&LF,1);	
	
	//New line
	write(fd,&ESC,1);
	write(fd,&STAR,1);
	write(fd,&NUL,1); // m = 0
	write(fd,&VHUIT,1);
	write(fd,&NUL,1);

	data1=0xF0;
	data2=0x0F;
	data3=0xFA;
	data4=0xAF;
	for(i=0;i<LINE_LENGTH;i++){
		write(fd,&data1,1);
		write(fd,&data2,1);
		write(fd,&data3,1);
		write(fd,&data4,1);
	}
	write(fd,&CR,1);
	write(fd,&LF,1);	
}

int main(void) {
	int mode = 1;
	int fd = init_serial(PATH, SPEED);
	init_impr(fd);
	printf("Debut de controle imprimante\n");
	printf("Choisir un mode d'impression :\n 1 = Multipoint \t 2 = Compressed Raster Graphics \t 3 = Bit Image \n");
	scanf("%d",&mode);
	switch(mode){
		case 1 :
			multipoint_print_mode(fd);
			break;
		case 2 :
			rle_mode(fd);
			break;
		case 3 :
			bit_image(fd);
			break;
	}
	close_serial(fd);
	return 0;
}