dimanche 22 février 2015

Termios configuration


In a MAC OS X (10.10) program, I have a hard time setting up termios correctly for RS-485 serial communication (I use a starcom USB → RS-485 featuring the FTDI chip)


I need to set up the following:




  • 1 Start bit (0 bit)

  • 8 Databits

  • 1 Odd parity bit

  • 1 Stop bit (1 bit)

  • 19200 baud



Communication is binary, so there is no stop character.


Currently, I have found how to set most of the settings (see code below), but I didn't found where to tell termios I want a start bit.



@property(readwrite) int fileDescriptor;

_fileDescriptor = open(bsdPath, O_RDWR | O_NOCTTY | O_NONBLOCK);
ioctl(_fileDescriptor, TIOCEXCL)
fcntl(_fileDescriptor, F_SETFL, 0)
tcgetattr(_fileDescriptor, &gOriginalTTYAttrs)

struct termios options = gOriginalTTYAttrs;
cfmakeraw(&options);
struct termios* rawOptions = &options;

// Timeout 100 ms
rawOptions->c_cc[VTIME] = 1;
rawOptions->c_cc[VMIN] = 0;

// 8 bits
rawOptions->c_cflag |= CS8;

// Parity ODD
rawOptions->c_cflag |= PARENB;
rawOptions->c_cflag |= PARODD;

// Stop bit (I hope it means one stop bit)
rawOptions->c_cflag = rawOptions->c_cflag & ~CSTOPB;

// Flow control none
rawOptions->c_cflag = rawOptions->c_cflag & ~CRTSCTS;
rawOptions->c_cflag = rawOptions->c_cflag & ~(CDTR_IFLOW | CDSR_OFLOW);
rawOptions->c_cflag = rawOptions->c_cflag & ~CCAR_OFLOW;

// Turn on hangup on close (NO IDEA WHAT IT DOES)
rawOptions->c_cflag |= HUPCL;
// Set local mode on (NO IDEA WHAT IT DOES)
rawOptions->c_cflag |= CLOCAL;
// Enable receiver (USEFUL, I GUESS)
rawOptions->c_cflag |= CREAD;
// Turn off canonical mode and signals (NO IDEA WHAT IT DOES)
rawOptions->c_lflag &= ~(ICANON /*| ECHO*/ | ISIG);

// Raw (binary output)
rawOptions->c_oflag &= ~OPOST;

// 19200 baud
cfsetspeed(rawOptions, 19200);

// Applying settings
tcsetattr(_fileDescriptor, TCSANOW, rawOptions);



Aucun commentaire:

Enregistrer un commentaire