Beispiel #1
0
int open_serial_port(const char *port_name)
{
	int r;

	if (port_fd >= 0) {
		close(port_fd);
	}
	port_fd = open(port_name, O_RDWR);
	if (port_fd < 0) {
		report_open_error(port_name, errno);
		return -1;
	}
	r = set_baud(baud_rate);
	if (r == 0) {
		printf("Port \"%s\" opened at %s baud\r\n",
			port_name, baud_rate);
	} else {
		printf("Port \"%s\" opened, unable to set baud to %s\r\n",
			port_name, baud_rate);
	}
	#ifdef LINUX
	{
		struct serial_struct kernel_serial_settings;
		/* attempt to set low latency mode, but don't worry if we can't */
		r = ioctl(port_fd, TIOCGSERIAL, &kernel_serial_settings);
		if (r < 0) return 0;
		kernel_serial_settings.flags |= ASYNC_LOW_LATENCY;
		ioctl(port_fd, TIOCSSERIAL, &kernel_serial_settings);
	}
	#endif
    return 0;
}
Beispiel #2
0
void open_serial_port(const char *port_name)
{
	int r;
	struct serial_struct kernel_serial_settings;

	if (port_fd >= 0) {
		close(port_fd);
	}
	port_fd = open(port_name, O_RDWR | O_NDELAY | O_NOCTTY);
	if (port_fd < 0) {
		report_open_error(port_name, errno);
		return;
	}
	r = set_baud(baud_setting());
	if (r == 0) {
		printf("Port \"%s\" opened at %s baud\r\n",
			port_name, baud_setting());
		new_port_setting(port_name);
	} else {
		printf("Port \"%s\" opened, unable to set baud to %s\r\n",
			port_name, baud_setting());
	}
	/* attempt to set low latency mode, but don't worry if we can't */
	r = ioctl(port_fd, TIOCGSERIAL, &kernel_serial_settings);
	if (r < 0) return;
	kernel_serial_settings.flags |= ASYNC_LOW_LATENCY;
	ioctl(port_fd, TIOCSSERIAL, &kernel_serial_settings);
}
Beispiel #3
0
int open_serial_port(const char *port_name)
{
    int r;
    struct termios term_setting;

    if (port_fd >= 0) {
        close(port_fd);
    }

    port_fd = open(port_name, O_RDWR);

    if (port_fd < 0) {
        report_open_error(port_name, errno);
        return -1;
    }

    bzero(&term_setting, sizeof(term_setting));
    term_setting.c_cflag = (CS8 | CREAD);
    term_setting.c_cc[VMIN] = 1;
    term_setting.c_cc[VTIME] = 1;
    r = tcsetattr(port_fd, TCSANOW, &term_setting);

    if (r != 0) {
        return -1;
    }

    r = set_baud(baud_rate);

    if (r == 0) {
        printf("Port \"%s\" opened at %s baud\r\n",
               port_name, baud_rate);
    }
    else {
        printf("Port \"%s\" opened, unable to set baud to %s\r\n",
               port_name, baud_rate);
    }

#ifdef LINUX
    {
        struct serial_struct kernel_serial_settings;
        /* attempt to set low latency mode, but don't worry if we can't */
        r = ioctl(port_fd, TIOCGSERIAL, &kernel_serial_settings);

        if (r < 0) {
            return 0;
        }

        kernel_serial_settings.flags |= ASYNC_LOW_LATENCY;
        ioctl(port_fd, TIOCSSERIAL, &kernel_serial_settings);
    }
#endif
    return 0;
}