Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:15:42+00:00 2026-06-10T00:15:42+00:00

Obviously new to Linux serial programming. If I use /dev/ttyS4 from minicom or termie,

  • 0

Obviously new to Linux serial programming.

If I use /dev/ttyS4 from minicom or termie, I can communicate with another PC via a null-modem cable. With the code here, when I run the program, I receive no characters from the remote pc, but instead see the input from the local keyboard. I’m guessing this is a simple configuration mistake, but I’m not seeing it.

Insights appreciated!

#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 */



//const char NAK = 0x15;
char THE_PORT[] =  "/dev/ttyS4";
unsigned char m_messagebuf[255] ;

int main()
{
    int retval, fd, bwritten;   
    unsigned char m_recbuf[4];
    unsigned char m_sendbuf[73] ;       
    unsigned char haltchar = 0x1d;
    unsigned char c;
    char* p = 0;

    m_sendbuf[0] =  0x15;

    strcpy(m_messagebuf, "testing ");
    strcat(m_messagebuf, THE_PORT);
    printf("%s\n", m_messagebuf);
    sleep(1);

    //return(0);
    if (fd = open_port() < 0)
    {
        printf("oops...\n");
        return(0);
    }

    printf( "opened %s\n", THE_PORT);
    sleep(1);

    if (ConfigurePort(fd) < 0)
    {
        printf("oops...\n");
        return 0;
    }
    printf("configured %s\n", THE_PORT);
    sleep(1);

    while( 1)
    {
        printf("waiting for %s\n", THE_PORT);
        if ((retval = read(fd, &c, 1)) > 0)
        {
            printf("read %d from port : \n", retval);
            //m_recbuf[retval] = '0';
            printf("%c\n", c);

        }
        else
        {
            printf("Read 0 or  -1 from port (%d)\n", retval);
        }
        sleep(1);

        //bwritten = write(fd, (void *)&m_sendbuf, 1);
        //printf("wrote %d to port\n", bwritten);
    }       
    printf("Done.\n");
    close(fd);
    return 0;   

}


int ConfigurePort(int fd)
{   
    struct termios options;

    if ( tcgetattr(fd, &options) < 0)
    {

        perror(strcat("config_port: Unable to get attribs for ", THE_PORT));
        return -1;      
    }
    if (memset(&options, 0, sizeof(options)) < 0)
    {
        sprintf(m_messagebuf, "config_port: Unable to clear settings %s\n", THE_PORT);
        perror(m_messagebuf);
        return -1;
    }

    // 9600
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    // N81
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    // flow control OFF
    //options.c_cflag &= ~CNEW_RTSCTS;    
    // enable resd, keep owner
    options.c_cflag |= (CLOCAL | CREAD);

    // local options for raw input    
   options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    // raw output
    options.c_oflag &= ~OPOST;

    // raw input    
    options.c_iflag = INPCK | ISTRIP | IGNCR | IGNBRK;
    options.c_iflag &=  ~(IXON | IXOFF | IXANY);

    options.c_cc[VINTR]    = 0;     /* Ctrl-c */ 
    options.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
    options.c_cc[VERASE]   = 0;     /* del */
    options.c_cc[VKILL]    = 0;     /* @ */
    options.c_cc[VEOF]     = 0;     /* Ctrl-d */
    options.c_cc[VSWTC]    = 0;     /* '\0' */
    options.c_cc[VSTART]   = 0;     /* Ctrl-q */ 
    options.c_cc[VSTOP]    = 0;     /* Ctrl-s */
    options.c_cc[VSUSP]    = 0;     /* Ctrl-z */
    options.c_cc[VEOL]     = 0;     /* '\0' */
    options.c_cc[VREPRINT] = 0;     /* Ctrl-r */
    options.c_cc[VDISCARD] = 0;     /* Ctrl-u */
    options.c_cc[VWERASE]  = 0;     /* Ctrl-w */
    options.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
    options.c_cc[VEOL2]    = 0;     /* '\0' */

    options.c_cc[VMIN] = 1;     /* blocking read until 1 character arrives */
    options.c_cc[VTIME] = 0;        /* inter-character timer unused */

    //Set the new options for the port...      
    if (tcsetattr(fd, TCSAFLUSH, &options) < 0)
    {
        sprintf(m_messagebuf, "config_port: Unable to configure %s\n", THE_PORT);
        perror(m_messagebuf);
        return -1;
    }       
}

/*
 * Returns the file descriptor on success or -1 on error.
 */
int open_port(void)
{
      int fd; /* File descriptor for the port */

      fd = open(THE_PORT, O_RDWR | O_NOCTTY  | O_NDELAY | O_NONBLOCK); 
      if (fd == -1)
      {
            sprintf(m_messagebuf, "open_port: Unable to open %s\n", THE_PORT);
            perror(m_messagebuf);
            return -1;
      }       
     return (fd);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:15:43+00:00Added an answer on June 10, 2026 at 12:15 am
    if (fd = open_port() < 0)
    

    is equivalent to

    if (fd = (open_port() < 0))
    

    and < operator returns 0 or 1, this is not what you want for your file descriptor.

    You probably wanted to do:

    if ((fd = open_port()) < 0)
    

    instead.

    Another issue: your function ConfigurePort has no default return value at the end (the final }). No return value means it returns an indeterminate value and reading this value is undefined behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to C++ programming and am obviously missing something. In the following
I'm new to event programming, and I'm obviously misunderstanding something that I'm trying to
I am quite new to linux development, and having trouble using a class from
I am quite new to triggers so obviously I am doing something wrong somewhere.
I'm new to VisualForce, as you will obviously recognize by the following. How would
I've got a weird problem and it's most obviously due to my new status
I have a simple query: $query = new WP_Query('showposts=5'); that will obviously display 5
I am working on making a new SQLite database. Obviously for a number of
I'm still new to the Ada programming world so forgive me if this question
I was just downloading a new distro of linux using uTorrent, and started to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.