请问各位高人,mips平台上Android系统的一体机,用AT测试3G网卡为什么会发生回环?如何消除?
现在是我发“AT\r”串口回“AT OK ”我想消除回环,请高手指教!代码如下:
int main(void)
{
int fd;
struct termios options;
/* open the port */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
/* get the current options */
tcgetattr(fd, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag &= ~PARENB
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* set raw input, 1 second timeout */
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
init_modem(fd);
/* set the options */
tcsetattr(fd, TCSANOW, &options);
close(fd);
}
int /* O - 0 = MODEM ok, -1 = MODEM bad */
init_modem(int fd) /* I - Serial port file */
{
char buffer[255]; /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */
int tries; /* Number of tries so far */
for (tries = 0; tries < 3; tries ++)
{
/* send an AT command followed by a CR */
if (write(fd, "AT\r", 3) < 3)
continue;
/* read characters into our string buffer until we get a CR or NL */
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
// if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
// break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
printf("\nread string is %s",buffer);
if (strncmp(buffer, "OK", 2) == 0)
return (0);
}
return (-1);
}
程序运行的结果如下:
read string is AT
OK
正确的结果应该是只会回“OK”,现在串口把“AT”先弹回来了,对串口的配置调了很多次,同步异步也分别进行了测试,结果都是这样。有没接触过linux下USB转串口的前辈请指点一下。 |