Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Linux serial port programming

Linux serial port programming

Linux serial port programming

Been playing around with serial programming. I think I now have a good grasp of the concepts and syntax involved but I am having some problems. My code appears to be able to write happily to the serial port and appear to happily wait for input to arrive. No input ever seems to arrive from the modem.

I have confirmed the modem is functioning correctly. The serial port is fine (dual boot with XP) with hyperterminal. I have checked the modem documentation and ensure the serial settings are correct. I'm 100% sure all hardware is working.

I have various versions of this program. The one included below is a stripped down version. The configure_port function only sets the options that the modem manual mentions. I did include others in previous versions but had the same problems.

If anyone could help me on this it would be great!


// Serial port interface program               //

#include <stdio.h> // standard input / output functions
#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 definitionss
#include <time.h>   // time calls

int open_port(void)
int fd; // file description for the serial port

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

if(fd == -1) // if open is unsucessful
 perror("open_port: Unable to open /dev/ttyS0 - ");
 fcntl(fd, F_SETFL, 0);


int configure_port(int fd)      // configure the port
struct termios port_settings;      // structure to store the port settings in

cfsetispeed(&port_settings, B9600);    // set baud rates
cfsetospeed(&port_settings, B9600);

port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;

tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port


int query_modem(int fd)   // query modem with an AT command
int n;
fd_set rdfs;
struct timeval timeout;

// initialise the timeout structure
timeout.tv_sec = 10; // ten second timeout
timeout.tv_usec = 0;

write(fd, "AT\r", 3);  // send an AT command followed by a CR

// do the select
n = select(fd + 1, &rdfs, NULL, NULL, &timeout);

// check if an error has occured
if(n < 0)
 perror("select failed\n");
else if (n == 0)
 printf("\nBytes detected on the port!\n");


int main(void)
int fd = open_port();

RE: Linux serial port programming

Perhaps add this to the reading list

Perhaps the modem is in data mode, and you need to send it the escape sequence +++ to get it back into command mode where it will accept AT commands


RE: Linux serial port programming

Thanks but I don't think that is the case. I have played around with the modem with hyperterminal and I've never had to use the escape sequence you mention.

RE: Linux serial port programming

Spend some time making a null serial cable and connected two PC's together with it. I have now seen that my application does indeed write to the serial port as it appears in the serial terminal on the other machine. It's not essential for my app to read from the port but it would be nice. Going to do some further investigation!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close