|
情况是这样的:
我一个在linux下跑的C程序 主要是完成GSM发短信的功能~~~
短信短一点的情况 可以成功发出去“HELLO WORLD”这样~~
长一点 就发布过去“fwefjweifjweofjwoiejfioewfjwoejfweo”就发不出去了~
但是我在minicom 下是可以发送出去很长的字符串的~~[/color]我的代码如下
#include
#include
#include
#include
#include
#include
#include "fcntl.h"
int init_device(int portfd)
{
struct termios tty;
tcgetattr(portfd,&tty); //get the info
//串口参数:115200bps,8N1
cfsetospeed(&tty,B115200); //set out speed
cfsetispeed(&tty,B115200); //set in speed = 115200
tty.c_cflag &= ~CSIZE; //
tty.c_cflag |= CS8; // 控制模式:设置8
tty.c_lflag &= ~(ICANON| ECHO| ECHOE); //
tty.c_cflag |= CREAD;//
tty.c_cflag &= ~(PARENB | CSTOPB);//
tty.c_iflag |= IXON; //
tty.c_iflag |= IXOFF; //
tty.c_iflag |= IXANY;
tty.c_cflag &= ~CRTSCTS; //
tty.c_cc[VMIN] = 0; //只要读到数据,则read
tty.c_cc[VTIME] = 10; //若定时器超期,却未读到数据,返回0
tcsetattr(portfd, TCSANOW,&tty);
return 0;
}
void Set_GSM(int portfd, int mod )//缺省模式是text
{
int len;
int n;
char cmd[255];
//
tcflush(portfd, TCIOFLUSH);
sprintf(cmd,"AT+CMGF=%d\r",mod);
write(portfd,cmd,strlen(cmd));
sleep(2); //
n=read(portfd,cmd,255);//
cmd[n]='\0';
printf("receive....: %s",cmd);
//设置GSM
tcflush(portfd,TCIOFLUSH);
//sprintf(cmd,"AT+CSCS= USC2\r");
sprintf(cmd,"AT+CSCS= GSM\r");
write(portfd,cmd,strlen(cmd));
sleep(2); //
n=read(portfd,cmd,255);//
cmd[n]='\0';
printf("receive: %s",cmd);
//
tcflush(portfd,TCIOFLUSH);
sprintf(cmd,"AT+CSMP=17,168,0,0\r");
write(portfd,cmd,strlen(cmd));
sleep(2); //
n=read(portfd,cmd,255);//
cmd[n]='\0';
printf("receive: %s",cmd);
//
tcflush(portfd,TCIOFLUSH);
sprintf(cmd,"AT+CNMI=2,1,0,0,1\r");
write(portfd,cmd,strlen(cmd));
sleep(2); //
n=read(portfd,cmd,255);//
cmd[n]='\0';
printf("receive: %s",cmd);
//
//tcflush(portfd,TCIOFLUSH);
//sprintf(cmd,"AT+CSMP=17,168,0,0\r");
//write(portfd,cmd,strlen(cmd));
//sleep(2); //
//n=read(portfd,cmd,255);//
//cmd[n]='\0';
//printf("receive: %s",cmd);
// return 1;
}
int Send_TextMod(int portfd, char phone[40], char message[255])
{
printf("phone = %s, portfd= %d, msg = %s\n\n",phone,portfd,message);
int len, n;
char cmd[255],msg[114];
char ctrlZ=26;
int mod = 1;
//
tcflush(portfd,TCIOFLUSH);
sprintf(cmd,"AT+CMGS =%s\r",phone);
write(portfd,cmd,strlen(cmd));
sleep(2);
n=read(portfd,cmd,255);//
cmd[n]='\0';
printf("receive: %s\n",cmd);
//发送msg
strcat(message,"\x01a");
write(portfd,message,strlen(message));
sleep(5);
n=read(portfd,cmd,255);//
cmd[n]='\0';
printf("receive: %s",cmd);
close(portfd);
return 1;
}
int main(int argc, char ** argv)
{
char devttyS[40];
char phone[40];
char message[255];
int portfd;
if(argc<4)
{
printf("usage: %s phone_num, dev_path, message...\n",argv[0]);
return -1;
}
else
{
strcpy(phone,argv[1]);
strcpy(devttyS,argv[2]);
strcpy(message,argv[3]);
}
message[strlen(message)] = '\0';
//
portfd = open(devttyS,O_RDWR);
printf("串口id :portfd = %d\n",portfd);
init_device(portfd); //
Set_GSM(portfd,1);
Send_TextMod(portfd,phone,message);
return 0;
}
|
|