Find the answer to your Linux question:
Results 1 to 2 of 2
Hi,All. I'm trying to write a program that can write to and read from serial port(ttys0). I can open port and write it successfully but can not read from it.The ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    1

    Reading from serial port

    Hi,All.
    I'm trying to write a program that can write to and read from serial port(ttys0).
    I can open port and write it successfully but can not read from it.The number of read bytes always is zero.Below is the source code.Any idea?
    /*******************************************
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <fstream>
    #include <iostream>
    #include <unistd.h>
    using namespace std;
    #define BAUDRATE B38400
    #define MODEMDEVICE "/dev/ttyS1"
    #define _POSIX_SOURCE 1
    #define FALSE 0
    #define TRUE 1

    volatile int STOP=FALSE;

    main()
    {
    int fd,c, res;
    struct termios oldtio,newtio;
    char buf[255];

    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
    if (fd <0) {perror(MODEMDEVICE); exit(-1); }

    tcgetattr(fd,&oldtio); /* save current port settings */

    bzero(&newtio, sizeof(newtio));
    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR;
    newtio.c_oflag = 0;

    /* set input mode (non-canonical, no echo,...) */
    newtio.c_lflag = 0;

    newtio.c_cc[VTIME] = 0;
    newtio.c_cc[VMIN] = 0;

    tcflush(fd, TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);
    int n;

    for (int i=0;i<3;i++)
    {
    char s[10];
    cout<<"\n input string:";
    cin >>s;

    //**************************************
    n = write(fd, s, strlen(s));

    if (n>0)
    printf("writes %d bytes\n",n);
    else
    printf("doese not write\n");

    //**************************************
    char buf[100];
    n=read(fd,buf,n);
    if (n<0)
    printf("doese not read");
    else
    printf("\n%d bytes read",n);
    }
    tcsetattr(fd,TCSANOW,&oldtio);
    }

  2. #2
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    85
    hi,

    try to add some changes which i written in code.

    fcntl(fd,F_SETFL,O_NDELAY);
    newtio.c_cc[VMIN] = 1;




    Quote Originally Posted by minoo View Post
    Hi,All.
    I'm trying to write a program that can write to and read from serial port(ttys0).
    I can open port and write it successfully but can not read from it.The number of read bytes always is zero.Below is the source code.Any idea?
    /*******************************************
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <fstream>
    #include <iostream>
    #include <unistd.h>
    using namespace std;
    #define BAUDRATE B38400
    #define MODEMDEVICE "/dev/ttyS1"
    #define _POSIX_SOURCE 1
    #define FALSE 0
    #define TRUE 1

    volatile int STOP=FALSE;

    main()
    {
    int fd,c, res;
    struct termios oldtio,newtio;
    char buf[255];

    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
    if (fd <0) {perror(MODEMDEVICE); exit(-1); }

    tcgetattr(fd,&oldtio); /* save current port settings */

    bzero(&newtio, sizeof(newtio));
    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR;
    newtio.c_oflag = 0;

    /* set input mode (non-canonical, no echo,...) */
    newtio.c_lflag = 0;

    newtio.c_cc[VTIME] = 0;
    //newtio.c_cc[VMIN] = 0;
    newtio.c_cc[VMIN] = 1;
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);
    int n;

    for (int i=0;i<3;i++)
    {
    char s[10];
    cout<<"\n input string:";
    cin >>s;

    //**************************************
    n = write(fd, s, strlen(s));

    if (n>0)
    printf("writes %d bytes\n",n);
    else
    printf("doese not write\n");

    //**************************************
    char buf[100];
    fcntl(fd,F_SETFL,O_NDELAY);
    usleep(50000);

    n=read(fd,buf,n);
    if (n<0)
    printf("doese not read");
    else
    printf("\n%d bytes read",n);
    }
    tcsetattr(fd,TCSANOW,&oldtio);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...