2
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ suggest connecting a VOM to the DTR pin of the port and noting if the pin properly changes state when the port is written to, \$\endgroup\$ Commented Aug 30, 2015 at 13:43

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Opening files without closing them will lead to problems on all self respecting operating systems. It consumes memory and it may block processes waiting for data that is consumed by older processes. \$\endgroup\$
    – jippie
    Commented Aug 28, 2015 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.