USSD stands for Unstrcutured Supplementary Services Data. It is a way of sending short commands from the mobile phone to the GSM network. It uses, like SMS, the signalling channel of the GSM connection. Unlike SMS, it does not use a store and forward architecture, but a session oriented connection. USSD text messages can be up to 182 bytes in length. Messages received on the mobile phone are not stored.
USSD is defined within the GSM standard in the documents:
USSD is most used to make it easy for the (prepaid) mobile user to query his prepaid balance using his mobile phone. It can also be used in mobile payments systems and information services such as weather forecasts and traffic information.
To automate USSD, for instance to check your prepaid balance before submitting an SMS message, can be done using an AT command. All GSM phones and modems that support USSD also support the following command:
The following sample demonstrates how to check your prepaid balance:
AT+CUSD=1,"*101#" +CUSD: 2,"Your current balance is $ 22.10", 0
In this example we are sending the "*101#" USSD command to the network. Within seconds the network responds with a response text and an error code. In this case we have an amount of USD22.10 left on our account. The result code in this example is : '0'.
The following resultcodes exist:
|0||No further user action required|
|1||Further user action required|
|2||USSD terminated by network|
|4||Operation not supported|
The last parameter of the response indicates the DCS (data coding scheme) used in the return text.
The SMS and MMS Toolkit can be used to send USSD data and retrieve the results. Currently only GSM is supported to send and receive USSD data. The sample code below demonstrates how to query an USSD command from Visual Basic using a connected GSM phone or modem.
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long Private Const MAX_PATH = 260 Public objGsm As AXmsCtrl.SmsProtocolGsm Private Function SetDefaultLogFile() Dim Buffer As String Buffer = Space(MAX_PATH) If GetTempPath(MAX_PATH, Buffer) <> 0 Then TextLogfile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "UssdLog.txt" Else TextLogfile.Text = "C:\UssdLog.txt" End If End Function Private Sub CommandSend_Click() Dim strResponse As String Dim strCommand As String Dim strFields MousePointer = vbHourglass strCommand = "AT+CUSD=1," & Chr(34) & TextCommand & Chr(34) & ",15" objGsm.LogFile = TextLogfile.Text ' Set logfile objGsm.Device = ComboDevice.Text ' Set Device If ComboDeviceSpeed.ListIndex = 0 Then objGsm.DeviceSpeed = 0 ' use default speed Else objGsm.DeviceSpeed = ComboDeviceSpeed.List(comboSpeed.ListIndex) End If strResponse = objGsm.SendCommand(strCommand, 10000) ' Send USSD Command If (InStr(strResponse, "OK") > 0) Then ' Response should be OK strResponse = objGsm.SendCommand("", 10000) ' Wait for response If (InStr(strResponse, "+CUSD:") > 0) Then ' If USSD response is received, display text between "" strFields = Split(strResponse, Chr(34)) strResponse = strFields(1) End If End If If (objGsm.LastError <> 0) Then TextResponse.Text = "N/A" TextResult.Text = "ERROR #" & objGsm.LastError & " (" & objGsm.GetErrorDescription(objGsm.LastError) & ")" Else TextResponse.Text = strResponse TextResult.Text = "SUCCESS" End If TextResponse.Text = strResponse MousePointer = vbDefault End Sub Private Sub CommandView_Click() If FileExists(TextLogfile.Text) = True Then Shell "notepad " + TextLogfile.Text, vbNormalFocus End If End Sub Private Sub Form_Load() Dim i, lDeviceCount Set objGsm = CreateObject ("ActiveXperts.SmsProtocolGsm") lDeviceCount = objGsm.GetDeviceCount() ' Get number of devices For i = 0 To lDeviceCount - 1 ComboDevice.AddItem ( objGsm.GetDevice ( i ) ) ' Add devices to list box Next With ComboDevice .AddItem ("COM1") ' Add serial devices .AddItem ("COM2") .AddItem ("COM3") .AddItem ("COM4") .AddItem ("COM5") .AddItem ("COM6") .AddItem ("COM7") .AddItem ("COM8") .ListIndex = 0 End With With ComboDeviceSpeed .AddItem ("Default") ' Setup devicespeed combo .AddItem ("1200") .AddItem ("2400") .AddItem ("9600") .AddItem ("19200") .AddItem ("38400") .AddItem ("57600") .AddItem ("115200") .ListIndex = 0 End With SetDefaultLogFile End Sub Public Function FileExists(sFileName As String) As Boolean FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName)) End Function