-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
329 lines (307 loc) · 7.26 KB
/
main.c
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
/***********************************************************************
* paralelo.c
*
* Jun 20 2006
* Copyright 2006 Gustavo Cortez
* gustavo@lugtucuman.org.ar
*
***********************************************************************
* Description:
*
* Send data to printer, to another PC and
* receive data from other PC using Parallel Port.
*
************************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <linux/ppdev.h>
#include <string.h>
#include <asm/io.h>
#define BASEPORT 0x378 /* Parallel Port */
#define DATA BASEPORT+0 /* Data Register */
#define STATUS BASEPORT+1 /* Status Register */
#define CONTROL BASEPORT+2 /* Control Register */
void catcher();
void print_to(char * charset);
void reset_printer();
char _status();
char _control();
void send_parallel(char character);
void _ack();
void _busy();
void waiting_connection();
void _server_();
void _client_();
void tx_byte(const char character);
char rx_nibble();
void die_on_error(void) __attribute__ ((noreturn));
int main(int argc, char **argv)
{
/* Set perm to write parallel port */
if (ioperm(BASEPORT, 3, 1))
die_on_error();
if (argc == 1 || argc > 3) {
printf("Send Date To Printer v0.1\n");
printf("Enter argument --help to help list\n");
}
else {
if (!(strcmp(argv[1], "--help"))) {
printf("Send Date To Printer v0.1\n");
printf("\nOptions: \n");
printf("--print\t\tPrint characters set in LPTx\n");
printf("--server\tReceive data through LPTx from other PC\n");
printf("--client\tSend data through LPTx to other PC\n");
printf("--chat\t\tRun in chat mode\n");
printf("--help\t\tPrint this help\n\n");
printf("Example:\n");
printf("\t<command> --print 'Hola mundo'\n\n");
}
else if (!(strcmp(argv[1], "--print"))) {
if (argc < 3) {
printf("Argument error\n");
die_on_error();
}
else {
/* Send characters to printer */
print_to(argv[2]);
}
}
else if (!(strcmp(argv[1], "--server"))) {
if (argc > 2) {
printf("Argument error\n");
die_on_error();
}
else {
/* Call to _server_() procedure.
* Server only receive data from parallel port */
_server_();
}
}
else if (!(strcmp(argv[1], "--client"))) {
if (argc > 2) {
printf("Argument error\n");
die_on_error();
}
else {
/* Call to _client_() procedure.
* Client only send data to parallel port */
_client_();
}
}
else {
printf("Argument error\n");
die_on_error();
}
}
if (ioperm(BASEPORT, 3, 0))
die_on_error();
return 0;
}
void die_on_error(void)
{
perror("ioperm");
exit(EXIT_FAILURE);
}
void catcher(){
int fd;
ioctl(fd, PPRELEASE);
die_on_error();
}
void reset_printer ()
{
char control;
control = _control();
/* Reset printer */
outb(control & 0xfb, CONTROL);
outb(control | 0x04, CONTROL);
printf("Status Register: %x\n",_status());
}
char _status ()
{
char status;
status = inb(STATUS);
return status;
}
char _control()
{
char control;
control = inb(CONTROL);
return control;
}
void print_to (char * charset)
{
int charset_long, i=0;
char control;
control = _control();
/* Signal for end program with Ctrl+c */
signal(SIGINT, catcher);
/* Always reset printer */
reset_printer();
charset_long = strlen(charset);
/* Waiting for connect the printer */
waiting_connection();
printf("\nPrinted characters: ");
for (i; i<charset_long; i++) {
/* Send character to printer */
send_parallel(charset[i]);
printf("%c",charset[i]);
}
printf("\nPrint finished.\n");
}
void send_parallel (char character)
{
char control;
control = _control();
outb(control & 0xfe, CONTROL); // Strobe to low
outb(character, DATA);
outb(control | 0x01, CONTROL); // Strobe to high
}
void _busy()
{
char status;
printf("Busy ");
do {
status = _status();
sleep(1);
printf(".");
}while (status & 0x80);
}
void _ack()
{
char status;
do {
status = _status();
sleep(1);
}while (!(status & 0x40));
}
void waiting_connection()
{
char status;
printf("Waiting for connection ");
while(1) {
status = _status();
if (status & 0x10) break;
printf(".");
sleep(5);
}
printf(" Connected.\n");
}
void _client_()
{
char * word, control;
int word_long, i=0;
word = (char *)malloc(sizeof(char));
control = _control();
/* Signal for end program with Ctrl+c */
signal(SIGINT, catcher);
printf("Client mode:\n");
printf("Type characters to send (or zero to close):\n");
while(1) {
printf("\r> ");
scanf("%s", word);
word_long = strlen(word);
/* Close connection if character is zero '0' */
if (word_long == 1) {
if (word[0] == '0') {
tx_byte(word[i]);
break;
}
}
for(i=0; i<word_long; i++) {
tx_byte(word[i]);
}
}
printf("\nConnection closed.\n");
}
void _server_()
{
char control, status, nibble_high, nibble_low;
char character;
int flag;
control = _control();
/* Signal for end program with Ctrl+c */
signal(SIGINT, catcher);
printf("Server mode:\n");
printf("Data received:\n");
while (1) {
if (!(((inb(STATUS) >> 3) & 0x1f) & 0x10)) {
/* Receive 4 LSB */
nibble_low = rx_nibble();
nibble_low = nibble_low & 0x0f;
/* Send confirmation to client */
outb(0x10,DATA);
/* Receive 4 MSB */
nibble_high = rx_nibble();
nibble_high = nibble_high << 4;
nibble_high = nibble_high & 0x0f;
/* Send confirmation to client */
outb(0x0,DATA);
/* Get MSB and LSB */
character = nibble_high | nibble_low;
/* Print LSB, MSB and character received */
printf("\nNibble_low: %x\n", nibble_low);
printf("Nibble_high: %x\n", nibble_high);
printf("Character (hex): %x\n", character);
printf("Character (chr): %c\n", character);
/* Close connection if character is zero '0' */
if (nibble_high == 0x0 && nibble_low == 0x0) break;
}
}
printf("\nConnection closed.\n");
}
int received_ok()
{
char status;
status = _status();
if(!(status & 0x80))
return 1;
else
return 0;
}
void tx_byte(const char character)
{
char nibble_high, nibble_low;
outb(0x10,DATA);
usleep(10);
nibble_low = character & 0x0f;
outb(nibble_low, DATA);
while(((inb(STATUS) >> 3) & 0x1f) & 0x10) usleep(10000);
nibble_high = character >> 4;
nibble_high = nibble_high & 0x0f;
outb(nibble_high, DATA);
usleep(500000);
if(((inb(STATUS) >> 3) & 0x1f) & 0x10) {
usleep(100000);
outb(0x0,DATA);
printf("Character sent\n");
}
else {
usleep(100000);
outb(0x0,DATA);
printf("Character fail to send\n");
}
}
char rx_nibble()
{
char status;
/* Select 5 LSB */
status = inb(STATUS) >> 3;
status = status & 0x0f;
return status;
}