Search code examples
clinuxbaud-rate

C code for non-standard baud rate on Debian/Raspberry Pi


I'm working with a hardware device which only operates with the non-standard baud rate of 625000.

I need to connect to and read and write data from this device via a USB port. Hence I've been trying to develop a little C program which will allow me to do this. However, this code needs to work in a Linux environment (Debian/Raspian) and unfortunately my Linux skills are only rudimentary.

Therefore, I was hoping some one could explain to me in the simplest terms (code examples would be great!) how I set up a non-standard baud rate of 625000 on Linux, connect to my hardware device (ttyUSB0), and write a bit stream to the device (0x02 0x01) and read from it the 7 bytes it returns.

I've had a look at the following Stack Overflow questions:

And others...

However, the holes in my Linux knowledge are just too great for me to make the necessary connections. How can I do it?


Solution

  • So after a bit more searching I stumbled upon the following code at:

    https://jim.sh/ftx/files/linux-custom-baudrate.c

    Below, is a copy of the above code which I've dumbed down quite a bit for my purposes however, it should be quite simple to implement now.

    #include<string.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<errno.h>
    #include<unistd.h>
    #include<fcntl.h>
    #include<termio.h>
    #include <linux/serial.h>
    
    static int rate_to_constant(int baudrate) {
    #define B(x) case x: return B##x
            switch(baudrate) {
            B(50);     B(75);     B(110);    B(134);    B(150);
            B(200);    B(300);    B(600);    B(1200);   B(1800);
            B(2400);   B(4800);   B(9600);   B(19200);  B(38400);
            B(57600);  B(115200); B(230400); B(460800); B(500000);
            B(576000); B(921600); B(1000000);B(1152000);B(1500000);
        default: return 0;
        }
    #undef B
    }
    
    int main() {
    
        struct termios options;
        struct serial_struct serinfo;
        int fd;
        int speed = 0;
        int rate = 625000;
    
        /* Open and configure serial port */
        if ((fd = open("/dev/ttyUSB0",O_RDWR|O_NOCTTY)) == -1)
        {
            return -1;
        }
    
        // if you've entered a standard baud the function below will return it
        speed = rate_to_constant(rate);
    
        if (speed == 0) {
            /* Custom divisor */
            serinfo.reserved_char[0] = 0;
            if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
                return -1;
            serinfo.flags &= ~ASYNC_SPD_MASK;
            serinfo.flags |= ASYNC_SPD_CUST;
            serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;
            if (serinfo.custom_divisor < 1)
                serinfo.custom_divisor = 1;
            if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
                return -1;
            if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
                return -1;
            if (serinfo.custom_divisor * rate != serinfo.baud_base) {
                warnx("actual baudrate is %d / %d = %f",
                      serinfo.baud_base, serinfo.custom_divisor,
                      (float)serinfo.baud_base / serinfo.custom_divisor);
            }
        }
    
        fcntl(fd, F_SETFL, 0);
        tcgetattr(fd, &options);
        cfsetispeed(&options, speed ?: B38400);
        cfsetospeed(&options, speed ?: B38400);
        cfmakeraw(&options);
        options.c_cflag |= (CLOCAL | CREAD);
        options.c_cflag &= ~CRTSCTS;
        if (tcsetattr(fd, TCSANOW, &options) != 0)
        {
            //return -1;
        }
    
    
        //return fd;
    
        char ping_cmd[] = {2,1};
        char ping_rec[7];
    
        write(fd,&ping_cmd,sizeof(ping_cmd));
        read(fd,&ping_rec,sizeof(ping_rec));
    
        int i;
        for (i = 0; i < sizeof(ping_rec); i++)
        {
            printf("%d ",ping_rec[i]);
        }
    
    
        close(fd);
        return 0;
    }
    

    As the more astute coders out there will notice, since I pulled this code into my main, the presence of all those "return -1" is almost certainly bad programming practice, however, I'm not sure how I should clean it up and hence I'd love hear your suggestions - I will make edits as suggested.

    In the meantime though, should you face a similar problem to me, the above should do nicely.