Help with serial port.

Discussion in 'C Programming' started by laclac01@yahoo.com, Oct 8, 2005.

  1. Guest

    I need help, i can't figure out why this code doesnt work. I am using
    linux.
    All i want to be able to do is write some text to an LCD attached to my
    serial port on a linux machine. The LCD is serial and I know it works,
    baud is 2400 1N8. Attached to com 2.
    can someone tell me whats up with this code??
    Code:
    #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 */
    
    /* insert the brackets for each include */
    
    int fd;
    
    int open_port()
    {
    
    struct termios options;
    
    fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY
    );
    
    if (fd == -1)
    {
    fprintf(stderr, "open_port: Unable to open /dev/ttyS1
    - %s\n",
    strerror(errno));
    }
    else
    {
    fcntl(fd, F_SETFL, FNDELAY );
    
    if ( tcgetattr(fd, &options ) )
    {
    fputs("fehler\n", stderr); exit (-1);
    }
    
    cfsetispeed(&options, B2400);
    cfsetospeed(&options, B2400);
    
    
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    
    options.c_lflag |= (ICANON | ECHO | ECHOE);
    
    //options.c_cflag |= (CLOCAL | CREAD);
    //options.c_cflag |= PARENB; /* no parity */
    //options.c_cflag |= 0;
    //options.c_cflag |= ~CSTOPB;
    //options.c_cflag |= CS8; /* 7 data bits */
    //options.c_cflag |= CRTSCTS;
    
    /* Enable data to be processed as raw input */
    //options.c_lflag &= ~(ICANON | ECHO | ISIG);
    
    /* Set the new options for the port */
    if ( tcsetattr( fd, TCSANOW, &options ) )
    {
    fputs("fehler\n", stderr); exit (-1);
    }
    
    }
    return (fd);
    }
    int main()
    {
    
    int n=0;
    
    open_port();
    
    n = write( fd, "out_sp_00 25\n", 13 );
    if (n < 0)
    fputs("Error\n", stderr);
    
    /* Close the serial port */
    close(fd);
    
    return 1;
    }
    
    
    
     
    , Oct 8, 2005
    #1
    1. Advertising

  2. Skarmander Guest

    wrote:
    > I need help, i can't figure out why this code doesnt work. I am using
    > linux.
    > All i want to be able to do is write some text to an LCD attached to my
    > serial port on a linux machine. The LCD is serial and I know it works,
    > baud is 2400 1N8. Attached to com 2.

    <snip>

    You're looking for one of the comp.os.linux.* newsgroups, possibly
    comp.os.linux.development or comp.os.linux.hardware.

    This newsgroup only deals with questions regarding standard C.

    S.
     
    Skarmander, Oct 8, 2005
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    > I need help, i can't figure out why this code doesnt work. I am using
    > linux.
    > All i want to be able to do is write some text to an LCD attached to my
    > serial port on a linux machine. The LCD is serial and I know it works,
    > baud is 2400 1N8. Attached to com 2.
    > can someone tell me whats up with this code??


    No, this is wrong group for what you need. This group is dedicated to the
    standard C, which for some reason knows nothing (as per the standard) of the
    hardware connected to the CPU. Try a different group. Try documentation on
    ports and using them in your OS.

    Alex
     
    Alexei A. Frounze, Oct 8, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shashi
    Replies:
    0
    Views:
    336
    shashi
    Nov 18, 2004
  2. shashi
    Replies:
    1
    Views:
    376
    Miki Tebeka
    Nov 18, 2004
  3. Pom
    Replies:
    2
    Views:
    1,720
    Bas-i
    Jan 31, 2007
  4. chapeau_melon
    Replies:
    5
    Views:
    346
    red floyd
    Feb 28, 2007
  5. msalerno
    Replies:
    3
    Views:
    421
    Ilmari Karonen
    Jul 14, 2005
Loading...

Share This Page