-1

I am having trouble selecting correct settings for the serial port to be opened. Information I have is the following:

  • Synchronization: Asynchronous method
  • Communication method: Full duplex transmission
  • Communication speed: 9600 bps (bits per second)
  • Transmission code: 8-bit data
  • Data configuration: Start bit 1, data 8-bit + parity 1, stop bit 1
  • Error control: Horizontal (CRC) and vertical (even number) parities
  • 1 byte configuration PC should not use a control signal (DTR, DSR, RTS and CTS) at the time of this connection.

What I have is something like:

bool configurePort(void)   {
    struct termios port_settings;
    bzero(&port_settings, sizeof(port_settings));

    tcgetattr(fd, &port_settings);

    cfsetispeed(&port_settings, B9600);
    cfsetospeed(&port_settings, B9600);

    port_settings.c_cflag &= ~CSIZE;
    port_settings.c_cflag |= CS8;

    // parity bit
    //port_settings.c_cflag &= ~PARENB;
    //port_settings.c_cflag &= ~PARODD;
    // hardware flow
    port_settings.c_cflag &= ~CRTSCTS;
    // stop bit
    //port_settings.c_cflag &= ~CSTOPB;

    port_settings.c_iflag = IGNBRK;
    port_settings.c_iflag &= ~(IXON | IXOFF | IXANY);
    port_settings.c_lflag = 0;
    port_settings.c_oflag = 0;

    port_settings.c_cc[VMIN] = 1;   
    port_settings.c_cc[VTIME] = 0;
    port_settings.c_cc[VEOF] = 4;   

    tcsetattr(fd, TCSANOW, &port_settings);


    return true;
}

Tried various modifications but nothing seems to work.

The device is connected over USB-serial (ttyUSB0) and I have permissions. It opens device, sends (?) data but never gets anything back...

Can someone point me what should be done?

kometonja
  • 437
  • 4
  • 15
  • 1
    Your comment is not constructive and unrelated to a question. Moreover, this is more C than C++ – kometonja Feb 18 '16 at 18:21
  • You need to test the return codes from **tcgetattr()** and **tcsetattr()**, especially since you're encountering problems. Maybe it's not done in examples, but checking return codes is the proper means for writing robust code. You haven't specified whether this is canonical I/O or not. Defining VEOF in raw mode is illogical. You haven't set parity. Study [Setting Terminal Modes Properly](http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_12.html#SEC237) and [Serial Programming Guide for POSIX Operating Systems](http://www.cmrr.umn.edu/~strupp/serial.html) – sawdust Feb 18 '16 at 19:31

1 Answers1

1

Try with this:

bool configurePort(void)   {
    struct termios port_settings;
    bzero(&port_settings, sizeof(port_settings));

    if(tcgetattr(fd, &port_settings) < 0) {
        perror("tcgetattr");
        return false;
    }

    cfmakeraw(&port_settings);

    cfsetispeed(&port_settings, B9600);
    cfsetospeed(&port_settings, B9600);

    //input
    port_settings.c_iflag &= ~(IXON | IXOFF | IXANY); //disable flow control

    //local
    port_settings.c_lflag = 0;  // No local flags

    //output
    port_settings.c_oflag |= ONLRET;
    port_settings.c_oflag |= ONOCR;
    port_settings.c_oflag &= ~OPOST;


    port_settings.c_cflag &= ~CRTSCTS; // Disable RTS/CTS    
    port_settings.c_cflag |= CREAD; // Enable receiver
    port_settings.c_cflag &= ~CSTOPB;


    tcflush(fd, TCIFLUSH);


    if(tcsetattr(fd, TCSANOW, &port_settings) < 0) {
        perror("tcsetattr");
        return false;
    }

    int iflags = TIOCM_DTR;
    ioctl(fd, TIOCMBIC, &iflags); // turn off DTR

    return true;
} //configure port