test_impr.c
3.36 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
#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>
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);
}
int main(void) {
int fd = init_serial("/dev/usb/lp0", 9600);
int cpt_data = 0;
printf("Debut de contrôle imprimante\n");
unsigned char c=0x1B; //ESC
//unsigned char d = 0x40; //@
unsigned char e = 0x0D; //CR
unsigned char f = 0x0A; //LF
unsigned char g = 0x28; // (
unsigned char h = 0x47; // G
unsigned char i = 0x01; //1
unsigned char j = 0x00; //0
unsigned char de = 0x02; //2
unsigned char s = 0x7F; //127
unsigned char v = 0x40; //64
unsigned char ae = 0x56; //V
unsigned char af = 0x55; //U
unsigned char p = 0x2E; //.
unsigned char hu = 0x08;
unsigned char full_data = 0xFF;
unsigned char empty_data = 0x00;
unsigned char semi_data = 0x55;
unsigned char data = 0x0F;
unsigned char data_t = 0xF0;
unsigned char data_o = 0xA1;
unsigned char data_a = 0x22;
unsigned char data_p = 0xB7;
unsigned char fin = 0x0C; //FF
unsigned char nh = 0xC8;
write(fd, &c, 1); //ESC
//write(fd, &d, 1); //@ : ESC @ permet d'initialiser l'imprimante
//write(fd,&e, 1);//CR
//write(fd, &f, 1);
write(fd, &g, 1); // (
write(fd, &h, 1);
write(fd, &i, 1);
write(fd, &j, 1);
write(fd, &i, 1); //ESC ( G 1 0 1 pour passer en mode graphique
write (fd, &c,1);
write(fd, &g, 1);
write(fd, &af, 1);
write(fd, &i, 1);
write(fd, &j, 1);
write(fd, &i, 1);
write(fd, &j, 1); //ESC ( U 10 10 dot density
write(fd, &c, 1);
write(fd, &g,1);
write(fd, &ae, 1);
write(fd, &de, 1);
write(fd, &i, 1);
write(fd, &s,1);
write(fd, &v, 1); //ESC ( V 2 0 127 64 position de depart
write(fd, &c, 1);
write(fd, &p, 1);
write(fd, &j, 1);
write(fd, &i, 1);
write(fd, &j, 1);
write(fd, &i, 1);
write(fd, &j, 1);
write(fd, &hu, 1);
write(fd, &i, 1);
write(fd, &j, 1);
write(fd, &c, 1);
write(fd, &p, 1);
write(fd, &j, 1);
write(fd, &i, 1);
write(fd, &j, 1);
write(fd, &i, 1);
write(fd, &j, 1);
write(fd, &hu, 1);
write(fd, &i, 1);
write(fd, &nh, 1);
while (cpt_data < 200) {
write(fd, &(full_data), 1);
write(fd, &(data_o), 1);
write(fd, &(data), 1);
cpt_data++;
}
cpt_data = 0;
while(cpt_data < 200) {
write(fd, &(semi_data), 1);
write(fd, &(data_o), 1);
write(fd, &(full_data), 1);
cpt_data++;
}
cpt_data = 0;
while (cpt_data < 200) {
write(fd, &(data), 1);
write(fd, &(data_p), 1);
write(fd, &(data_t), 1);
cpt_data++;
}
cpt_data = 0;
while(cpt_data < 200) {
write(fd, &(data_p), 1);
write(fd, &(full_data), 1);
write(fd, &(semi_data), 1);
cpt_data++;
}
close_serial(fd);
return 0;
}