Re: need a sample code

From: Alain Mosnier (alain.mosnier_at_chello.se)
Date: 11/08/05


Date: Tue, 08 Nov 2005 13:28:48 +0100


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);
}






Relevant Pages

  • Re: need a sample code
    ... > emb in linux wrote: ... 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: Slow browsing with cable modem
    ... >> This is your GW when using Linux. ... My, erh, comments related to the IPs in the first post where there ... I _think_ the first post inconsistencies were typos mostly, ... and two IP's for hosts is the smallest you can go. ...
    (comp.os.linux.networking)
  • SuSE 9.0 Professional LCD Support
    ... sorry for the cross post, but this is my first post in a linux ... SuSE 9.0 Profesional pure ... other) that support linux ...
    (alt.os.linux.suse)
  • Re: Uses for a TVRO dish antenna?
    ... What else could I do with this antenna? ... displaying good images of the Florida as seen from space. ... If you do build a system for receiving the NOAAPORT images, ... I am presently stuck due to my poor Linux skills, and Linux is used by the ...
    (rec.radio.amateur.antenna)