How to change baud rate to 14400 on Linux?

1.I used to try to design a flash software to product device on Linux
2.Workers can flash Photon by click one button,Nice
3.After flash several Photon,I find that if I plug in Photon to PC it came into DFU immediately
4.14400 is not a standard uart baud rate,Linux may keep it even device have been removed,Next time you try to open CDC port it will set baud rate to 14400,You can do nothing(It is not easy to change CDC driver for Linux and It is not a issue of driver,baud rate is baud rate not a CMD)
5.I finished the auto flash software on MS-Windows

How to change baud rate to 14400 on Linux?

#include <asm/ioctls.h>
#include <asm/termbits.h>

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>

int Set_Custom_Baud_Rate(int is, int os)
{
        struct termios2 tio;

        char *tty = "/dev/ttyACM1";
        int fd = open(tty , O_NOCTTY);

        if (fd < 0) {
                printf("can not open %s\n", tty);
                return -1;
        }

        if (ioctl(fd, TCGETS2, &tio) < 0) {
                printf("ioctl TCGETS2 %s\n", tty);
                return -2;
        }

        tio.c_cflag &= ~CBAUD;
        tio.c_cflag |= BOTHER;
        tio.c_ispeed = is;
        tio.c_ospeed = os;

        if (ioctl(fd, TCSETS2, &tio) < 0) {
                printf("ioctl TCSETS2 %s\n", tty);
                return -3;
        }

        return 0;
}

int main(void)
{
        Set_Custom_Baud_Rate(14400, 14400);
        //Set_Custom_Baud_Rate(115200, 115200);
}
1 Like

Here is custom-baud, one of the dependecies of po-util that lets you set the baud rate of a device to any speed, including 14400.

After you compile it, it’s used like this:

$ ./custom-baud /dev/ttyACM1 14400
2 Likes

I’ve changed the title of your post to make it more searchable.

1 Like

I want to tell others do not waste time on Linux,After you set one CDC port to 14400,then remove it,plug in another Photon then open CDC port,Photon will immediately into dfu mode,You can not change baud rate before call open(),after you call open() Photon switch to dfu mode and the CDC port gone,You will never get a chance to change the baud rate.

I used to spend almost one week to find out why

I experience this on linux as well, and sadly the only way to stop the baud rate from sticking when opening a serial monitor is to keep reconnecting the device until it gets a new /dev/ttyACMx or reboot the linux machine entirely. Particle will be moving away from magic baud rates in the future.

Update:
I’m no longer experiencing this bug for some reason.

Check the most recent comment here: