Electronic – BeagleBone Serial Communication

beaglebone blackcuart

I have a problem seeing characters written in /dev/ttyS8 on my PC terminal with this C code:

void main (void)
{
    int file, i;
    unsigned char receive[100]; // declare a buffer for receiving data
    char buf[20];
    size_t nbytes;
    ssize_t bytes_written;

    if ((file = open("/dev/ttyS8", O_RDWR))<0)
    {
        printf("UART: Failed to open the file.\n");
        return;
    }

    //
    struct termios options; // the termios structure is vital
    tcgetattr(file, &options); // sets the parameters associated with file

    // Set up the communications options:
    // 9600 baud, 8-bit, enable receiver, no modem control lines
    options.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
    options.c_iflag = IGNPAR | ICRNL; // ignore partity errors, CR -> newline
    tcflush(file, TCIFLUSH); // discard file information not transmitted
    tcsetattr(file, TCSANOW, &options); // changes occur immmediately

    strcpy(buf, "This is a test\n");
    nbytes = strlen(buf);

    while (1)
    {
        bytes_written = write(file, buf, nbytes);
        sleep(10);
    }
    close(file);
}

I tested if maybe type of serial cable is a problem, but it behaves the same.
/dev/ttyS8 is my UART output, but I cant determine what I'm doing wrong. Any idea what to test will be helpful, or on what to pay attention when trying to do something like this.

Thanks for your help!

Best Answer

I found the solution it was in too big difference between baudrates of receiver and transmitter.

I would add that it is very recommended that when you open any file in Linux, you need to close it or you'll have a problems.