zoukankan      html  css  js  c++  java
  • linux 短信收发

    #include <termios.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
            #include <fcntl.h>
            #include <string.h>
            #include <sys/types.h>
            #include <sys/stat.h>

    struct message_info{
                    char cnnu[16];
                    char phnu[16];
                    char message[128];
            };

    struct pdu_info {
                    char cnswap[32];
                    char phswap[32];
            };

    void serial_init(int fd)
    {
                    struct termios options;
                    tcgetattr(fd, &options);
                    options.c_cflag |= ( CLOCAL | CREAD );
                    options.c_cflag &= ~CSIZE;
                    options.c_cflag &= ~CRTSCTS;
                    options.c_cflag |= CS8;
                    options.c_cflag &= ~CSTOPB;
                    options.c_iflag |= IGNPAR;
                    options.c_oflag = 0;
                    options.c_lflag = 0;

                cfsetispeed(&options, B9600);
        cfsetospeed(&options, B9600);
        tcsetattr(fd, TCSANOW, &options);
    }

    void swap(char number[], char swap[]) {
        char ch1[] = "86";
        char tmp[16];
        int i;

        memset(swap, 0, 32);
        memset(tmp, 0, 16);
        strcpy(swap, number);
        strcat(swap, "f");
        strcat(ch1, swap);
        strcpy(swap, ch1);

        for (i = 0; i <= strlen(swap) - 1; i += 2) {
            tmp[i + 1] = swap[i];
            tmp[i] = swap[i + 1];
        }
        strcpy(swap, tmp);
    }

    int send(int fd, char *cmgf, char *cmgs, char *message) {
        int nread, nwrite;
        char buff[128];
        char reply[128];

        memset(buff, 0, sizeof (buff));
        strcpy(buff, "at ");
        nwrite = write(fd, buff, strlen(buff));
        printf("nwrite=%d,%s ", nwrite, buff);

        memset(reply, 0, sizeof (reply));
        sleep(1);
        nread = read(fd, reply, sizeof (reply));
        printf("nread=%d,%s ", nread, reply);

        memset(buff, 0, sizeof (buff));
        strcpy(buff, "AT+CMGF=");
        strcat(buff, cmgf);
        strcat(buff, " ");
        nwrite = write(fd, buff, strlen(buff));
        printf("nwrite=%d,%s ", nwrite, buff);

        memset(reply, 0, sizeof (reply));
        sleep(1);
        nread = read(fd, reply, sizeof (reply));
        printf("nread=%d,%s ", nread, reply);

        memset(buff, 0, sizeof (buff));
        strcpy(buff, "AT+CMGS=");
        strcat(buff, cmgs);
        strcat(buff, " ");
        nwrite = write(fd, buff, strlen(buff));
        printf("nwrite=%d,%s ", nwrite, buff);

        memset(reply, 0, sizeof (reply));
        sleep(1);
        nread = read(fd, reply, sizeof (reply));
        printf("nread=%d,%s ", nread, reply);

        memset(buff, 0, sizeof (buff));
        strcpy(buff, message);
        nwrite = write(fd, buff, strlen(buff));
        printf("nwrite=%d,%s ", nwrite, buff);

        memset(reply, 0, sizeof (reply));
        sleep(1);
        nread = read(fd, reply, sizeof (reply));
        printf("nread=%d,%s ", nread, reply);
    }

    int send_en_message(int fd, struct message_info info) {
        getchar();
        char cmgf[] = "1";
        int conter = 0;
        char cmgs[16] = {''};

        printf("enter recever phnumber : ");
        gets(info.phnu);
        while (strlen(info.phnu) != 11) {
            if (conter >= 3) {
                printf("conter out ! ");
                return -1;
            }
            printf("number shuld be --11-- bits ! enter agin : ");
            gets(info.phnu);
            conter++;
        }

        printf("enter you message ! ");
        gets(info.message);
        strcat(info.message, "x1a");
        strcat(cmgs, info.phnu);

        send(fd, cmgf, cmgs, info.message);
    }

    int send_zh_message(int fd, struct message_info info) {
        char cmgf[] = "0";
        char cmgs[4] = {''};
        char ch2[] = "0891";
        char ch3[] = "1100";
        char ch4[] = "000800";
        char ch5[] = "0d91";
        char final[128];
        char *message[3] = {
            "0a5BB691CC7740706BFF01",
            "0a5BB691CC67098D3CFF01",
            "1a676866539E4FFF0C4F605988558A4F6056DE5BB65403996DFF01"
        };
        struct pdu_info pdu;
        int conter = 0, flag, len;
        getchar();
        memset(final, 0, 80);

        printf("enter your centre phnumber : ");
        gets(info.cnnu);
        while (strlen(info.cnnu) != 11) {
            if (conter >= 3) {
                printf("conter out ! ");
                return -1;
            }
            printf("number shuld be --11-- bits ! enter agin : ");
            gets(info.cnnu);
            conter++;
        }

        printf("enter your recever phnumber : ");
        gets(info.phnu);
        while (strlen(info.phnu) != 11) {
            if (conter >= 3) {
                printf("conter out ! ");
                return -1;
            }
            printf("number shuld be --11-- bits ! enter agin : ");
            gets(info.phnu);
            conter++;
        }
        printf("choice message : ");
        printf("1.fire. ");
        printf("2.thief. ");
        printf("3.mother@home. ");
        scanf("%d", &flag);
        swap(info.phnu, pdu.phswap);
        swap(info.cnnu, pdu.cnswap);

        strcpy(final, ch2);
        strcat(final, pdu.cnswap);
        strcat(final, ch3);
        strcat(final, ch5);
        strcat(final, pdu.phswap);
        strcat(final, ch4);
        strcat(final, message[flag - 1]);
        strcat(final, "x1a");

        len = strlen(ch3) + strlen(ch4) + strlen(ch5) + strlen(pdu.phswap) + strlen(message[flag - 1]);
        puts(final);
        sprintf(cmgs, "%d", len / 2);
        puts(final);
        send(fd, cmgf, cmgs, final);
    }

    int main() {
        int fd;
        char choice;
        struct message_info info;
        fd = open("/dev/s3c2410_serial1", O_RDWR | O_NOCTTY | O_NDELAY);
        if (-1 == fd) {
            perror("Can't Open Serial Port");
        }
        serial_init(fd);
        printf(" ============================================ ");
        printf(" this is a gprs test program ! ");
        printf(" copyright fj@farsight 2011 ");
        printf("============================================ ");
        printf("enter your selete : ");
        printf("1.send english message. ");
        printf("2.send chinese message. ");
        printf("3.exit. ");
        choice = getchar();
        switch (choice) {
            case '1': send_en_message(fd, info);
                break;
            case '2': send_zh_message(fd, info);
                break;
            case '3': break;
            default: break;
        }
        close(fd);
        return 0;

    }

  • 相关阅读:
    古文_硕鼠,原文及翻译
    使用php模拟post的几种方法
    alpha版、beta版、rc版的意思
    8007003Windows Update遇到未知错误
    树上10只鸟,开枪打死1只,还剩几只?
    [转]乐死我了,怎么样成为一个全栈程序员(Full Stack Developer),附我想专注的语言
    [转]Visual C++ RunTime的特征——非烫即屯
    斗破苍穹中的几个人物图片
    吐槽一下中国的大学的教材
    百度知道里关于C++的讨论
  • 原文地址:https://www.cnblogs.com/zym0805/p/4885595.html
Copyright © 2011-2022 走看看