Weird ioctl behaviour in a linux program called using system()

18 vues (au cours des 30 derniers jours)
Enrico Lumetti
Enrico Lumetti le 1 Déc 2018
Hello, I have a C program that allows setting a custom baud rate on a serial device.
It uses ioctl() to do so under linux, but its behavior is different when I use it under matlab, compared to a standard invocation from my terminal.
set_baud.c
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stropts.h>
#include <asm/termios.h>
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("Usage: %s <serial device>", argv[0]);
return 1;
}
int fd = open(argv[1], O_RDWR);
int speed = 31250;
struct termios2 tio;
ioctl(fd, TCGETS2, &tio);
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= BOTHER;
tio.c_ispeed = speed;
tio.c_ospeed = speed;
int r = ioctl(fd, TCSETS2, &tio);
close(fd);
return r;
}
relevant matlab line
serial_port = '/dev/ttyUSB1';
system('gcc set_baud.c -o set_baud');
system(['./set_baud ' serial_port]);
When calling it from matlab, a baud rate of 38400 (which is a default baud rate) is set, while calling it from my terminal sets the correct one.
I still have to run strace to it, is anybody aware of a reason for this?

Réponses (0)

Catégories

En savoir plus sur Install Products dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by