Exemplo n.º 1
0
int  serial_port_open(struct SerialPort *me, const char *device,
                      void(*term_conf_callback)(struct termios *, speed_t *))
{

    speed_t speed;
    if ((me->fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
        TRACE(TRACE_ERROR, "%s, open failed: %s (%d)\n", device, strerror(errno), errno);
        return -1;
    }
    if (tcgetattr(me->fd, &me->orig_termios) < 0) {
        TRACE(TRACE_ERROR, "%s, get term settings failed: %s (%d)\n", device, strerror(errno), errno);
        close(me->fd);
        return -1;
    }
    me->cur_termios = me->orig_termios;
    term_conf_callback(&me->cur_termios, &speed);
    if (cfsetispeed(&me->cur_termios, speed)) {
        TRACE(TRACE_ERROR, "%s, set term speed failed: %s (%d)\n", device, strerror(errno), errno);
        close(me->fd);
        return -1;
    }
    if (tcsetattr(me->fd, TCSADRAIN, &me->cur_termios)) {
        TRACE(TRACE_ERROR, "%s, set term attr failed: %s (%d)\n", device, strerror(errno), errno);
        close(me->fd);
        return -1;
    }
    serial_port_flush(me);
    return 0;

}
Exemplo n.º 2
0
int  serial_port_open_raw(struct SerialPort* me, const char* device, speed_t speed) {
  if ((me->fd = open(device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
    TRACE(TRACE_ERROR,"%s, open failed: %s (%d)\n", device, strerror(errno), errno);
    return -1;
  }
  if (tcgetattr(me->fd, &me->orig_termios) < 0) {
    TRACE(TRACE_ERROR,"%s, get term settings failed: %s (%d)\n", device, strerror(errno), errno);
    close(me->fd);
    return -1;
  }
  me->cur_termios = me->orig_termios;
  /* input modes  */
  me->cur_termios.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INPCK|ISTRIP|INLCR|IGNCR
                 |ICRNL |IUCLC|IXON|IXANY|IXOFF|IMAXBEL);
  me->cur_termios.c_iflag |= IGNPAR;
  /* control modes*/
  me->cur_termios.c_cflag &= ~(CSIZE|PARENB|CRTSCTS|PARODD|HUPCL|CSTOPB);
  me->cur_termios.c_cflag |= CREAD|CS8|CLOCAL;
  /* local modes  */
  me->cur_termios.c_lflag &= ~(ISIG|ICANON|IEXTEN|ECHO|FLUSHO|PENDIN);
  me->cur_termios.c_lflag |= NOFLSH;
  if (cfsetispeed(&me->cur_termios, speed)) {
    TRACE(TRACE_ERROR,"%s, set term speed failed: %s (%d)\n", device, strerror(errno), errno);
    close(me->fd);
    return -1;
  }
  if (tcsetattr(me->fd, TCSADRAIN, &me->cur_termios)) {
    TRACE(TRACE_ERROR,"%s, set term attr failed: %s (%d)\n", device, strerror(errno), errno);
    close(me->fd);
    return -1;
  }
  serial_port_flush(me);
  return 0;
}