#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. /etc/ssh/sshd_config 配置(ssh连接)

    # Package generated configuration file# See the sshd_config(5) manpage for details # What ports, IPs ...

  2. [Functional Programming] Monad

    Before we introduce what is Monad, first let's recap what is a pointed functor: A pointed functor is ...

  3. 总想自己动动手系列·3·微信公众号和外网服务交互之通过TOKEN验证(准备篇·1)

    一.准备工作 (1)准备一个微信公众号(对私的订阅号或者对公的服务号). (2)准备一台部署了web应用,并且已经发布出去的Linux服务器(需要说明的是:微信公众号强烈建议使用80端口,使用其他自定 ...

  4. Minimum edit distance(levenshtein distance)(最小编辑距离)初探

    最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...

  5. 浅析JDK中ServiceLoader的源码

    前提 紧接着上一篇<通过源码浅析JDK中的资源加载>,ServiceLoader是SPI(Service Provider Interface)中的服务类加载的核心类,也就是,这篇文章先介 ...

  6. 将图片转成base64字符串并在JSP页面显示的Java代码

    *本事例主要讲了如下几点:  * 1:将图片转换为BASE64加密字符串.  * 2:将图片流转换为BASE64加密字符串.  * 3:将BASE64加密字符串转换为图片. * 4:在jsp文件中以引 ...

  7. Flume入门:安装、部署

    一.什么是Flume? flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用.Flume 初始的发行版本目前被统称为 Flume OG(original genera ...

  8. 【laravel5.4】DB::table的操作

    基于laravel5.4版本的查询构造器的简单几个操作:(相对于TP3.2版本) //获取指定多行多列,二维,,对象 [select] $names = ''; $names = DB::table( ...

  9. 【laravel54】win系下本地域名配置(解决403错误)

    1.apache虚拟目录地址应该指向\laravel54\public 目录下:(TP5类似) <VirtualHost *:> DocumentRoot "D:\phpStud ...

  10. .Net Excel 导出图表Demo(柱状图,多标签页) .net工具类 分享一个简单的随机分红包的实现方式

    .Net Excel 导出图表Demo(柱状图,多标签页) 1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表) 2 Epplus 的安装和引用 新建一 ...