serial: More error-checking & logging, add baudrates.
This mostly affects the non-Windows code so far, the rest will follow.
This commit is contained in:
parent
d7c776b9ff
commit
b19f4622b6
|
@ -2,6 +2,7 @@
|
||||||
* This file is part of the sigrok project.
|
* This file is part of the sigrok project.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
|
* Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
|
||||||
|
* Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -29,10 +30,20 @@
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#endif
|
#endif
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include "libsigrok.h"
|
#include "libsigrok.h"
|
||||||
#include "libsigrok-internal.h"
|
#include "libsigrok-internal.h"
|
||||||
|
|
||||||
|
/* Message logging helpers with driver-specific prefix string. */
|
||||||
|
#define DRIVER_LOG_DOMAIN "serial: "
|
||||||
|
#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
|
||||||
|
#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
|
||||||
|
#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
|
||||||
|
#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
|
||||||
|
#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
|
||||||
|
#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
|
||||||
|
|
||||||
// FIXME: Must be moved, or rather passed as function argument.
|
// FIXME: Must be moved, or rather passed as function argument.
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static HANDLE hdl;
|
static HANDLE hdl;
|
||||||
|
@ -54,6 +65,8 @@ SR_PRIV GSList *list_serial_ports(void)
|
||||||
{
|
{
|
||||||
GSList *ports;
|
GSList *ports;
|
||||||
|
|
||||||
|
sr_dbg("Getting list of serial ports on the system.");
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* TODO */
|
/* TODO */
|
||||||
ports = NULL;
|
ports = NULL;
|
||||||
|
@ -66,8 +79,10 @@ SR_PRIV GSList *list_serial_ports(void)
|
||||||
for (i = 0; serial_port_glob[i]; i++) {
|
for (i = 0; serial_port_glob[i]; i++) {
|
||||||
if (glob(serial_port_glob[i], 0, NULL, &g))
|
if (glob(serial_port_glob[i], 0, NULL, &g))
|
||||||
continue;
|
continue;
|
||||||
for (j = 0; j < g.gl_pathc; j++)
|
for (j = 0; j < g.gl_pathc; j++) {
|
||||||
ports = g_slist_append(ports, g_strdup(g.gl_pathv[j]));
|
ports = g_slist_append(ports, g_strdup(g.gl_pathv[j]));
|
||||||
|
sr_dbg("Found serial port '%s'.", g.gl_pathv[j]);
|
||||||
|
}
|
||||||
globfree(&g);
|
globfree(&g);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -75,57 +90,113 @@ SR_PRIV GSList *list_serial_ports(void)
|
||||||
return ports;
|
return ports;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open the specified serial port.
|
||||||
|
*
|
||||||
|
* @param pathname OS-specific serial port specification. Examples:
|
||||||
|
* "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
|
||||||
|
* @param flags Flags to use when opening the serial port.
|
||||||
|
*
|
||||||
|
* @return 0 upon success, -1 upon failure.
|
||||||
|
*/
|
||||||
SR_PRIV int serial_open(const char *pathname, int flags)
|
SR_PRIV int serial_open(const char *pathname, int flags)
|
||||||
{
|
{
|
||||||
|
/* TODO: Abstract 'flags', currently they're OS-specific! */
|
||||||
|
|
||||||
|
sr_dbg("Opening serial port '%s' (flags = %d).", pathname, flags);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* FIXME: Don't hardcode COM1. */
|
pathname = "COM1"; /* FIXME: Don't hardcode COM1. */
|
||||||
hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
|
|
||||||
|
hdl = CreateFile(pathname, GENERIC_READ | GENERIC_WRITE, 0, 0,
|
||||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
||||||
if (hdl == INVALID_HANDLE_VALUE) {
|
if (hdl == INVALID_HANDLE_VALUE) {
|
||||||
/* TODO: Error handling. */
|
sr_err("Error opening serial port '%s'.", pathname);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
return open(pathname, flags);
|
int fd;
|
||||||
|
|
||||||
|
if ((fd = open(pathname, flags)) < 0) {
|
||||||
|
/*
|
||||||
|
* Should be sr_err(), but since some drivers try to open all
|
||||||
|
* ports on a system and see if they succeed, this would
|
||||||
|
* yield ugly output for e.g. "sigrok-cli -D".
|
||||||
|
*/
|
||||||
|
sr_dbg("Error opening serial port '%s': %s.", pathname,
|
||||||
|
strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
return fd;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Close the serial port.
|
* Close the specified serial port.
|
||||||
* Returns 0 upon success, -1 upon failure.
|
*
|
||||||
|
* @param fd File descriptor of the serial port.
|
||||||
|
*
|
||||||
|
* @return 0 upon success, -1 upon failure.
|
||||||
*/
|
*/
|
||||||
SR_PRIV int serial_close(int fd)
|
SR_PRIV int serial_close(int fd)
|
||||||
{
|
{
|
||||||
|
sr_dbg("FD %d: Closing serial port.", fd);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* Returns non-zero upon success, 0 upon failure. */
|
/* Returns non-zero upon success, 0 upon failure. */
|
||||||
return (CloseHandle(hdl) == 0) ? -1 : 0;
|
return (CloseHandle(hdl) == 0) ? -1 : 0;
|
||||||
#else
|
#else
|
||||||
|
int ret;
|
||||||
|
|
||||||
/* Returns 0 upon success, -1 upon failure. */
|
/* Returns 0 upon success, -1 upon failure. */
|
||||||
return close(fd);
|
if ((ret = close(fd)) < 0) {
|
||||||
|
sr_dbg("FD %d: Error closing serial port: %s.",
|
||||||
|
fd, strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Flush serial port buffers (if any).
|
* Flush serial port buffers (if any).
|
||||||
* Returns 0 upon success, -1 upon failure.
|
*
|
||||||
|
* @param fd File descriptor of the serial port.
|
||||||
|
*
|
||||||
|
* @return 0 upon success, -1 upon failure.
|
||||||
*/
|
*/
|
||||||
SR_PRIV int serial_flush(int fd)
|
SR_PRIV int serial_flush(int fd)
|
||||||
{
|
{
|
||||||
|
sr_dbg("FD %d: Flushing serial port.", fd);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* Returns non-zero upon success, 0 upon failure. */
|
/* Returns non-zero upon success, 0 upon failure. */
|
||||||
return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
|
return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
|
||||||
#else
|
#else
|
||||||
|
int ret;
|
||||||
|
|
||||||
/* Returns 0 upon success, -1 upon failure. */
|
/* Returns 0 upon success, -1 upon failure. */
|
||||||
return tcflush(fd, TCIOFLUSH);
|
if ((ret = tcflush(fd, TCIOFLUSH)) < 0)
|
||||||
|
sr_err("Error flushing serial port: %s.", strerror(errno));
|
||||||
|
|
||||||
|
return ret;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Write a number of bytes to the specified serial port.
|
* Write a number of bytes to the specified serial port.
|
||||||
* Returns the number of bytes written, or -1 upon failure.
|
*
|
||||||
|
* @param fd File descriptor of the serial port.
|
||||||
|
* @param buf Buffer containing the bytes to write.
|
||||||
|
* @param count Number of bytes to write.
|
||||||
|
*
|
||||||
|
* @return The number of bytes written, or -1 upon failure.
|
||||||
*/
|
*/
|
||||||
SR_PRIV int serial_write(int fd, const void *buf, size_t count)
|
SR_PRIV int serial_write(int fd, const void *buf, size_t count)
|
||||||
{
|
{
|
||||||
|
sr_spew("FD %d: Writing %d bytes.", fd, count);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
DWORD tmp = 0;
|
DWORD tmp = 0;
|
||||||
|
|
||||||
|
@ -133,17 +204,32 @@ SR_PRIV int serial_write(int fd, const void *buf, size_t count)
|
||||||
/* Returns non-zero upon success, 0 upon failure. */
|
/* Returns non-zero upon success, 0 upon failure. */
|
||||||
WriteFile(hdl, buf, count, &tmp, NULL);
|
WriteFile(hdl, buf, count, &tmp, NULL);
|
||||||
#else
|
#else
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
/* Returns the number of bytes written, or -1 upon failure. */
|
/* Returns the number of bytes written, or -1 upon failure. */
|
||||||
return write(fd, buf, count);
|
ret = write(fd, buf, count);
|
||||||
|
if (ret < 0)
|
||||||
|
sr_err("FD %d: Write error: %s.", fd, strerror(errno));
|
||||||
|
else if ((size_t)ret != count)
|
||||||
|
sr_spew("FD %d: Only wrote %d/%d bytes.", fd, ret, count);
|
||||||
|
|
||||||
|
return ret;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Read a number of bytes from the specified serial port.
|
* Read a number of bytes from the specified serial port.
|
||||||
* Returns the number of bytes read, or -1 upon failure.
|
*
|
||||||
|
* @param fd File descriptor of the serial port.
|
||||||
|
* @param buf Buffer where to store the bytes that are read.
|
||||||
|
* @param count The number of bytes to read.
|
||||||
|
*
|
||||||
|
* @return The number of bytes read, or -1 upon failure.
|
||||||
*/
|
*/
|
||||||
SR_PRIV int serial_read(int fd, void *buf, size_t count)
|
SR_PRIV int serial_read(int fd, void *buf, size_t count)
|
||||||
{
|
{
|
||||||
|
sr_spew("FD %d: Reading %d bytes.", fd, count);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
DWORD tmp = 0;
|
DWORD tmp = 0;
|
||||||
|
|
||||||
|
@ -151,48 +237,104 @@ SR_PRIV int serial_read(int fd, void *buf, size_t count)
|
||||||
/* Returns non-zero upon success, 0 upon failure. */
|
/* Returns non-zero upon success, 0 upon failure. */
|
||||||
return ReadFile(hdl, buf, count, &tmp, NULL);
|
return ReadFile(hdl, buf, count, &tmp, NULL);
|
||||||
#else
|
#else
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
/* Returns the number of bytes read, or -1 upon failure. */
|
/* Returns the number of bytes read, or -1 upon failure. */
|
||||||
return read(fd, buf, count);
|
ret = read(fd, buf, count);
|
||||||
|
if (ret < 0) {
|
||||||
|
/*
|
||||||
|
* Should be sr_err(), but that would yield lots of
|
||||||
|
* "Resource temporarily unavailable" messages.
|
||||||
|
*/
|
||||||
|
sr_spew("FD %d: Read error: %s.", fd, strerror(errno));
|
||||||
|
} else if ((size_t)ret != count) {
|
||||||
|
sr_spew("FD %d: Only read %d/%d bytes.", fd, ret, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a backup of the current parameters of the specified serial port.
|
||||||
|
*
|
||||||
|
* @param fd File descriptor of the serial port.
|
||||||
|
*
|
||||||
|
* @return Pointer to a struct termios upon success, NULL upon errors.
|
||||||
|
* It is the caller's responsibility to g_free() the pointer if no
|
||||||
|
* longer needed.
|
||||||
|
*/
|
||||||
SR_PRIV void *serial_backup_params(int fd)
|
SR_PRIV void *serial_backup_params(int fd)
|
||||||
{
|
{
|
||||||
|
sr_dbg("FD %d: Creating serial parameters backup.", fd);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* TODO */
|
/* TODO */
|
||||||
#else
|
#else
|
||||||
struct termios *term;
|
struct termios *term;
|
||||||
|
|
||||||
/* TODO: 'term' is never g_free()'d? */
|
|
||||||
if (!(term = g_try_malloc(sizeof(struct termios)))) {
|
if (!(term = g_try_malloc(sizeof(struct termios)))) {
|
||||||
sr_err("serial: %s: term malloc failed", __func__);
|
sr_err("termios struct malloc failed.");
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
tcgetattr(fd, term);
|
/* Returns 0 upon success, -1 upon failure. */
|
||||||
|
if (tcgetattr(fd, term) < 0) {
|
||||||
|
sr_err("FD %d: Error getting serial parameters: %s.",
|
||||||
|
strerror(errno));
|
||||||
|
g_free(term);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return term;
|
return term;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
SR_PRIV void serial_restore_params(int fd, void *backup)
|
/**
|
||||||
|
* Restore serial port settings from a previously created backup.
|
||||||
|
*
|
||||||
|
* @param fd File descriptor of the serial port.
|
||||||
|
* @param backup Pointer to a struct termios which contains the settings
|
||||||
|
* to restore.
|
||||||
|
*
|
||||||
|
* @return 0 upon success, -1 upon failure.
|
||||||
|
*/
|
||||||
|
SR_PRIV int serial_restore_params(int fd, void *backup)
|
||||||
{
|
{
|
||||||
|
sr_dbg("FD %d: Restoring serial parameters from backup.", fd);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* TODO */
|
/* TODO */
|
||||||
#else
|
#else
|
||||||
tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
|
int ret;
|
||||||
|
|
||||||
|
/* Returns 0 upon success, -1 upon failure. */
|
||||||
|
if ((ret = tcsetattr(fd, TCSADRAIN, (struct termios *)backup)) < 0) {
|
||||||
|
sr_err("FD %d: Error restoring serial parameters: %s.",
|
||||||
|
strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Set serial parameters.
|
* Set serial parameters for the specified serial port.
|
||||||
*
|
*
|
||||||
* flowcontrol: 1 = rts/cts, 2 = xon/xoff
|
* @param baudrate The baudrate to set.
|
||||||
* parity: 0 = none, 1 = even, 2 = odd
|
* @param bits The number of data bits to use.
|
||||||
|
* @param parity The parity setting to use (0 = none, 1 = even, 2 = odd).
|
||||||
|
* @param stopbits The number of stop bits to use (1 or 2).
|
||||||
|
* @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS,
|
||||||
|
* 2 = XON/XOFF).
|
||||||
|
*
|
||||||
|
* @return SR_OK upon success, SR_ERR upon failure.
|
||||||
*/
|
*/
|
||||||
SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
int stopbits, int flowcontrol)
|
int stopbits, int flowcontrol)
|
||||||
{
|
{
|
||||||
|
sr_dbg("FD %d: Setting serial parameters.", fd);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
DCB dcb;
|
DCB dcb;
|
||||||
|
|
||||||
|
@ -225,8 +367,8 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
dcb.BaudRate = CBR_2400;
|
dcb.BaudRate = CBR_2400;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
/* TODO: Error handling. */
|
sr_err("Unsupported baudrate: %d.", baudrate);
|
||||||
break;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
dcb.ByteSize = bits;
|
dcb.ByteSize = bits;
|
||||||
dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
|
dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
|
||||||
|
@ -240,10 +382,43 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
struct termios term;
|
struct termios term;
|
||||||
speed_t baud;
|
speed_t baud;
|
||||||
|
|
||||||
if (tcgetattr(fd, &term) < 0)
|
sr_dbg("FD %d: Getting terminal settings.", fd);
|
||||||
|
if (tcgetattr(fd, &term) < 0) {
|
||||||
|
sr_err("tcgetattr() error: %ѕ.", strerror(errno));
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
switch (baudrate) {
|
switch (baudrate) {
|
||||||
|
case 50:
|
||||||
|
baud = B50;
|
||||||
|
break;
|
||||||
|
case 75:
|
||||||
|
baud = B75;
|
||||||
|
break;
|
||||||
|
case 110:
|
||||||
|
baud = B110;
|
||||||
|
break;
|
||||||
|
case 134:
|
||||||
|
baud = B134;
|
||||||
|
break;
|
||||||
|
case 150:
|
||||||
|
baud = B150;
|
||||||
|
break;
|
||||||
|
case 200:
|
||||||
|
baud = B200;
|
||||||
|
break;
|
||||||
|
case 300:
|
||||||
|
baud = B300;
|
||||||
|
break;
|
||||||
|
case 600:
|
||||||
|
baud = B600;
|
||||||
|
break;
|
||||||
|
case 1200:
|
||||||
|
baud = B1200;
|
||||||
|
break;
|
||||||
|
case 1800:
|
||||||
|
baud = B1800;
|
||||||
|
break;
|
||||||
case 2400:
|
case 2400:
|
||||||
baud = B2400;
|
baud = B2400;
|
||||||
break;
|
break;
|
||||||
|
@ -253,6 +428,9 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
case 9600:
|
case 9600:
|
||||||
baud = B9600;
|
baud = B9600;
|
||||||
break;
|
break;
|
||||||
|
case 19200:
|
||||||
|
baud = B19200;
|
||||||
|
break;
|
||||||
case 38400:
|
case 38400:
|
||||||
baud = B38400;
|
baud = B38400;
|
||||||
break;
|
break;
|
||||||
|
@ -262,19 +440,34 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
case 115200:
|
case 115200:
|
||||||
baud = B115200;
|
baud = B115200;
|
||||||
break;
|
break;
|
||||||
|
case 230400:
|
||||||
|
baud = B230400;
|
||||||
|
break;
|
||||||
#ifndef __APPLE__
|
#ifndef __APPLE__
|
||||||
case 460800:
|
case 460800:
|
||||||
baud = B460800;
|
baud = B460800;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
|
sr_err("Unsupported baudrate: %d.", baudrate);
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
if (cfsetospeed(&term, baud) < 0)
|
|
||||||
return SR_ERR;
|
|
||||||
if (cfsetispeed(&term, baud) < 0)
|
|
||||||
return SR_ERR;
|
|
||||||
|
|
||||||
|
sr_dbg("FD %d: Configuring output baudrate to %d (%d).",
|
||||||
|
fd, baudrate, baud);
|
||||||
|
if (cfsetospeed(&term, baud) < 0) {
|
||||||
|
sr_err("cfsetospeed() error: %ѕ.", strerror(errno));
|
||||||
|
return SR_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sr_dbg("FD %d: Configuring input baudrate to %d (%d).",
|
||||||
|
fd, baudrate, baud);
|
||||||
|
if (cfsetispeed(&term, baud) < 0) {
|
||||||
|
sr_err("cfsetispeed() error: %ѕ.", strerror(errno));
|
||||||
|
return SR_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sr_dbg("FD %d: Configuring %d data bits.", fd, bits);
|
||||||
term.c_cflag &= ~CSIZE;
|
term.c_cflag &= ~CSIZE;
|
||||||
switch (bits) {
|
switch (bits) {
|
||||||
case 8:
|
case 8:
|
||||||
|
@ -284,17 +477,21 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
term.c_cflag |= CS7;
|
term.c_cflag |= CS7;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
sr_err("Unsupported data bits number: %d.", bits);
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sr_dbg("FD %d: Configuring %d stop bits.", fd, stopbits);
|
||||||
term.c_cflag &= ~CSTOPB;
|
term.c_cflag &= ~CSTOPB;
|
||||||
switch (stopbits) {
|
switch (stopbits) {
|
||||||
case 1:
|
case 1:
|
||||||
|
/* Do nothing, a cleared CSTOPB entry means "1 stop bit". */
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
term.c_cflag |= CSTOPB;
|
term.c_cflag |= CSTOPB;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
sr_err("Unsupported stopbits number: %d.", stopbits);
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,14 +500,18 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
switch (flowcontrol) {
|
switch (flowcontrol) {
|
||||||
case 0:
|
case 0:
|
||||||
/* No flow control. */
|
/* No flow control. */
|
||||||
|
sr_dbg("FD %d: Configuring no flow control.", fd);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
|
sr_dbg("FD %d: Configuring RTS/CTS flow control.", fd);
|
||||||
term.c_cflag |= CRTSCTS;
|
term.c_cflag |= CRTSCTS;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
|
sr_dbg("FD %d: Configuring XON/XOFF flow control.", fd);
|
||||||
term.c_iflag |= IXON | IXOFF;
|
term.c_iflag |= IXON | IXOFF;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
sr_err("Unsupported flow control setting: %d.", flowcontrol);
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,24 +519,33 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
term.c_cflag &= ~(PARODD | PARENB);
|
term.c_cflag &= ~(PARODD | PARENB);
|
||||||
switch (parity) {
|
switch (parity) {
|
||||||
case SERIAL_PARITY_NONE:
|
case SERIAL_PARITY_NONE:
|
||||||
|
sr_dbg("FD %d: Configuring no parity.", fd);
|
||||||
term.c_iflag |= IGNPAR;
|
term.c_iflag |= IGNPAR;
|
||||||
break;
|
break;
|
||||||
case SERIAL_PARITY_EVEN:
|
case SERIAL_PARITY_EVEN:
|
||||||
|
sr_dbg("FD %d: Configuring even parity.", fd);
|
||||||
term.c_cflag |= PARENB;
|
term.c_cflag |= PARENB;
|
||||||
break;
|
break;
|
||||||
case SERIAL_PARITY_ODD:
|
case SERIAL_PARITY_ODD:
|
||||||
|
sr_dbg("FD %d: Configuring odd parity.", fd);
|
||||||
term.c_cflag |= PARENB | PARODD;
|
term.c_cflag |= PARENB | PARODD;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
sr_err("Unsupported parity setting: %d.", parity);
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Some default parameters */
|
/* Do NOT translate carriage return to newline on input. */
|
||||||
term.c_iflag &= ~(ICRNL);
|
term.c_iflag &= ~(ICRNL);
|
||||||
|
|
||||||
|
/* Disable canonical mode, and don't echo input characters. */
|
||||||
term.c_lflag &= ~(ICANON | ECHO);
|
term.c_lflag &= ~(ICANON | ECHO);
|
||||||
|
|
||||||
if (tcsetattr(fd, TCSADRAIN, &term) < 0)
|
/* Write the configured settings. */
|
||||||
|
if (tcsetattr(fd, TCSADRAIN, &term) < 0) {
|
||||||
|
sr_err("tcsetattr() error: %ѕ.", strerror(errno));
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
|
|
|
@ -131,7 +131,7 @@ SR_PRIV int serial_flush(int fd);
|
||||||
SR_PRIV int serial_write(int fd, const void *buf, size_t count);
|
SR_PRIV int serial_write(int fd, const void *buf, size_t count);
|
||||||
SR_PRIV int serial_read(int fd, void *buf, size_t count);
|
SR_PRIV int serial_read(int fd, void *buf, size_t count);
|
||||||
SR_PRIV void *serial_backup_params(int fd);
|
SR_PRIV void *serial_backup_params(int fd);
|
||||||
SR_PRIV void serial_restore_params(int fd, void *backup);
|
SR_PRIV int serial_restore_params(int fd, void *backup);
|
||||||
SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
||||||
int stopbits, int flowcontrol);
|
int stopbits, int flowcontrol);
|
||||||
SR_PRIV int serial_set_paramstr(int fd, const char *paramstr);
|
SR_PRIV int serial_set_paramstr(int fd, const char *paramstr);
|
||||||
|
|
Loading…
Reference in New Issue