lukelukeluke
Erfahrenes Mitglied
Hallo,
Versuche seit Stunden von ner seriellen Schnittstelle zu lesen, auf Mac OS X (also Verhalten wie Linux). Habe div Programmschnipsel auf Internet gefunden und getestet (z.B. Siehe unten). Das Problem ist immer das gleiche: Mein serielles Gerät sendet was, das kriege ich auch angezeigt, aber auch wenn ich nichts mehr sende Empfange ich immer wieder die gleichen Daten, endlos sozusagen. Liegt das an irgendwelchen Speichern die ich nicht lösche, oder kann es sein das mein Serial Adapter, resp. dessen Treiber einfach nicht zu gebrauchen ist?
Hier ist mein Code:
Versuche seit Stunden von ner seriellen Schnittstelle zu lesen, auf Mac OS X (also Verhalten wie Linux). Habe div Programmschnipsel auf Internet gefunden und getestet (z.B. Siehe unten). Das Problem ist immer das gleiche: Mein serielles Gerät sendet was, das kriege ich auch angezeigt, aber auch wenn ich nichts mehr sende Empfange ich immer wieder die gleichen Daten, endlos sozusagen. Liegt das an irgendwelchen Speichern die ich nicht lösche, oder kann es sein das mein Serial Adapter, resp. dessen Treiber einfach nicht zu gebrauchen ist?
Hier ist mein Code:
Code:
//http://www.openframeworks.cc/setup/xcode
//http://openmtproject.sourceforge.net/doc/mtview/classof_serial.html#ab0b70837b7a0ffc7f71d4ae381857c41
//http://en.wikibooks.org/wiki/Serial_Programming/termios
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h> // For FIONREAD
#include <stdlib.h>
// Standard:
#include <iostream>
// openFramework:
//#include "ofMain.h"
//#include "ofAppGlutWindow.h"
// Eigene includes:
//#include "BufferManager.h"
using namespace std;
struct termios tio;
struct termios stdio;
int tty_fd; // Serial handle
fd_set rdset;
int res;
int bytes_avaiable;
int readamount;
char buf[1025]; // Buffer
BufferManager mybuf;
void setup_serial() {
//printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
//fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking (funktioniert nicht wenn auskommentiert)
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open("/dev/tty.PL2303-0000201A", O_RDWR | O_NONBLOCK);
cfsetospeed(&tio,B115200); // 115200 baud
cfsetispeed(&tio,B115200); // 115200 baud
//tcflush(tty_fd, TCIFLUSH);
tcsetattr(tty_fd,TCSANOW,&tio);
}
int main(int argc,char** argv) {
//unsigned char c='D';
setup_serial();
for(int i=0;i<99999999;i++) {
ioctl(tty_fd, FIONREAD, &bytes_avaiable);
if(bytes_avaiable>1024) { readamount=1024; }
else { readamount=bytes_avaiable; }
if(readamount>0) {
res = read(tty_fd,buf,readamount-1);
buf[readamount-1] = '\0';
cout << buf;
}
//sleep (1);
}
close(tty_fd);
}