0

I am using C# serial port controlling gsm modem. Now in Mikroelectronia USART Terminal after sending:

AT+CUSD=1,"*778#",15

It receives:

AT+CUSD=1,"*778#",15

OK

+CUSD: 0,"Balance: 0.00 TK. Validity: 29-Jul-13. Bonus: 0.00TK. Free Min: 0. Dial *121*2092# for 3 hit songs of Ashiqui-2 as ur Caller Tunetk.10",64

But in C# after sending data

AT+CUSD=1,"*778#",15

it returns:

AT+CUSD=1,"*778#",15

OK

+CUSD: 0,"Balance: 0.00 TK. Validity: 29-Jul-13. Bonus: 0.00TK. Free Min: 0. Dial *121*2092# for 3 hit songs of Ashiqui-2 as ur Caller Tune

That means in C# it does not receive any data after "Caller Tune". Why is that happening? Part of C# code I have used is

        private void Form1_Load(object sender, EventArgs e)
        {
            sp1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        }

        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            var valueOfPort = sp1.ReadExisting();
            textBox1.AppendText(valueOfPort);

        }
 private void button1_Click(object sender, EventArgs e)
        {
            TextBox.CheckForIllegalCrossThreadCalls = false;

            try
            {


                if (!sp1.IsOpen)
                {
                    sp1.Open();
                }
                sp1.Write(textBox2.Text+"\r");

            }
            catch(Exception ex)
            {

                MessageBox.Show(string.Format("Exception : {0}", ex.Message), "Port Error");
            }
        }
rakib
  • 131
  • 3
  • 10

2 Answers2

3

the TextBox.CheckForIllegalCrossThreadCalls = false; makes me very suspicious, I think that is what is breaking your program, simply just invoke to update the text box properly and I think it will work. Just change the event code to the following:

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    if(textbox1.InvokeRequired)
    {
        textbox1.Invoke(new SerialDataReceivedEventHandler(sp_DataReceived), sender, e);
    }
    else
    {
        var valueOfPort = sp1.ReadExisting();
        textBox1.AppendText(valueOfPort);
    }
}

What this will do is check if you are running on the correct thread, if not it restarts the function again on the UI thread with the same arguments. Also be sure to remove the TextBox.CheckForIllegalCrossThreadCalls = false;


UPDATE: After re-reading the MSDN I found this remark

This method returns the contents of the stream and internal buffer of the SerialPort object as a string. This method does not use a time-out. Note that this method can leave trailing lead bytes in the internal buffer, which makes the BytesToRead value greater than zero.

Try checking to see if there is more data to read after calling ReadExisting.

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    if(textbox1.InvokeRequired)
    {
        textbox1.Invoke(new SerialDataReceivedEventHandler(sp_DataReceived), sender, e);
    }
    else
    {
        while(sp1.BytesToRead > 0)
        {
            var valueOfPort = sp1.ReadExisting();
            textBox1.AppendText(valueOfPort);
        }
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
1

Have you defined your device's EOL and set it correctly in your program? C# serial port does not necessarily fire up DataReceive at the start of the message nor does it know where the message ends. It may terminate anywhere in the message. You should try explicitly set the EOL which can be uniquely identified by the port in order for it to buffer incoming message and to return it once it is completed.

// e.g. I normally used (carriage return + newline) instead of just either one
sp1.NewLine = "\r\n";
KMC
  • 19,548
  • 58
  • 164
  • 253
  • \r\n is just an example. I once had a hardware that by default used \n to distinct messages, which create problems because the message content itself also contain other \n. (Image if Caller Tunetk.10",64 is actually Caller Tune\ntk.10",64, the EOL won't work properly). I then configured the hardware to use \r\n as its EOL because it is highly unlikely to have this within the content of the message. You have to configured your hardware before setting the same EOL in your program. – KMC Jul 22 '13 at 01:52
  • I think i have figured out the problem....I have analysed the received data in hex value and i saw after the "Caller Tune" it sends a 0x00 value in hex..thank you for your time – rakib Jul 22 '13 at 02:26
  • 1
    @rakib You should post your answer and mark it as accepted, otherwise this question will keep getting promoted to the front page as a question with no accepted answer. – Scott Chamberlain Jul 22 '13 at 02:45