reading and writing-serial port.

This is a discussion on reading and writing-serial port. within the Networking/Device Communication forums, part of the General Programming Boards category; hi, i'm trying 1.to write a string to a serial port, 2. read this string from the port again i'm ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    reading and writing-serial port.

    hi,
    i'm trying
    1.to write a string to a serial port,
    2. read this string from the port again

    i'm using Serial Programming Guide for POSIX Operating Systems - Michael R Sweet as a reference.
    this is the code snippet that i have put together:
    Code:
    int file= open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
     if(file == 0)
                {
                      perror("open_port: Unable to open       /dev/ttyS0 - ");
                }
                else
                      fcntl(file, F_SETFL, 0);
    
    while(1)
    {
    printf("enter input data:\n");
    scanf("%s",&input);
    
    chkin=write(file,input,sizeof input);
    if(chkin<0)
    {
    printf("cannot write to port\n");
    }
    fcntl(file, F_SETFL, FNDELAY);
    chkin=read(file,line,sizeof line);
    if(chkin<0)
    {
    printf("cannot read from port\n");
    }
    printf("line=%s\n",line);
    printf("do you want to send the next packet?(y/n):\n");
    scanf("%s",&nextpacket);
    if((strcmp(nextpacket,"y")!=0))
    break;
    
    }
    the program is writing fine to the serial socket, but the read is not happening.
    where am i going wrong?
    a lot of the examples on this i have come across do some configuring at the port. is this essential?
    thank you.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,680
    Yes it is essential that the serial ports are configured. Both ports must be configured to the same parameters, or you will not have reliable communication.


    Jim

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Yes it is essential that the serial ports are configured. Both ports must be configured to the same parameters, or you will not have reliable communication.


    Jim
    thanks for replying jim. but i'm not sure i understand you, what do you mean by "both" the ports? i'm using just one port to write and read.
    write to dev/ttyS0 and then read what i have written.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,680
    Quote Originally Posted by xenanovich View Post
    thanks for replying jim. but i'm not sure i understand you, what do you mean by "both" the ports? i'm using just one port to write and read.
    write to dev/ttyS0 and then read what i have written.
    If you are only using 1 port are you using a loop back cable on the serial port plug(connect pin 2 to pin 3) ? If not you must enable local echo in order to read anything from the port.

    Jim

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    If you are only using 1 port are you using a loop back cable on the serial port plug(connect pin 2 to pin 3) ? If not you must enable local echo in order to read anything from the port.

    Jim
    i configured the port and enabled local echo under minicom. its working now. thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  2. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  3. reading and writing from a COM port
    By abq_guy in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 04:57 PM
  4. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 12:28 AM
  5. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 03:29 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21