RS232 - POSIX



  • Hallo,

    ich versuche ueber Linux Signale per RS232 zu empfangen. Es ist wichtig, dass der SystemCall read, nach jedem Zeichen/Byte zurueckkehrt, weil ich ein Slip Protokoll damit realisieren will. Momentan ist es noch so, dass read mit mehreren Bytes zurueckkehrt. Wie kann ich es denn loesen? Weiß nicht mehr weiter, ich brauch hilfe...

    if((fd = open("/dev/ttyUSB0", O_RDWR | O_NDELAY ))  < 0)
     perror("could not open usb device");
    else
     printf("erfolgreiches oeffnen: %i \n", fd);
    
    fcntl(fd, F_SETFL, 0);
    tcgetattr(fd, &options);
    
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    
    // 8N1 - 8Bit no parity
    options.c_cflag &= ~PARENB;
    // no second stop bit
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    //options.c_cflag &= ~CRTSCTS;
    options.c_cflag |= (CS8 | CLOCAL | CREAD);
    //options.c_cflag = B9600;
    //
    options.c_lflag &=  ~(ICANON | ECHO | ECHOE | ISIG);
    
    options.c_iflag &= ~(IXON  | IXOFF | IXANY);
    
    // output options
    
    // options.c_oflag &= ~(ICANON  | ECHO | ECHOE | ISIG);
    options.c_oflag &= ~OPOST;
    
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &options);
    

    Hier mal ein Bsp:
    http://www.escapete.de/rs232.jpg



  • escapete schrieb:

    Es ist wichtig, dass der SystemCall read, nach jedem Zeichen/Byte zurueckkehrt, weil ich ein Slip Protokoll damit realisieren will. Momentan ist es noch so, dass read mit mehreren Bytes zurueckkehrt.

    probiers so:

    ...
    char b;
    if (1 == read (fd, &b, 1))
    {
      // 1 byte abgeholt, steht nun in 'b'
    }
    ...
    


  • An den Einstellungen wird es nicht liegen? Weil wenn ich das nu so mache, dann klappt das auch ganz toll 🙂

    Vielen Dank schon mal!!!


Anmelden zum Antworten