need a sample code

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
hai all
This is my first post to this group.I am very much new to embedded
programming .Now i want to write data to com1 and want to read from
com2 .Here i have connected a NULL-MODEM cable in between the two
serial ports.
The following is the code,which i am running on my Linux Box.
I am getting the return value from read as -1.

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

main()
{
 int fd[2],n;
  char   buff[4];

 fd[0]=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
 fd[1]=open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);
 if (fd[0]  || fd[1] <= 0)
 {
   perror("open_port:Unable to open /dev/ttyS0  or /dev/ttyS1");
   abrot();
  }

  /* Before writing data i am reading the buffer */

  fcntl(fd[0],F_SETFL,FNDELAY);
  n=read(fd[0],buff,4);
  printf("%d",n); /*here i am getting the value of n as -1*/
  fputs(buff,stdout);/*here i am getting some garbage values */

  fcntl(fd[1],F_SETFL,FNDELAY);/*here i am setting the flags*/
  n=read(fd[1],buff,4);
  printf("%d",n); /*here i am getting the value of n as -1*/
  fputs(buff,stdout);/*here i am getting garbage values */

 /* Writing data to com1*/

  fcntl(fd,F_SETFL,0);
  n=write(fd,"hai\r",4);
  printf("write n value is %d\n",n); /* here i am getting the value of
n as 4*/
  if (n<=0)
{
  fputs ("write() of 4 bytes to com1 is failed!\n",stderr);
  abort();
}

  /* Reading from the com2*/

  fcntl(fd[1],F_SETFL,FNDELAY);
  n=read(fd[1],buff,4);
  printf("read n value is %s\n",buff);
  if (n<=0)
   fputs ("read() of 4 bytes from com2 is failed!\n",stderr);
  fputs(buff,stdout);

 /*Closing the com ports*/

  close(fd[0]);
  close(fd[1]);
}


the output of the code is :garbage values(for 1st fputs)
                                     n=4
                                     n=-1(printf in read)
                                     read of 4 bytes from com2 is
failed
                                     garbage values(for last fputs)

what might
Hope i explaned clearly.
Thanks in Advance.


                                With Regards

                                  emb in linux.


Re: need a sample code
This is a multi-part message in MIME format.
--------------070906020105040002000405
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

emb in linux wrote:
Quoted text here. Click to load it

I haven't analyzed your code, but hereafter you will find an example
that works for me. I have separated the sending and the receiving parts
in two separate processes, each with its own source file and binary. I
start the receiving part first, and it receives the character from the
sending part when I start it later on.

Good luck,

Alain

Sending part:

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */


int
main()
{
    int fd; /* File descriptor for the port */
    struct termios options;

    /*
     * Open the first serial port, read/write, no controlling terminal,
     * not care about the Data Carrier Detect signal
     */
    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
        /*
         * Could not open the port.
         */
        perror("Unable to open /dev/ttyS0\n");
    }
    else {
        /*
         * Set all flags to 0 for the read call to be blocking!???
         * (according to "Serial Programming Guide for POSIX Operating Systems")
         */
        fcntl(fd, F_SETFL, 0);

        /*
         * Get the current options for the port...
         */
        tcgetattr(fd, &options);

        /*
         * Set the baud rates to 115200...
         */
        cfsetispeed(&options, B115200);
        cfsetospeed(&options, B115200);

        /*
         * Enable the receiver and set local mode...
         */
        options.c_cflag |= (CLOCAL | CREAD);

        /*
         * Set the option for 8N1
         */
        options.c_cflag &= ~PARENB;
        options.c_cflag &= ~CSTOPB;
        options.c_cflag &= ~CSIZE;
        options.c_cflag |= CS8;

        /*
         * Set the option for raw input
         */
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

        /*
         * Set the option for raw output
         */
        options.c_oflag &= ~OPOST;

        /*
         * Set the new options for the port...
         */
        tcsetattr(fd, TCSANOW, &options);

        if (write(fd,"A",1)<0)
              fprintf(stderr,"Write() of 1 byte failed!\n");
        close (fd);
    }

    return (0);
}


Receiving part:

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */


