English 中文(简体)
C中 Linux 的连续序列编程
原标题:serial programming on linux in C

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
", 5);  // send an AT command followed by a CR
/*usleep(50);
while (read(fd, &chr, 1))
{
printf("0x%x
",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
");
}
else if (n == 0)
{
printf("Timeout
");
}
else
{
printf("
Bytes detected on the port!
");
}

}

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!

罗罗

最佳回答

我的建议是, 您没有在 rfd 中添加 fd 。 请检查 FD 宏来清除一个集, 添加 fd 并检查 fd 是否有输入 。

<强 > 更新

您真的需要将 fd 添加到 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

当选择多个 fd 时, n 可以成为 & gt; 1 。

问题回答

简单 c 代码

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);   




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签