4

I want to communicate with my PC using the RS232 port. I can open "/dev/ttyS0" and write data using write() function, But i can't read correct data from "dev/ttyS0" by using read(). read() function read the unnecessary data.Please tell me how to solve this problem?

My program code here :

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

int main()
{
    int n = 0, fd = 0, bytes = 0;

    char buffer[10];

    struct termios term;

    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

    if (fd == -1)
    {
        perror("open");
        return;
    }
    else
    {
        fcntl(fd, F_SETFL, 0);
        perror("Port");
    }

    tcgetattr(fd, &term);

    cfsetispeed(&term, B115200);
    cfsetospeed(&term, B115200);

    term.c_cflag |= (CLOCAL | CREAD);
    term.c_cflag &= ~PARENB;
    term.c_cflag &= ~CSTOPB;
    term.c_cflag &= ~CSIZE;
    term.c_cflag |= CS8;
    term.c_cflag &= ~CRTSCTS;
    term.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    term.c_iflag &= ~(IXON | IXOFF | IXANY);
    term.c_oflag &= ~OPOST; 

    term.c_cc[VMIN] = 0;
    term.c_cc[VTIME] = 10;


    tcsetattr(fd, TCSANOW, &term); 

    printf("Enter the string...\n");
        scanf("%s", buffer);

        write(fd, buffer, sizeof(buffer));
        perror("write");

//  fcntl(fd, F_SETFL, FNDELAY);    

    sleep(1);

    bytes = read(fd, buffer, sizeof(buffer));
    perror("read");

    buffer[bytes] = '\0';
    printf("Bytes : %d\n", bytes);
    printf("%s\n", buffer);
    memset(buffer, '\0', 10);   
}
7
  • Please give an example of what data you are reading and what you were expecting Commented Jan 28, 2016 at 11:01
  • 1
    Why are you using nonblocking IO? Do you know what the effect of this is? Commented Jan 28, 2016 at 11:01
  • for example "Hello world" write and read garbage data Commented Jan 28, 2016 at 11:04
  • @FUZxxl: Good point. Perhaps fd isn't ready when tcgetattr is called (so maybe it just returns -EAGAIN. The code doesn't check errors on anything, so I'd suggest using strace to see what system calls are made, and what their return values are. Also, use a known-good terminal emulator like minicom to make sure things work properly. Commented Jan 29, 2016 at 17:06
  • 1
    @PeterCordes Library functions never clear errno. That's a POSIX rule. Commented Jan 29, 2016 at 18:34

1 Answer 1

2

As I already answered HERE the following code works well. Did you try to change the serial line? Are you sure you are shortcutting pin 2 and 3 of serial DB9 connector?

int main()
{
    int n = 0, fd = 0, bytes = 0;
    char ch = 0;

    char buffer[128], *bufPtr;
    int nBytes = 0, tries = 0, x = 0;

    struct termios term;

    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

    if (fd == -1)
    {
        perror("open");
        return;
    }
    else
    {
        fcntl(fd, F_SETFL, 0);
        perror("Port");
    }

    if (n = tcgetattr(fd, &term) == -1)
    {
        perror("tcgetattr");
        return;
    }

    if (n = cfsetispeed(&term, B115200) == -1)
    {
        perror("cfsetispeed");
        return;
    }

    if (n = cfsetospeed(&term, B115200) == -1)
    {
        perror("cfsetospeed");
        return;
    }

    term.c_cflag |= (CLOCAL | CREAD);
    term.c_cflag &= ~PARENB;
    term.c_cflag &= ~CSTOPB;
    term.c_cflag &= ~CSIZE;
    term.c_cflag |= CS8;
    term.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    term.c_iflag &= ~(IXON | IXOFF | IXANY);
    term.c_cflag &= ~CRTSCTS;
    term.c_oflag &= ~OPOST;

    if (n = tcsetattr(fd, TCSANOW, &term) == -1)
    {
        perror("tcsetattr");
        return;
    }

    char stringToSend[128];

    printf("Enter the string...\n");
    scanf("%s", stringToSend);

    size_t len = strlen(stringToSend) +1 ;

    write(fd,stringToSend, len);
    perror("write");

    size_t receivedBytes = 0;
    bytes = 0;
    memset(buffer, 0x00, sizeof(buffer));
    while (receivedBytes<len)
    {
       bytes = read(fd, &buffer[receivedBytes], sizeof(buffer)-1);
       perror("read");

       if (bytes > 0)
           receivedBytes += bytes;
    }

    printf("Bytes : %d and data: %s\n", receivedBytes, buffer);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.