int
main()
{
    int fd; /* File descriptor for the port */
    char *c;
   struct termios options;

    /*
     * Open the first serial port, read/write, no controlling terminal,
     * not care about the Data Carrier Detect signal
     */
    fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
        /*
         * Could not open the port.
         */
        perror("Unable to open /dev/ttyS1\n");
    }
    else {
        /*
         * Set all flags to 0 for the read call to be blocking!???
         * (according to "Serial Programming Guide for POSIX Operating Systems")
         */
        fcntl(fd, F_SETFL, 0);

        /*
         * Get the current options for the port...
         */
        tcgetattr(fd, &options);

        /*
         * Set the baud rates to 115200...
         */
        cfsetispeed(&options, B115200);
        cfsetospeed(&options, B115200);

        /*
         * Enable the receiver and set local mode...
         */
        options.c_cflag |= (CLOCAL | CREAD);

        /*
         * Set the option for 8N1
         */
        options.c_cflag &= ~PARENB;
        options.c_cflag &= ~CSTOPB;
        options.c_cflag &= ~CSIZE;
        options.c_cflag |= CS8;

        /*
         * Set the option for raw input
         */
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

        /*
         * Set the option for raw output
         */
        options.c_oflag &= ~OPOST;

        /*
         * Set the new options for the port...
         */
        tcsetattr(fd, TCSANOW, &options);

        fprintf(stderr,"In server\n");
        if (read(fd,c,1)<1)
            fprintf(stderr,"Read() of 1 byte failed!\n");
        else
            fprintf(stderr,"Received %c\n",*c);
        close (fd);
    }

    return (0);
}

--------------070906020105040002000405
Content-Type: text/x-csrc;
 name="seriser.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="seriser.c"

/*
 * seriser
 * 2005, Alain Mosnier
 */

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */


int
main()
{
    int fd; /* File descriptor for the port */
    char *c;
  struct termios options;

    /*
     * Open the first serial port, read/write, no controlling terminal,
     * not care about the Data Carrier Detect signal
     */
    fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
        /*
         * Could not open the port.
         */
        perror("Unable to open /dev/ttyS1\n");
    }
    else {
        /*
         * Set all flags to 0 for the read call to be blocking!???
         * (according to "Serial Programming Guide for POSIX Operating Systems")
         */
        fcntl(fd, F_SETFL, 0);

        /*
         * Get the current options for the port...
         */
        tcgetattr(fd, &options);

        /*
         * Set the baud rates to 19200...
         */
        cfsetispeed(&options, B115200);
        cfsetospeed(&options, B115200);

        /*
         * Enable the receiver and set local mode...
         */
        options.c_cflag |= (CLOCAL | CREAD);

        /*
         * Set the option for 8N1
         */
        options.c_cflag &= ~PARENB;
        options.c_cflag &= ~CSTOPB;
        options.c_cflag &= ~CSIZE;
        options.c_cflag |= CS8;

        /*
         * Set the option for raw input
         */
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

        /*
         * Set the option for raw output
         */
        options.c_oflag &= ~OPOST;

        /*
         * Set the new options for the port...
         */
        tcsetattr(fd, TCSANOW, &options);

        fprintf(stderr,"In server\n");
        if (read(fd,c,1)<1)
            fprintf(stderr,"Read() of 1 byte failed!\n");
        else
            fprintf(stderr,"Received %c\n",*c);
        close (fd);
    }

    return (0);
}

--------------070906020105040002000405--

Re: need a sample code
hai
1st of all thanks for your sample code.
i want to check my comports on the same system.i am getting receive
program as "IN SERVER",but not getting the byte.how to acheive
that!what might be the wrong here!
 waiting for ur reply
                                                  with regards
                                                   emb in linux.
Quoted text here. Click to load it


Re: need a sample code
hai
thanks for your code .
i have acheived my startup task comports.
once again thanks for your help.

                                                            with
regards
                                                            emb in linux


Re: need a sample code
hi
i am able to read and write a string from and to com-ports and i am
unable to read a muti strings(sentence).how can i do that?
thanks in advance.

                                                                 with
regards
                                                                  emb
in linux
Alain Mosnier wrote:
Quoted text here. Click to load it


Re: need a sample code

hi
using fgets i am able to do that.

Quoted text here. Click to load it


Re: need a sample code
hello Alain
can i do this more interactivelly.
i did the following excercises.
1.i have  open a port and  a send a byte  then closed it .By using
receive program i opened other port read the  charecter .(in between
null modem cable is there)
2.same as above with string.
3.same as above with mutiple strings(sentence)

now i want do this little bit interactivelly.
i will open ttyS0 and give a byte from keyboard and the same byte i
have to recive from ttyS1 without closing ttyS0.here is it possible!
if u have any suggesstions please help me out.
thanks in advance.


with regards

emb in linux.


Re: need a sample code
Hi,

Quoted text here. Click to load it
A I said in my answer to the email to sent me directly:

To be honest I'm not sure whether I will have the time to help you out.
My advice is to post your code on the mailing list and ask questions as
specific as possible. Attach printouts from your program too. I guess if
you post your code, explain what you expect, and also post what you get
(exact printout from your program), people on the list will help you
out. On the other hand, I don't think you should expect that people will
give you code to answer your questions. In my case, you were lucky that
I had a litle sample at hand when I read your message.

Best regards,

Alain

Re: need a sample code
Quoted text here. Click to load it

And what does errno/perror() say ?

PS: The code appears to have been typed in (misspelling of abort() as abrot())

Try pasting the _actual_ source code.

Simon.

--
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP      
Microsoft: The Standard Oil Company of the 21st century

Site Timeline