-1

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#

Community
  • 1
  • 1
aminvincent
  • 553
  • 1
  • 12
  • 43

1 Answers1

1

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);
    }
}
Alex
  • 13,024
  • 33
  • 62
  • so it means that i must add some code (method port_DataReceived) in my project ? and adding `port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);` in btnCheck? – aminvincent Apr 27 '15 at 03:29
  • @aminvincent yes that is the idea. the `port_DataReceived` method can be a private method in your form. – Alex Apr 27 '15 at 03:30
  • thanks for your suggest,..when i run my project and click btnCheck disappear anything..... can u help me to fix my code above? – aminvincent Apr 27 '15 at 03:43
  • @aminvincent I am sorry, I don't understand your question. What do you mean by "when I click btnCheck disappear anything" ? – Alex Apr 27 '15 at 03:55
  • i have tried again for code above,..absolutely work...but the result just appear OK .....i don't know how to fix this... – aminvincent Apr 27 '15 at 09:05
  • @aminvincent I still don't quite understand what you mean. But I did find another problem with the code. I updated my answer to explain how you can solve it. – Alex Apr 27 '15 at 15:13
  • i have tried again code suggest from u above,..but it work fine. just my problem didn't solve,...when i click in btnCheck it always apper result in txtMessage = OK ...when i try ussd command AT+CUSD=1,\"*388#\",15 in hyperterminal it work fine bro,...i don't know.. this image result of my form https://www.dropbox.com/s/u73olhdnsebrqfc/result.png – aminvincent Apr 28 '15 at 06:30
  • @aminvincent, well I cannot debug your serial communication for you. You may need to check handshaking settings, ...., I can't know what is needed for your specific use case. I just provided a solution for you to display the serial communication response in a textbox, like you asked in your question. – Alex May 01 '15 at 00:29
  • ,..your suggest really work in my project to display it in my serial communication response in a textbox,...thanks....so finally i just resolve my code to fix how to serial communication work... – aminvincent May 02 '15 at 01:46