2

I've got an Arduino board and want to read the data it's spitting off using USB at a custom baud rate. Hacking up some of the code Arduino suggest, I get this C code:

int serialport_init(const char* serialport, int baud)
{
    struct termios toptions;
    int fd;

    printf("init_serialport: opening port %s @ %d bps\n", serialport,baud);

    fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
    serialPortPointer = fd;

    if (fd == -1)
    {
        printf("Unable to open port when initialising hardware'n");
        return -1;
    }

    if (tcgetattr(fd, &toptions) < 0)
    {
        printf("Couldn't get term attributes when initialising hardware\n");
        return -1;
    }
    speed_t brate = baud; // let you override switch below if needed
    switch(baud) {
        case 4800:   brate=B4800;   break;
        case 9600:   brate=B9600;   break;
        case 14400:  brate=B14400;  break;
        case 19200:  brate=B19200;  break;
        case 28800:  brate=B28800;  break;
        case 38400:  brate=B38400;  break;
        case 57600:  brate=B57600;  break;
        case 115200: brate=B115200; break;
    }
    cfsetispeed(&toptions, EXTA);
    cfsetospeed(&toptions, EXTA);

    // 8N1
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;
    // no flow control
    toptions.c_cflag &= ~CRTSCTS;

    toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl

    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    toptions.c_oflag &= ~OPOST; // make raw

    // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
    toptions.c_cc[VMIN]  = 0;
    toptions.c_cc[VTIME] = 20;

    if(tcsetattr(fd, TCSANOW, &toptions) < 0)
    {
        printf("Couldn't set term attributes when initialising hardware\n");
        return -1;
    }

    return fd;
}

The problem is that the termios.h file doesn't support 31250 (MIDI) baud rate... If I try entering 31250 as the baud rate, this function returns -1 and says "Couldn't set term attributes when initialising hardware" (it fails right at the end).

So - how can I write a program, in C, or any other language, that reads the data off at the baud rate I want? Does termios.h support custom baud rates?

I literally just want to read the data on the serial port - nothing else.

Mike
  • 47,263
  • 29
  • 113
  • 177
Lee
  • 21
  • 1
  • 3

2 Answers2

1

This is a library that enables MIDI I/O communications on the Arduino serial ports. You need an Arduino capable of minimum 2 serial ports (like this one). One serial will be used for communication to MIDI devices (31250bps), and the other one to PC (for example 115200bps). If you have only one serial port on your Arduino board then you can also experiment with a software serial library like this.

avra
  • 3,690
  • 19
  • 19
  • 1
    Ah great stuff - I don't need the MIDI one at the moment, but that will definitely become useful in the near future - thanks. I only have 1 serial port on the board (that uses a mini USB connection) - I'm using a Seeeduino board: http://www.skpang.co.uk/catalog/seeeduino-v221-328-board-p-389.html. When I get to look at this properly later, I'll let you know if the software serial library lets me read debug data at the MIDI Baud rate - thanks – Lee Sep 20 '11 at 10:47
0

The termios.h API simply does not have a way to express user-defined baud rates, though depending on your operating system there may be an extension to do so. Can you try setting up the Arduino to use the more standard 38400 instead?

Jeffrey Hantin
  • 35,734
  • 7
  • 75
  • 94
  • Thanks for the answer. Unfortunately I can't - I'm using an Arduino soldered to a MIDI cable, and MIDI will only work at the rate 31250: http://arduino.cc/en/Tutorial/Midi – Lee Sep 20 '11 at 00:58
  • I've heard of aliasing as described here - But I don't really understand it: http://stackoverflow.com/questions/4968529/how-to-set-baud-rate-to-307200-on-linux – Lee Sep 20 '11 at 00:58
  • What exactly are you trying to build? – Jeffrey Hantin Sep 20 '11 at 02:39
  • A midi device - I would like to just read data off the USB for debugging - the Arduino can be plugged into the USB. However, the BAUD rate is set to 31250 on the Arduino - how do I read data off the USB at this speed? – Lee Sep 20 '11 at 08:40
  • Sending debug data on the same port as MIDI will mix the two data streams, unless you're only trying to monitor the MIDI data from the PC. On a board that only has the one port, you can perhaps use [SoftwareSerial](http://www.arduino.cc/en/Reference/SoftwareSerial) to act as a makeshift secondary debug port, though you may need to level-shift the output from TTL (+5v/0v) to RS232 (+12v/-12v) to get your PC to read it. – Jeffrey Hantin Sep 20 '11 at 20:30