Skip to content

Commit

Permalink
Serial fixes (#935)
Browse files Browse the repository at this point in the history
- Allow changing parity mode after connection is opened
- Fix return value of gp_port_serial_write so that bytes written
  can be --debug-ged
- Fix inverted logic on parity error checking

Co-authored-by: Colin Leroy-Mira <[email protected]>
  • Loading branch information
colinleroy and Colin Leroy-Mira authored Nov 16, 2023
1 parent 14f422d commit 57de6e2
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions libgphoto2_port/serial/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
struct _GPPortPrivateLibrary {
int fd; /* Device handle */
int baudrate; /* Current speed */
int parity; /* Current parity */
};

static int gp_port_serial_check_speed (GPPort *dev);
Expand Down Expand Up @@ -341,8 +342,9 @@ gp_port_serial_init (GPPort *dev)

C_MEM (dev->pl = calloc (1, sizeof (GPPortPrivateLibrary)));

/* There is no default speed. */
/* There is no default speed or parity. */
dev->pl->baudrate = -1;
dev->pl->parity = -1;

return GP_OK;
}
Expand Down Expand Up @@ -489,7 +491,7 @@ gp_port_serial_write (GPPort *dev, const char *bytes, int size)
#else
ioctl (dev->pl->fd, TCDRAIN, 0);
#endif
return GP_OK;
return len;
}


Expand Down Expand Up @@ -552,7 +554,7 @@ gp_port_serial_read (GPPort *dev, char *bytes, int size)
gp_port_set_error (dev, _("Parity error."));
return GP_ERROR_IO_READ;
}
if (!memcmp(bytes,ffchar,1)) {
if (memcmp(bytes,ffchar,1)) {
gp_port_set_error (dev, _("Unexpected parity response sequence 0xff 0x%02x."), ((unsigned char*)bytes)[0]);
return GP_ERROR_IO_READ;
}
Expand Down Expand Up @@ -766,8 +768,9 @@ gp_port_serial_check_speed (GPPort *dev)
if (!dev->pl->fd)
return (GP_OK);

/* If baudrate is up to date, do nothing */
if (dev->pl->baudrate == dev->settings.serial.speed)
/* If baudrate and parity are up to date, do nothing */
if (dev->pl->baudrate == dev->settings.serial.speed
&& dev->pl->parity == dev->settings.serial.parity)
return (GP_OK);

GP_LOG_D ("Setting baudrate to %d...", dev->settings.serial.speed);
Expand Down Expand Up @@ -798,6 +801,7 @@ gp_port_serial_check_speed (GPPort *dev)
tio.c_cc[VTIME] = 0;

if (dev->settings.serial.parity != GP_PORT_SERIAL_PARITY_OFF) {
GP_LOG_D ("Setting parity to %s...", dev->settings.serial.parity == GP_PORT_SERIAL_PARITY_ODD?"odd":"even");
tio.c_iflag &= ~IGNPAR;
tio.c_iflag |= INPCK | PARMRK ;
tio.c_cflag |= PARENB;
Expand Down Expand Up @@ -855,6 +859,7 @@ gp_port_serial_check_speed (GPPort *dev)
#endif

dev->pl->baudrate = dev->settings.serial.speed;
dev->pl->parity = dev->settings.serial.parity;
return GP_OK;
}

Expand Down

0 comments on commit 57de6e2

Please sign in to comment.