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
  • Your usage of FD_ISSSET() and FD_ZERO() and FD_CLR() makes no sense. You need FD_ZERO() plus FD_SET(), unconditionally. See @ott-- answer below. Commented May 23, 2012 at 16:27

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I don't understand, what do you mean by "you haven't added fd to rfds"? How do i set a fd? I'm not sure... please advise! Thanks!
Check the manpage for select (man 3 select) for FD_ZERO, FD_SET and FD_ISSET.
Okay, Thank you Ott! I added this but nothing has really changed on the result. Added following lines just before the write(): if (FD_ISSET(fd, &rdfs)){ FD_ZERO(&rdfs); FD_CLR(fd,&rdfs); }
Okay, I got the sending and receiving figured out correctly now! I can confirm that data sent is correct and I can read data back with read() but the select doesn't kick in....why is that? Thanks for any hints or suggestions that you can provide!
@cerr Can you update your code to the current state? What does select() return? Or does it just timeout?
|
0

Simple c code

char types[25];    
char buffu[256];   
int fd:

strcat(types,"speed 115200 -parenb cs8 cstopb -ixon -ixoff -crtscts clocal cread raw");   
fd=open("dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);   
ioctl(fd, F_SETFL, 0);   

.....

write (fd, buffu, 255);   
usleep(400);   
read (fd,buffu,255);   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.