Cellular Radio Module - ArgumentOutOfRangeException

I have problems with the Cellular Radio Module.
When an sms arrives, I get an ‘System.ArgumentOutOfRangeException’.
Very rarely, it’s successfull. But when I’ve got the exception then the program is completely broken.

The reason in code:

while (serialLine.BytesToRead > 0)
{
response += (char) serialLine.ReadByte();
}

This loop is left, even if still data is coming in. Probably a very small break in the serial communication?

Loader (TinyBooter) Version: 4.2.3.3
Firmware (TinyCLR) Version: 4.2.5.0

Other versions that I can check?
And how can I solve this?

Best regards,
Stijn.

it’s the parsing later on that throws the exception.

                    string cmd = "+CMGR:";
                    first = response.IndexOf(cmd) + cmd.Length;
                    int mid = response.IndexOf("\n", first);
                    last = response.IndexOf("OK", mid);

but the initial problem is that not everything is read from the serial buffer

then I’ll loose my sms information…
it’s clear in the received buffer that it’s not complete.

if I just catch the exception and continue, I’ll loose information

There is no exception… It’s the parsing of the received response.

string cmd = “+CMGR:”;
first = response.IndexOf(cmd) + cmd.Length;
int mid = response.IndexOf("\n", first);
last = response.IndexOf(“OK”, mid);

Invalid response results that:
=> mid = - 1

=> last = response.IndexOf(“OK”, mid);
=> this line results in ArgumentOutOfRangeException because mid = -1

You need to check that you have the full message and then start parsing.

Any idea how you can check such a message?

If you have distinctive message separator then you accumulate the content until you get that separator. Or if your message has fixed size you can check the size.

Difficult for an sms message :wink:

The problem in the parsing is caused by a not complete response…
So if the response is ok, then the parsing is ok.

Problem is that the response is missing some data at the end. That makes parse failing.

No, here is the problem: serialLine.BytesToRead should be zero at a certain moment so that this loop stops.

I don’t know if this is a hardware or software issue?
Maybe caused by performance of VS2012? But also running in deployment, it seems the same issue.

public CellularRadio(int socketNumber)
{
socket = GT.Socket.GetSocket(socketNumber, true, this, null);
pwrkey = new GTI.DigitalOutput(socket, GT.Socket.Pin.Three, false, this);
serialLine = new GTI.Serial(socket, 19200, GTI.Serial.SerialParity.None, GTI.Serial.SerialStopBits.One, 8,
GTI.Serial.HardwareFlowControl.Required, this);

        serialLine.Open();
        serialLine.Write("AT");
        Thread.Sleep(1000);
        String response = "";
        while (serialLine.BytesToRead > 0)
        {
            response += (char) serialLine.ReadByte();
        }
        DebugPrint("RESP: " + response);
        if (response.Length != 0)
        {
            DebugPrint("Sending off pulse");
            pwrkey.Write(true);
            Thread.Sleep(1200);
            pwrkey.Write(false);
            Thread.Sleep(500);
        }
        isPowerOn = false;

        readerThread = new Thread(new ThreadStart(run));
        readerThread.Start();
    }

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)
        public CellularRadio(int socketNumber)
        {
            socket = GT.Socket.GetSocket(socketNumber, true, this, null);
            pwrkey = new GTI.DigitalOutput(socket, GT.Socket.Pin.Three, false, this);
            serialLine = new GTI.Serial(socket, 19200, GTI.Serial.SerialParity.None, GTI.Serial.SerialStopBits.One, 8,
                                        GTI.Serial.HardwareFlowControl.Required, this);

            serialLine.Open();
            serialLine.Write("AT");
            Thread.Sleep(1000);
            String response = "";
            while (serialLine.BytesToRead > 0)
            {
                response += (char) serialLine.ReadByte();
            }
            DebugPrint("RESP: " + response);
            if (response.Length != 0)
            {
                DebugPrint("Sending off pulse");
                pwrkey.Write(true);
                Thread.Sleep(1200);
                pwrkey.Write(false);
                Thread.Sleep(500);
            }
            isPowerOn = false;

            readerThread = new Thread(new ThreadStart(run));
            readerThread.Start();
        }

You realize that this is the constructor of the CellularRadio object, and that putting that loop in there is pretty much going to fail - if you look lower down in the code you quoted, it creates a new thread that’s job is to read the input.

Can you take a step back and tell us what you’re trying to do, and how you went about it?

I had that problem also. I added the driver source directly into my project so I could change things on the fly. Here is my revision that seemed to “fix” this problem:


 #region Check Sms Received (CMTI)

			if (response.IndexOf("+CMTI:") > 0)
			{
                try
				{
				    string cmd = "+CMTI:";
				    first = response.IndexOf(cmd) + cmd.Length;
				    last = response.IndexOf("\n", first);
				    reply = (response.Substring(first, last - first)).Trim();
				    char[] sep = { ',' };
				    string[] split = reply.Split(sep);

/*				try
				{
 */
					int position = int.Parse(split[1]);
					newMessages.Enqueue(position);
					RetrieveSms(position, false);
				}
				catch (Exception)
				{
					DebugPrint("ERROR");
				}
			}

@ logictechs - But then you can loose sms information?

If this happens during receiving, the information will be lost.
I want to use this combined with a relay: if sms received with certain contact, activate relais.
So it must be without any error (in normal condition).

How can we find the author of this code?
And who maintains the ‘GTM.Seeed’ library?

@ Brett - this is not my code, but the code from ‘GTM.Seeed.CellularRadio’…

Hmm, you are bumping into the usual pitfalls and roadblocks. I wish I had some more time to coordinate a serious overhaul of this driver. I actually think the module is ok, but the driver is having serious issues. Thanks logitechs for helping Stijn.

Also check out Byron’s driver and the thread here https://www.ghielectronics.com/community/forum/topic?id=9414