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 7989981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:53:11+00:00 2026-06-04T12:53:11+00:00

I want to send a certain string on my serial port and read the

  • 0

I want to send a certain string on my serial port and read the answer into a buffer for further analysis. I have come up with some code but am not able to read any answer even tho screen /dev/ttyUSB0 19200 on the shell works just fine for me. The decive expects 8 data bits, 1 start bit, 1 stop bit, and no parity. at 19200 baud.
Now my code looks like this and it keeps timning out: 🙁

/////////////////////////////////////////////////
// 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/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

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

return(fd);
}

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

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

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;

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

}

int query_modem(int fd)   // query modem with an AT command
{
int n;
fd_set rdfs;
struct timeval timeout;
ssize_t retval;
char bufptr[100];
char chr;
int cnt = 0;
int i = 0;

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

if (FD_ISSET(fd, &rdfs)){
  FD_ZERO(&rdfs);
  FD_CLR(fd,&rdfs);
}

retval = write(fd, "TEST\r", 5);  // send an AT command followed by a CR
/*usleep(50);
while (read(fd, &chr, 1))
{
printf("0x%x\n",chr);
usleep(10);
}*/

// 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("Timeout\n");
}
else
{
printf("\nBytes detected on the port!\n");
}

}

int main(void)
{
int fd = open_port();
configure_port(fd);
query_modem(fd);
return(0);
}

What I would expect back is a string saying
“TEST” the same as in screen upon I hit enter.
Any help would be appreciated!
Thank you very much!

Ron

  • 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-04T12:53:13+00:00Added an answer on June 4, 2026 at 12:53 pm

    My suggestion is that you haven’t added fd to rfds. Check the FD_* macros to clear a set, add a fd and check if a fd has input.

    Update

    You really need to add the fd to the fdset:

    write(...
    FD_ZERO(&rdfs);
    FD_SET(fd, &rfds);
    n = select(...
    if (n > 0) {
        if (FD_ISSET(fd, &rfds)) {
            // this fd has input waiting to be read
    

    n can become > 1 when you are selecting multiple fd.

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

Sidebar

Related Questions

I have an ArrayList of type String that contain certain values. I want to
I want to send push notification to certain users only. From what I have
I want to check users' subscribed dates for certain period. And send mail to
I want to send some info back to my database when a user prints
I have a pretty big table where I want to get certain rows by
I'm writing some C++ code that will have to send data over TCP/IP. I
I want send a email by Email Class in codeigniter with gmail, but i
In my application i want send a email to one account.in one button click
Hello Sir i want send list of data to php server i use following
Want to send the text from my current vb application to the Active Window

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.