Set non-common BaudRate, MATLAB 2017 & termios.h

4 views (last 30 days)
Nikolay Konstantinov
Nikolay Konstantinov on 13 Jan 2018
Answered: Walter Roberson on 11 Feb 2018
My os is Ubuntu 16.04 & MATLAB R2017a.
When I'm trying to listen serial port with non-common baud rate, I get error:
s = serial('/dev/ttyUSB0','BaudRate',4000001)
f=open(s);
Open failed: BaudRate could not be set to the specified value.
I found, that there is termios.h file with baud-rate definitions, like that
#define B3500000 0010016
#define B4000000 0010017
And if I set baud rate which is not defined I get error.
So, to listen it via terminal at non-common speed, I'm using this code:
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <asm/termios.h>
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("%s device speed\n\nSet speed for a serial device.\nFor instance:\n %s /dev/ttyUSB0 75000\n", argv[0], argv[0]);
return -1;
}
int fd = open(argv[1], O_RDONLY);
int speed = atoi(argv[2]);
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);
if (r == 0) {
printf("Changed successfully.\n");
} else {
perror("ioctl");
}
}
And after that I'm able to listen the serial port on the non-common serial speed.
Then I come back to the Matlab, and try set the baud-rate which works fine in the terminal. And I'm still getting same error, even more, after creating the serial object, all settings are lost, and it is getting not working in the terminal.
I think Matlab is using some compiled library, which is based on the termios.h, so is it needed to be recompiled.
Am I wrong? Is it possible to solve this issue? I want to set 6Mbit/sec
  1 Comment
Walter Roberson
Walter Roberson on 11 Feb 2018
Small note: you need fopen() rather than open() in that MATLAB code.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 11 Feb 2018
"I think Matlab is using some compiled library, which is based on the termios.h, so is it needed to be recompiled."
No, MATLAB delegates the serial I/O to Java.

Products

Community Treasure Hunt

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

Start Hunting!