Problem with StreamReader.ReadLine()

i have been able to send AT commands to my gsm modem and got their response using streams..

my problem now is when i issue the AT+CMGS="<destination number>" command my app hangs.. it is as if it does not end reading the stream.. i dont know what's the problem with this one. i run it before in .NET 2003 and it works fine. when i upgraded to .NET 2005 beta, thats the time when i started encountering this problem..

can anybody help me with this? thank u so much!
[494 byte] By [byang] at [2007-11-19 10:51:42]
# 1 Re: Problem with StreamReader.ReadLine()
You might need to call Flush() on the stream.

Anyway, the reason why .NET 2.0 is BETA is because there are still bugs in it...

Darwen.
darwen at 2007-11-9 1:49:38 >
# 2 Re: Problem with StreamReader.ReadLine()
dont use a stream reader to read from a network stream.

reason being that stream reader is a wrapper for reading sequential streams. network streams are not guarenteed to be sequential or complete. as packets are recieved they are placed into the stream buffer. the stream reader will look for newline terminated blocks when it does a readline, which is why it might hang.

I've seen the most success with reading network streams by hand:

Socket s = new Socket(...);

byte[] buffer = new byte[1024];
string message = "";
int count = 0;
do {
count = s.Recieve(buffer, 0, 1024);
message += Encoding.Utf8.GetString(buffer, 0, count);
}while(count == 1024);

recieve blocks until packets are recieved. otherwise the stream buffer are all null bytes. you could read continually but thats not guarenteed to work. since there's no newline chars readline will hang (indefinately or until a thread abort from what I've seen).

most likely the tolerances used between the 2 versions have changed (I've seen similar things happen between 1.1 and 1.1 sp1).

try to report it to microsoft (http://lab.msdn.microsoft.com/productfeedback/) anyway.
MadHatter at 2007-11-9 1:50:38 >
# 3 Re: Problem with StreamReader.ReadLine()
Anyway, the reason why .NET 2.0 is BETA is because there are still bugs in it...

Darwen.

Well shucks! v1.1 still has bugs too.
zips at 2007-11-9 1:51:39 >
# 4 Re: Problem with StreamReader.ReadLine()
well i read that problem with Streams in dealing networks however im into serial port programming and not into socket programming.. and i think there is a difference between the two. by the way, im using IntPtr handle and DLL imports [kernel.dll] in dealing with com ports.

here is my code in reading response from a modem..

public unsafe ArrayList ReadResponseFromModem()
{
string line;
ArrayList str = new ArrayList();

try
{
Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle(portHandle, false);
System.IO.FileStream fs = new System.IO.FileStream(safeFileHandle, FileAccess.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(fs);

fs.Flush();

if (fs.CanRead)
{
try
{
int i;
while ((i = sr.Peek()) >= 0)
{
line = sr.ReadLine();
if (!line.Equals(""))
{
str.Add(line);
}
}
}
catch (OperationCanceledException operationCanceledException)
{
Console.WriteLine(operationCanceledException.Message);
}
}
else
{
Console.WriteLine("Cannot read from port");
}

sr.Close();
fs.Close();
}
catch (IOException io)
{
Console.WriteLine(io.Message + "\nERROR CODE: " + GetLastError());
}
return str;
}

again, my problem arises only when i issue the AT+CMGS="<destination number>" command. other AT commands work fine and i got my expected output.

added to that.. i think the problem also is with the response of AT+CMGS="<destination number>" command. it seems that the streamreader.readline() did not encounter an end of line or maybe an end of file(? not sure) because i tried issuing the command in the hyperterminal and the modem's response is '>' and it waits for user input, which in this case is the message...

i have been trying to research and find out more regarding this issue however i haven't found any solutions to this. i badly needed ur suggestions and idea regarding this problem... thank u so much!
byang at 2007-11-9 1:52:44 >