关键代码如下
enum STATUS {UssdStart=1, UssdReply, UssdRetCode0, UssdRetCode1, UssdRetCode2, UssdInteraction, UssdDisp, UssdSend, UssdEnd, UssdTimeout };
int main(int argc, char** argv)
{
char UssdNO[50]={0};
char InteractBuff[50]={0};
char outbuff[1024]={0};
enum STATUS Status = UssdStart;
int fd = open("/dev/ttyS0",O_RDWR | O_NONBLOCK);
if(fd == -1)
{
printf("open ttyS0 error!\n");
}
else
{
SetSpeed(fd, 115200);
if (SetParity(fd,8,1,'N') == FALSE) {
printf("Set Parity Error\n");
}
SetRawMode(fd);
}
WriteToSIMCOM(fd, "ATE0\r");
if(ReadSIMCOMLine(fd, outbuff, 5, 0) == 1)
{
if(strstr(outbuff, "OK"))
{
}
}
else
Status = UssdTimeout;
while(1)
{
switch(Status)
{
case UssdStart:
printf("Please enter the USSD number:\n");
gets(UssdNO);
WriteToSIMCOM(fd, PackUSSDStr(UssdNO));
if(ReadSIMCOMLine(fd, outbuff, 15, 0) == 1)
{
if(strstr(outbuff, "OK"))
{
//printf("outbuff:%s\n", outbuff);
Status++;
}
}
else
Status = UssdTimeout;
break;
case UssdReply:
if(ReadSIMCOMLine(fd, outbuff, 5, 0) == 1)
{
if(strstr(outbuff, "+CUSD: "))
{
char UssdRetCode = GetUssdRetStatus(outbuff);
//printf("UssdReply:UssdRetCode:%c\n", UssdRetCode);
if(UssdRetCode == '0')
Status = UssdRetCode0;
else if(UssdRetCode == '1')
Status = UssdRetCode1;
else if(UssdRetCode == '2')
Status = UssdRetCode2;
else
Status = UssdEnd;
}
}
else
Status = UssdTimeout;
break;
case UssdRetCode0:
printf("\n%s\n", UnPackUSSDStr(outbuff));
Status = UssdEnd;
break;
case UssdRetCode1:
Status = UssdDisp;
break;
case UssdRetCode2://need to ATD
WriteToSIMCOM(fd, PackATDStr(UssdNO));
if(ReadSIMCOMLine(fd, outbuff, 15, 0) == 1)
{
if(strstr(outbuff, "NO CARRIER"))
{
//printf("outbuff:%s\n", outbuff);
Status = UssdInteraction;
}
}
else
Status = UssdTimeout;
break;
case UssdInteraction:
if(ReadSIMCOMLine(fd, outbuff, 5, 0) == 1)
{
if(strstr(outbuff, "+CUSD: "))
{
char UssdRetCode = GetUssdRetStatus(outbuff);
//printf("UssdInteraction:UssdRetCode:%c\n", UssdRetCode);
if(UssdRetCode == '0')
Status = UssdRetCode0;
else if(UssdRetCode == '1')
Status = UssdDisp;
else if(UssdRetCode == '2')
Status = UssdRetCode2;
else
Status = UssdEnd;
}
}
else
Status = UssdTimeout;
break;
case UssdDisp:
printf("\n%s\n", UnPackUSSDStr(outbuff));
Status++;
break;
case UssdSend:
printf(">");
memset(InteractBuff, 0, sizeof(InteractBuff));
gets(InteractBuff);
WriteToSIMCOM(fd, PackUSSDStr(InteractBuff));
if(ReadSIMCOMLine(fd, outbuff, 5, 0) == 1)
{
if(strstr(outbuff, "OK"))
{
//printf("outbuff:%s\n", outbuff);
Status = UssdInteraction;
}
}
else
Status = UssdTimeout;
break;
case UssdEnd:
printf("Ussd finished!\n");
exit(0);
break;
case UssdTimeout:
printf("Ussd receive timeout!\n");
exit(0);
break;
}
}
return 0;
}
目前存在的疑问
1、不知道如何中断当前的USSD连接,ATH试过是不行的
2、返回+CUSD: 0 和 +CUSD: 2时不知道如何做通用的处理