Große Umstellung. Viel hinzugefügt.
[h316.git] / pc-tools / src-filters / load.c
1 #include <stdio.h>
2 #include <termios.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <signal.h>
7 #include <stdlib.h>
8
9 struct termios orgstate;
10 struct termios workstate;
11
12 #define PORT_FILE "/dev/ttyS0"
13 #define BUFFER_SIZE 1000000
14
15 char * buffer;
16 int sp, ep, count, portfd;
17
18 void sighandler(int sig){
19 sp=0;
20 ep=count-1;
21 close (portfd);
22 // fprintf(stderr,"%i %i\n", sp, ep);
23 if (count>0) {
24 while(buffer[sp]==0) sp++;
25 while(buffer[ep]==0) ep--;
26 }
27
28 write(1,buffer+sp,ep-sp+1);
29 tcdrain(2);
30 fprintf(stderr,"Eingelesen: %i\n",ep-sp+1);
31 tcsetattr(portfd, TCSANOW, &orgstate);
32 close(portfd);
33 exit(0);
34 }
35
36 int main (int argc, char ** argv){
37
38 portfd=open(PORT_FILE, O_RDWR);
39 tcgetattr(portfd, &orgstate);
40 tcgetattr(portfd, &workstate);
41 cfmakeraw(&workstate);
42 // workstate.c_oflag=0;
43 workstate.c_cflag|=CS8+CREAD+CLOCAL+CRTSCTS+CSTOPB ;
44 // workstate.c_lflag=0;
45 cfsetspeed(&workstate, B4800);
46 tcsetattr(portfd, TCSANOW, &workstate);
47 count=0;
48
49 signal(SIGINT, sighandler);
50 buffer=(char *) malloc(BUFFER_SIZE);
51 fprintf(stderr, "Warte auf Daten. Einspielen und dann CTRL-C drücken.\n");
52
53 while(1) {
54 int erg=read(portfd, buffer+count, 1);
55 if (erg>0) count+=erg;
56 }
57
58
59 } //main();
60 // Ende.