SIM 900のUSSDテストウィジェット

3642 ワード

 
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!
"); } else { SetSpeed(fd, 115200); if (SetParity(fd,8,1,'N') == FALSE) { printf("Set Parity Error
"); } 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:
"); gets(UssdNO); WriteToSIMCOM(fd, PackUSSDStr(UssdNO)); if(ReadSIMCOMLine(fd, outbuff, 15, 0) == 1) { if(strstr(outbuff, "OK")) { //printf("outbuff:%s
", 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
", 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("
%s
", 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
", 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
", 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("
%s
", 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
", outbuff); Status = UssdInteraction; } } else Status = UssdTimeout; break; case UssdEnd: printf("Ussd finished!
"); exit(0); break; case UssdTimeout: printf("Ussd receive timeout!
"); exit(0); break; } } return 0; } 1、 USSD ,ATH 2、 +CUSD: 0 +CUSD: 2