I have written a small and simple serial port read/write application to connect to my modem and to run commands. There is a problem. The program works perfectly for the most part but when the command takes a bit of time to execute and respond, the program acts strangely. Here is my code:
void write (char cmd[256]) {
int n_written = 0;
char atcmd [259];
sprintf (atcmd, "%s\r\n", cmd);
n_written = write( fd, &atcmd, strlen(atcmd) );
printf ("WROTE: %d\n", n_written);
}
void read (char * cmdresp) {
int n = 0;
char response[1024];
memset(response, 0 , sizeof response);
n = read( fd, &response, sizeof (response) );
if (n < 0) {
printf ("ERROR=%d - %s\n", errno, strerror(errno));
} else if (n == 0) {
printf ("Read nothing!\n");
} else {
printf ("READ: %s\n", response);
strcpy (cmdresp, response);
}
}
void manage_transaction (char * message, char * response) {
tv.tv_sec = 5;
tv.tv_usec = 0;
write (message);
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
int retval = select(fd+1, &rfds, NULL, NULL, &tv);
if (retval == -1) {
perror("select()");
} else if (retval) {
if (FD_ISSET(fd, &rfds) ) {
usleep(200000);
read(response);
}
} else {
printf("No data within five seconds.\n");
}
}
这是两个连续的运行,一个成功的运行不成功:
Successful:
AT> AT+CGSN
Client ConnectedHere is the message: AT+CGSN
WROTE: 11
READ: AT+CGSN
123456789012345
OK
AT+CGSN
123456789012345
OK
FAILED:
AT> ATD11234567890;
Here is the message: ATD11234567890;
WROTE: 19
READ: ATD11234567890;
AT> 1234567890;
&lt;&lt; - 如果我按回车键或发送另一条消息,其余信息将通过:
Here is the message:
WROTE: 4
READ:
OK
OK
AT>
我错过了什么吗?我该如何解决这个问题?
非常感谢任何想法。
***********编辑*************** 以下是open和config代码:
int open_port(void)
{
fd = open("/dev/ttyUSB2", O_RDWR | O_NOCTTY);
if(fd == -1) {
perror("open_port: Unable to open /dev/ttyUSB2");
}
else {
fcntl(fd, F_SETFL, 0);
printf("port is open.\n");
}
tcflush(fd, TCIFLUSH);
tcflush(fd, TCOFLUSH);
return fd;
}
void config () {
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
if ( tcgetattr ( fd, &tty ) != 0 ) {
printf ("ERROR=%d - %s\n", errno, strerror(errno));
}
tty_old = tty;
/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B115200);
cfsetispeed (&tty, (speed_t)B115200);
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_iflag &= ~INPCK;
tty.c_iflag |= (IGNBRK | BRKINT);
tty.c_iflag |= IGNPAR;
tty.c_iflag &= ~ICRNL;
tty.c_iflag &= ~IXON;
tty.c_iflag &= ~ISTRIP;
tty.c_iflag &= ~INLCR;
tty.c_iflag &= ~IGNCR;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 0; // 5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
tty.c_iflag &= ~(IXON |IXOFF|IXANY); /* no XON/XOFF flow control */
tty.c_oflag &= ~(IXON |IXOFF|IXANY); /* no XON/XOFF flow control */
tty.c_oflag &= ~(OPOST | ONLCR );
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes */
tcflush(fd, TCIFLUSH );
tcflush(fd, TCIOFLUSH );
int reuse = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse));
if ( (tcsetattr(fd,TCSANOW,&tty) < 0) ) {
printf ("ERROR=%d - %s\n", errno, strerror(errno));
}
}