+#include <stdio.h>
+#include <termios.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdlib.h>
+
+struct termios orgstate;
+struct termios workstate;
+
+#define PORT_FILE "/dev/ttyS0"
+#define BUFFER_SIZE 1000000
+
+char * buffer;
+int sp, ep, count, portfd;
+
+void sighandler(int sig){
+ sp=0;
+ ep=count-1;
+ close (portfd);
+// fprintf(stderr,"%i %i\n", sp, ep);
+ if (count>0) {
+ while(buffer[sp]==0) sp++;
+ while(buffer[ep]==0) ep--;
+ }
+
+ write(1,buffer+sp,ep-sp+1);
+ tcdrain(2);
+ fprintf(stderr,"Eingelesen: %i\n",ep-sp+1);
+ tcsetattr(portfd, TCSANOW, &orgstate);
+ close(portfd);
+ exit(0);
+}
+
+int main (int argc, char ** argv){
+
+ portfd=open(PORT_FILE, O_RDWR);
+ tcgetattr(portfd, &orgstate);
+ tcgetattr(portfd, &workstate);
+ cfmakeraw(&workstate);
+// workstate.c_oflag=0;
+ workstate.c_cflag|=CS8+CREAD+CLOCAL+CRTSCTS+CSTOPB ;
+// workstate.c_lflag=0;
+ cfsetspeed(&workstate, B4800);
+ tcsetattr(portfd, TCSANOW, &workstate);
+ count=0;
+
+ signal(SIGINT, sighandler);
+ buffer=(char *) malloc(BUFFER_SIZE);
+ fprintf(stderr, "Warte auf Daten. Einspielen und dann CTRL-C drücken.\n");
+
+ while(1) {
+ int erg=read(portfd, buffer+count, 1);
+ if (erg>0) count+=erg;
+ }
+
+
+} //main();
+// Ende.