#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\r");
    nwrite = write(fd, buff, strlen(buff));
    printf("nwrite=%d,%s\n", nwrite, buff);

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

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

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

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

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

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

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

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

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

printf("enter you message !\n");
    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] = {'\0'};
    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 :\n");
    gets(info.cnnu);
    while (strlen(info.cnnu) != 11) {
        if (conter >= 3) {
            printf("conter out !\n");
            return -1;
        }
        printf("number shuld be --11-- bits ! enter agin :\n");
        gets(info.cnnu);
        conter++;
    }

printf("enter your recever phnumber :\n");
    gets(info.phnu);
    while (strlen(info.phnu) != 11) {
        if (conter >= 3) {
            printf("conter out !\n");
            return -1;
        }
        printf("number shuld be --11-- bits ! enter agin :\n");
        gets(info.phnu);
        conter++;
    }
    printf("choice message :\n");
    printf("1.fire.\n");
    printf("2.thief.\n");
    printf("3.mother@home.\n");
    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("\n============================================\n");
    printf("\tthis is a gprs test program !\n");
    printf("\tcopyright fj@farsight 2011\n");
    printf("============================================\n");
    printf("enter your selete :\n");
    printf("1.send english message.\n");
    printf("2.send chinese message.\n");
    printf("3.exit.\n");
    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;

}

linux 短信收发的更多相关文章

  1. 在Linux下用C语言实现短信收发

     本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lyserver/archive/2008/10/01/3007090.aspx 首先,我根据功能需要创建了几个头文件 ...

  2. Android短信收发(二)

    接收SMS类,代码如下 //for receive SMS private SmsReceiver mSmsReceiver; @Override protected void onResume() ...

  3. STM32F103 GU906B模块GPRS、短信收发、拨号等功能的实现

    这个程序搞了我很久,尤其是对如何提高响应速度上,程序流程很简单,大概就是: 发送AT指令->等待模块响应->一旦响应了,立即返回,并处理掉. 这个程序不一定只能用在GU906上,程序框架在 ...

  4. 基于Socket客户端局域网或广域网内共享同一短信猫收发短信的开发解决方案

    可使同一网络(局域网或广域网)内众多客户端,共享一个短信猫设备短信服务器进行短信收发,短信服务器具备对客户端的管理功能. 下面是某市建设银行采用本短信二次开发平台时实施的系统方案图: 在该方案中,考虑 ...

  5. Android收发短信

    效果:点击发送短信开始发送短信 收到短信时将短信的内容显示出来 代码如下: 一.权限声明 <uses-permission android:name="android.permissi ...

  6. PHP通过串口发短信

    随技术进步,短信收发领域按时间先后产生了三种模式:BLOCK MODE,基于AT指令的TEXT MODE,基于AT指令的PDU MODE.其中,TEXT MODE比较简单,多款诺基亚手机均支持此款模式 ...

  7. Linux-C实现GPRS模块发送短信

    “GSM模块,是将GSM射频芯片.基带处理芯片.存储器.功放器件等集成在一块线路板上,具有独立的操作系统.GSM射频处理.基带处理并提供标准接口的功能模块.GSM模块根据其提供的数据传输速率又可以分为 ...

  8. java 短信猫发送短信的方法

    用java实现短信收发的功能,目前一般项目中短信群发功能的实现方法大致有下面三种: ·                 1. 向运行商申请短信网关,不需要额外的设备,利用运行商提供的API调用程序发送 ...

  9. 发送SMS短信(JSON) 转载

    http://blog.csdn.net/ldl22847/article/details/42553883 public   static string GetMobileConfByUserId( ...

随机推荐

  1. windows下读取Linux分区软件

    导读 ext3等日志型文件系统是Linux中被广泛应用的,通常是许多流行Linux发行版默认的文件系统.etx4也是Linux下的日志型文件系统,被设计作为ext3的继任者.他消除了64位存储限制,是 ...

  2. [AngularJS] Angular1.3 ngAria - 1

    Accessibility is an often overlooked essential feature of a web application. a11y Is a critical comp ...

  3. (剑指Offer)面试题39:判断平衡二叉树

    题目: 输入一课二叉树的根结点,判断该树是不是平衡二叉树.如果二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树. 思路: 1.重复遍历结点 参考上一题求二叉树的深度,先求出根结点 ...

  4. AsyncTask doinbackground onProgressUpdate onCancelled onPostExecute的基本使用

    对于异步操作的原理我就不讲了.在这我着重讲怎么使用异步操作的doinbackground onProgressUpdate onCancelled onPostExecute这四个方法 doinbac ...

  5. OJ帐号保存

    TOJ 614173971 HDU 宇智波佐助 POJ shiai ZOJ henyumen UVa henyumen Light OJ HENYUMEN bzoj henyumen ural 165 ...

  6. fopen /open,read/write和fread/fwrite区别

    fopen /open,read/write和fread/fwrite区别         转载URL:http://hi.baidu.com/%B9%C8%C9%F1%B2%BB%CB%C0jdp/ ...

  7. vcs github gitlab git名词解释

    vcs:version control system git:一个版本管理工具,从git上clone,除了代码,还会把版本信息也给你clone下来. github:一个基于git的代码管理网站,支持公 ...

  8. Oracle 正则 整词匹配 \b 不行

    在oracle中用整词匹配\b来包裹想要匹配的词并不可行, 正确的做法应该是这样: FROM DUAL WHERE REGEXP_LIKE('1 2 3 14','(^|\s|\W)3($|\s|\W ...

  9. linux命令:tail 命令

    tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...

  10. 【CentOS6.5】安装nginx报错:No package nginx available. Error: Nothing to do

    今天在给centos6.5安装nginx时候,提示报错No package nginx available. Error: Nothing to do, 后来百度一下,说缺少EPEL(epel是社区强 ...