Search code examples
c#winformsserial-portussd

type ussd command and display result in textbox using c#


I have a Windows Form in my project. This form contains 3 controls: txtUss, btnCheck and txtMessage. The scenario is that after a "ussd" command has been entered in the txtUss text box and the btnCheck button has been pressed, the result will displayed in txtMessage.

here is the detail of my winform https://www.dropbox.com/s/2lo8ci3rcoznvlh/ussd.PNG

btnCheck Code :

private void btnCheck_Click(object sender, EventArgs e)
{
    try
    {
        SerialPort port = new SerialPort();

        port.BaudRate = 115200;
        port.PortName = "COM3";
        port.Timeout = 300;

        port.Open();
        port.Write("AT+CUSD=1," + txtUssd.Text + ",15");

        txtMessage.text = ; // <<< here is the result. 
        // but i dont know how to refer port.Write("AT+CUSD=1," + txtUssd.Text + ",15");
        port.Close();               
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

I don't know how to get the result in the txtMessage text box.

Any one can suggest me or give some example...?

if you wanna get update solve for this question and topic checking balance in modem, follow this link : Check balance using USSD Command in C#


Solution

  • If you want to receive the response from the COM port, you will need to add an event handler to your code, and register it with the COM port's DataReceived event, so that you can read the response when data is received, and display it in your txtMessage text box.

    Because the event will be sent on a different thread, and it is not allowed to update the UI controls from a thread that did not create them, the update will take two steps, as shown in the method below that you will need to add to your form:

    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            // read the response.
            var response = ((SerialPort)sender).ReadLine();
    
            // Need to update the txtMessage on the UI thread.
            this.Invoke(new Action(() => txtMessage.Text = response));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    In your btnCheck_Click method, after the line port.Open(); you need to add the following line, to register the event handler:

    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    

    Another problem appears to be that you close the COM port immediately, possibly before the response is received. So I suggest you do the following:

    1. Make port a member variable of your Form class (name it _port).

      private SerialPort _port
    2. Create and open the COM port when your form is created:

      _port = new SerialPort();
      _port.BaudRate = 115200;
      _port.PortName = "COM3";
      _port.Timeout = 300;
      
      _port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
      _port.Open();
      
    3. Close and dispose the COM port when your form is closed.

      _port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);        
      _port.Close();
      _port.Dispose();
      

    Only keep this in your button click event handler:

    private void btnCheck_Click(object sender, EventArgs e)
    {
        try
        {
            _port.Write("AT+CUSD=1," + txtUssd.Text + ",15");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }