Re: need a sample code

From: emb in linux (haranath.t_at_gmail.com)
Date: 11/11/05

  • Next message: emb in linux: "Re: need a sample code"
    Date: 10 Nov 2005 21:41:11 -0800
    
    

    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:
    > Hi,
    >
    > emb in linux wrote:
    > > 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.
    >
    > 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);
    > }
    >
    > /*
    > * 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);
    > }


  • Next message: emb in linux: "Re: need a sample code"

    Relevant Pages

    • Re: need a sample code
      ... > This is my first post to this group.I am very much new to embedded ... > The following is the code,which i am running on my Linux Box. ... I have separated the sending and the receiving parts ...
      (comp.os.linux.embedded)
    • Re: need a sample code
      ... emb in linux wrote: ... I have separated the sending and the receiving parts ... >> in two separate processes, each with its own source file and binary. ...
      (comp.os.linux.embedded)
    • Re: pushing the envelope with sockets
      ... receiving on the socket they are received (upto the buffer size), you can even change what happens if the buffer runs full. ... int read = S.EndReceive; ... class AsyncReader: Reader ...
      (microsoft.public.dotnet.languages.csharp)
    • [Full-disclosure] Dropbear SSH server Denial of Service
      ... Dropbear SSH server Denial of Service ... Dropbear is a relatively small SSH 2 server and client. ... LEAF Bering uClibc - a small Linux firewall/network applicance ... int max_unauth_clients; ...
      (Full-Disclosure)
    • Dropbear SSH server Denial of Service
      ... Dropbear SSH server Denial of Service ... Dropbear is a relatively small SSH 2 server and client. ... LEAF Bering uClibc - a small Linux firewall/network applicance ... int max_unauth_clients; ...
      (Bugtraq)