最近在用ARM+GPRS做收发短信系统,但总是没反应,短信发不了!困惑很久,请路过的兄台多多指点,帮我看看程序有何错误!谢谢!谢谢!
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
static void Error(const char *Msg)
{
fprintf (stderr, "%s\n", Msg);
fprintf (stderr, "strerror() is %s\n", strerror(errno));
exit(1);
}
/***********初始化串口******************/
int init_Com(int portfd)
{
struct termios tty1;
tcgetattr(portfd,&tty1);
cfsetospeed(&tty1,B115200);
cfsetispeed(&tty1,B115200);
tty1.c_cflag &=~CSIZE;
tty1.c_cflag |=CS8;
tty1.c_cflag |=CREAD | CLOCAL;
tty1.c_cflag &=~(PARENB | CSTOPB);
tty1.c_iflag |=IXON;
tty1.c_iflag |=IXOFF;
tty1.c_iflag |=IXANY;
tty1.c_lflag &=~(ICANON | ECHO | ECHOE);
tty1.c_cflag &=~CRTSCTS;
tty1.c_cc[VMIN]=0;
tty1.c_cc[VTIME]=10;
tcsetattr(portfd,TCSANOW,&tty1);
return 0;
}
/****************构造PDU****************************/
int build_pdu(const char *phone,const unsigned char *msg ,unsigned char *pdu)
{
unsigned char pdu_phone[128],temp_phone[128],*buf="6D4B8BD5",pdu_msg[255];
int i,n;
sprintf(pdu,"001100");
sprintf(temp_phone,"%sF",phone);
for(i=0;i
{
pdu_phone=temp_phone[i+1];
pdu_phone[i+1]=temp_phone;
}
strcat(pdu,"0D91");
strcat(pdu,pdu_phone);
strcat(pdu,"0008AA");
n=code_convert("GB2312","UNICODE",msg,buf);
sprintf(pdu_msg,"%.2X",n);
strcat(pdu,pdu_msg);
for(i=0;i
{
sprintf(pdu_msg+i*2,"%.2X",buf[i+3]);
sprintf(pdu_msg+i*2+2,"%.2X",buf[i+2]);
}
strcat(pdu,pdu_msg);
return strlen(pdu)/2-1;
}
/******************字码转换****************************/
int code_convert(const char *srccode,const char *dstcode,const unsigned char *inbuf,unsigned char *outbuf)
{
int n=0;
unsigned int inbytesleft=strlen(inbuf);
unsigned int outbytesleft=255;
iconv_t cd_d=iconv_open(dstcode,srccode);
iconv(cd_d,&inbuf,&inbytesleft,&outbuf,&outbytesleft);
iconv_close(cd_d);
return 255-outbytesleft;
}
int main()
{
int portfd,len;
char cmd[255],msg[255],pdu[255];
char ctrlZ=26;
portfd=open("/dev/ttySAC1",O_RDWR);
if (portfd < 0)
Error("Unable to open device");
init_Com(portfd);
sprintf(cmd,"AT+CMGF=0\r");
sleep(1);
write(portfd,cmd,strlen(cmd));
sleep(1);
len=build_pdu("8613295887813","测试",pdu);
sprintf(cmd,"AT+CMGS=%d\r",len);
write(portfd,cmd,strlen(cmd));
sleep(1);
write(portfd,pdu,strlen(pdu));
write(portfd,ctrlZ,1);
sleep(1);
close(portfd);
}
GPRS:
华为 GTM900-B详细参数 基本功能
GSM/GPRS Phase2/2+
GSM 03.40 短信业务
GPRS CLASS 10 数据业务
主要特点
频段 800/900/1800MHz/850/1900MHz
接受灵敏度:106dBm
尺寸 56.5mm*36mm*3.0mm
工作温度范围 -30~+60摄氏度
集群通信业务
高速数据业务
脱网集群通信业务
主要功能
支持 Group3,class2 传真业务
电路交换业务(9.6kbps,14.4kpbs)
分组交换数据业务(64.4kpbs)
FR/HR/EFR/AMR 语音编码
支持 TCP/IP
呼叫转移、等待、保持、呼叫前转
多方通话、来电显示、可选铃声
接口特性
V.25 AT 命令集
40-pin zip 连接器
RS232 双工串口
红外串口接口
SIM 3.0V 和 1.8V 接口
2路模拟音频输入输出接口
电源输入接口和充电管理
ADC 输入