-1

此代码发送一些 SMS 消息。但它会为某些消息提供错误。可能是 GSM 模块无法识别的一些错误字符,例如 `(。有没有人可以帮助修复代码。我正在检查 GSM 设备是否连接到 ttyUSB2。这是 C++ 代码。

int sendSms(UsbPort &device, string phoneNumber, string message) {

    char buf[MAXBUF] = {0};
    int n;

    if(device.fildes > 0) {
        int max_check = 0;

        if (write(device.fildes, "ATZ\r", 4) < 4) {
            printf("ATZ write error - %s \n", strerror(errno));
            return -1;
        }

        this_thread::sleep_for(chrono::seconds(1));

        if (write(device.fildes, "AT+CMGF=1\r", 10) < 10) {
            printf("AT+CMGF=1 write error - %s \n", strerror(errno));
            return -1;
        }

        if (tcdrain(device.fildes) != 0) {
            perror("tcdrain() error");
            return -1;
        }

        this_thread::sleep_for(chrono::seconds(1));
        bzero(buf, sizeof(buf));
        read(device.fildes, buf, MAXBUF - 1);
        cout << "Buffer-1 = " << buf << endl;

        if (write(device.fildes, "ATE+CSMS=1\r", 11) < 11) {
            printf("ATE+CSMS=1 write error - %s \n", strerror(errno));
            return -1;
        }

        if (tcdrain(device.fildes) != 0) {
            perror("tcdrain() error");
            return -1;
        }

        this_thread::sleep_for(chrono::seconds(1));
        bzero(buf, sizeof(buf));

        if ((n = read(device.fildes, buf, MAXBUF - 1)) > -1) {
            int length = strlen(buf);
            cout << "length = " << length << endl;
            buf[length] = '\0';
            cout << "Buffer-2 = " << buf << endl;
            if (strstr(buf, "+CSMS:") != NULL) { // Ready to send SMS
                cout << "Can send SMS: " + string(buf) << endl;
                // Send SMS
                string data1 = "AT+CMGS=\"" + phoneNumber + "\"\r";
                if (write(device.fildes, data1.c_str(), data1.length()) < data1.length()) {
                    printf("AT+CMGS write error - %s \n", strerror(errno));
                    return -1;
                }
                else {
                    this_thread::sleep_for(chrono::seconds(1));
                    string data2 = message + "\x1A";
                    if (write(device.fildes, data2.c_str(), data2.length()) < data2.length()) {
                        printf("ATE+CSMS=1 write error - %s \n", strerror(errno));
                        return -1;
                    }

                    if (tcdrain(device.fildes) != 0) {
                        perror("tcdrain() error");
                        return -1;
                    }

                    this_thread::sleep_for(chrono::seconds(1));
                    bzero(buf, sizeof(buf));
                    if ((n = read(device.fildes, buf, MAXBUF - 1)) > -1) {
                        int length = strlen(buf);
                        cout << "length = " << length << endl;
                        buf[length] = '\0';
                        cout << "Buffer-2 = " << buf << endl;
                        if (strstr(buf, "OK") != NULL) {
                            cout << message + " sent to : " + telNo + " successfully." << endl;
                            return 0;
                        }
                    }
                    else {
                        cout << message + " sent to : " + telNo + " unsuccessful!" << endl;
                        return -1;
                    }
                }
            }
            else {
                cout << "Error: buf = " + string(buf) << endl;
                return -1;
            }
        }
    }
    return -1;
}

这是它无法发送短信的示例。

它没有发送的消息

4

1 回答 1

1

问题不在于 ASCII 字符,而在于发送比应有的更长的消息。

当我们使用Concatenated SMS时,它解决了我们的问题。它发送短信没有问题。

AT+CGMF=1
AT+CSCS="GSM"
AT+CSMP=17,167,0,241
AT+CMGS="Number"
> Message <CTRL+Z>
于 2021-02-12T11:27:10.937 回答