提前感谢您的帮助。
[使用以下示例Canonical Mode Linux Serial Port,我开始在C中编写一些API以发送AT命令并通过串行端口接收响应。我没问题读取响应(用于轮询的非阻塞读取),发现启用“ at命令”的设备也没有问题。
我面临的问题是写功能。大多数命令都起作用(最小的命令,如AT,ATI,ATI + CIMI等)。有时,诸如发送SMS之类的命令会失败。我认为问题在于写入的速度(比串行更快)。
如果在写操作和下一个写操作之间设置计时器,则不会发生所有问题。
以下为代码
int serial_write(int fd, char * command){
size_t len = strlen(command);
int wlen = write(fd, command, len);
if (wlen != len) {
return -1;
}
usleep(80*1000L);
if ( tcdrain(fd) != 0){
return -2;
}
return 0;
}
int open_tty(char *portname){
int fd;
/*Aperta NON bloccante, con la poll che aspetta 1 secondo*/
fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
if (fd < 0) {
printf("Error opening %s: %s\n", portname, strerror(errno));
return -1;
}
/*baudrate 115200, 8 bits, no parity, 1 stop bit */
if (set_interface_attribs(fd, B115200) < 0 ){
printf("Error set_interface_attribs: %s\n", strerror(errno));
return -1;
}
return fd;
}
int set_interface_attribs(int fd, int speed){
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
if ( cfsetospeed(&tty, (speed_t)speed) < 0 ){
printf("Error from cfsetospeed: %s\n", strerror(errno));
return -1;
}
if ( cfsetispeed(&tty, (speed_t)speed) < 0 ){
printf("Error from cfsetispeed: %s\n", strerror(errno));
return -1;
}
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; /* 8-bit characters */
tty.c_cflag &= ~PARENB; /* no parity bit */
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
tty.c_lflag |= ICANON | ISIG; /* canonical input */
tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN);
tty.c_iflag &= ~IGNCR; /* preserve carriage return */
tty.c_iflag &= ~INPCK;
tty.c_iflag &= ~(INLCR | ICRNL | IUCLC | IMAXBEL);
tty.c_iflag &= ~(IXON | IXOFF | IXANY); /* no SW flowcontrol */
tty.c_oflag &= ~OPOST;
tty.c_cc[VEOL] = 0;
tty.c_cc[VEOL2] = 0;
tty.c_cc[VEOF] = 0x04;
tty.c_cc[VTIME] = 10;
tty.c_cc[VMIN] = 0;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
int read_response(int fd, char ** res){
int count=1; /* contatore realloc 1 per lo \0*/
tcdrain(fd); /* waits until all of the data that has been written has been sent */
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN ;
do {
unsigned char buf[MAXBUF];
unsigned char *p;
int rdlen;
int n = poll( fds, 1, 1000);
if (n>0){
rdlen = read(fd, buf, sizeof(buf) - 1);
if (rdlen > 0) {
buf[rdlen] = 0;
for (p = buf; rdlen-- > 0; p++) {
if (*p < ' ')
*p = '\0'; /* replace any control chars */
}
if ( (strcmp((char *)buf, "") != 0) || (buf[0] == '^') ){
count += (strlen((char *)buf)+1); /* 2 per ; e ' ' */
*res = realloc (*res, count);
strncat(*res, (char *)buf, strlen((char *)buf));
strcat(*res, ";");
}
if (strcmp((char *)buf, ATCMD_OK) == 0){
return 0;
}
if (strcmp((char *)buf, ATCMD_ERROR) == 0){
return -1;
}
} else if (rdlen < 0) {
return -2;
} else { /* rdlen == 0 */
return -3;
}
} else {
return -4;
}
/* repeat read */
} while (1);
}
int send_sms(int fd, char *tel, char *text){
int wlen = 0;
char *res = malloc(sizeof(char*));
char at_send[strlen(ATCMD_CMGS) + strlen(tel) + 3]; //3=2apici+"\0"
strcpy(at_send, ATCMD_CMGS);
strcat(at_send, DL_QUOTE);
strcat(at_send, tel);
strcat(at_send, DL_QUOTE);
printf("Setting to sms text mode... ");
if ( (wlen = serial_write(fd, ATCMD_CMGF)) < 0 ){
printf("Error from write: %d, %d\n", wlen, errno);
}
if ( (wlen = serial_write(fd, C_R)) < 0 ){
printf("Error from write: %d, %d\n", wlen, errno);
}
if (read_response(fd, &res) < 0 ) {
printf("FAIL\n");
}
else {
printf("OK, RES: %s\n",res);
}
free(res);
printf("Sending SMS...");
if ( (wlen = serial_write(fd, at_send)) < 0 ){
printf("Error from write: %d, %d\n", wlen, errno);
}
if ( (wlen = serial_write(fd, C_R)) < 0 ){
printf("Error from write: %d, %d\n", wlen, errno);
}
if ( (wlen = serial_write(fd, text)) < 0 ){
printf("Error from write: %d, %d\n", wlen, errno);
}
if ( (wlen = serial_write(fd, CTRL_Z)) < 0 ){
printf("Error from write: %d, %d\n", wlen, errno);
}
if (read_response(fd, &res) < 0 ) {
printf("FAIL\n");
free(res);
return -1;
}
else {
printf("OK, RES: %s\n",res);
free(res);
return 0;
}
}
这些是最重要的功能。您可以在serial_write()中看到我正在使用usleep(),并且一切正常。删除usleep()会导致问题(如果有tcdrain也会出现)。
将提供各种帮助。
谢谢。
有时发送SMS之类的命令失败。
这不是对该问题的帮助或详细描述。简单地指出存在问题,然后期望解决方案是不合理的。
如果在写操作和下一个写操作之间设置计时器,则不会发生所有问题。
这表明您的程序在传输新的命令/消息之前没有正确等待调制解调器的响应。接收响应(而不是等待传输完成,即调用tcdrain(),和/或延迟任意时间间隔,例如调用usleep())是调制解调器正在正常运行的正确指示。现在准备接收。
您没有问题的命令以基本的命令和响应对话框为特征。发送一条单行消息,并以简短的顺序接收一条单行消息作为响应。
但是使用CMGS命令发送SMS消息不会遵循该简单对话框。但是您的程序无论如何都试图强制采用“一个消息->一个响应”的构造(结果显然是不可靠的结果)。根据GSM Technical Specification,可以/应该将CMGS命令作为
的写/读交换来处理。[w]command -> [r]prompt_response -> [w]text -> [r]prompt_response ...
[w]text -> [r]prompt_response -> [w]text + ^Z -> [r]2-line response
其中prompt_response
被指定为
a four character sequence <CR><LF><greater_than><space>
请注意,此响应不适用于规范读取(即常规行终止符位于此序列的开头而不是结尾)。还要注意,已发送消息文本中的每个回车符都会产生调制解调器对prompt_response
的发送。
鉴于CMGS命令的复杂性,程序要传输多行,然后期望仅处理一个响应,因此容易产生不可靠的结果。我发现您的代码存在其他问题,但是没有像CMGS命令的这种错误处理那样严重。该“答案”将仅指出程序中的主要缺陷(即提供“ help”),而不提供解决方案和/或重写。