Hi! I'm trying to run a few AT commands on a GSM modem. I'm doing this in C#.Net 2005.
When I run a few commands one after the other, the response generation takes a long time. The response for the 1st command is returned as blank, i.e nothing. And its actual response is returned as the response to the 3rd command.
That is, due to the delayed response generation, the responses are being over-written. When the program ends, the responses to the last 2 or 3 commands are simply lost.
Please help.
Amey
10 4560 horace1 1,510
Recognized Expert Top Contributor
after you send an AT command cannot you wait for a response such as OK or a message from the modem, then send the next command?
Banfa 9,065
Recognized Expert Moderator Expert
Personally I would go further than horace1 and say that you really should wait for the response from the first command before you send the second. That is part of what proper communications handshaking is about, you wait to see that 1 message is successful before sending the next, unless the protocol is specified as allowing you to send a more data with the reply to the initial data (like TCP/IP).
You do not know what the modem is doing (or the quality of the software it is running) your command may have caused network action, sending a second command while 1 is already in progress may be causing the modem handling problems or you may have requested that it does something that it can't do until the first command has completed.
Personally I would go further than horace1 and say that you really should wait for the response from the first command before you send the second. That is part of what proper communications handshaking is about, you wait to see that 1 message is successful before sending the next, unless the protocol is specified as allowing you to send a more data with the reply to the initial data (like TCP/IP).
You do not know what the modem is doing (or the quality of the software it is running) your command may have caused network action, sending a second command while 1 is already in progress may be causing the modem handling problems or you may have requested that it does something that it can't do until the first command has completed.
Re:
Thank u for taking interest in my problem. But the real problem lies here.
I'm going exactly the way u said, one command at a time, waiting for its reply before going for the 2nd command. But I don't have any means of computing the time it takes for the response to be generated. For the time being I'm using a delay of abt 7 secs after each command, hoping the required response will b generated within this time. But even after waiting for 7 secs, the responses are being over-written.These are some basic modem query commands. This is how I get their responses :
INFO - AT+GMM :
INFO - AT+FCLASS=? : Nokia N70
INFO - ATI1 : OK
INFO - ATI2 :
INFO - ATI3 : 0
INFO - ATI4 : OK
The "Nokia N70" is actually the output generated by the 1st command. The "0" is actually the output generated by the command At+FCLASS=?. But it comes as the output to the 4th command. The outputs to the last 3 commands are simply lost as the application terminates after running the last command and waiting for its response for 7 seconds. This will cause serious problems when I will send 100 messages at a time.
Please help.
Amey
Banfa 9,065
Recognized Expert Moderator Expert
Here is what v.250 has to say about executing commands and receiving replies
5.6 Executing commands
Upon receipt of the termination character, the DCE shall commence execution of the commands in
the command line in the order received from the DTE. Should execution of a command result in an
error, or a character be not recognized as a valid command, execution is terminated, the remainder of
the command line is ignored, and the ERROR result code is issued. Otherwise, if all commands
execute correctly, only the result code associated with the last command shall be issued; result codes
for preceding commands are suppressed. If no commands appear in the command line, the OK result
code is issued.
ITU v250 is the standard specifying the AT command set and operation for a normal or GSM modem/telephone available here. Additionally 3GPP 27.007 may be useful which is the specification of the extra commands used by a GPRS/3G modem/telephone available here (if you want it just get the latest version).
Anyway from the quote you can see that the modem you are using is not conforming to specification (it is not surpressing results from previous commands). This is not surprising I do not think I have ever used a modem that conformed to specification, apparently the modem manufacturers do not seem to think it is important which is why it is so hard to write modem drivers.
7 seconds does sound rather long for a response to a AT+GMM (I know the modems I have worked on responded more quickly) however we don't know what the modem is doing or how much load the modems processor is under. I would recommend a timeout of 30 seconds, 90% of the commands (and 99% of the commands you are likely to use) should respond in this time.
You can afford a long timeout, remember you should only hit it if there is an error of some sort so in normal operation it should not effect speed. However have you also considered the possibility of an error in the code you are using to send/receive data as the cause of the problem?
Here is what v.250 has to say about executing commands and receiving replies
ITU v250 is the standard specifying the AT command set and operation for a normal or GSM modem/telephone available here. Additionally 3GPP 27.007 may be useful which is the specification of the extra commands used by a GPRS/3G modem/telephone available here (if you want it just get the latest version).
Anyway from the quote you can see that the modem you are using is not conforming to specification (it is not surpressing results from previous commands). This is not surprising I do not think I have ever used a modem that conformed to specification, apparently the modem manufacturers do not seem to think it is important which is why it is so hard to write modem drivers.
7 seconds does sound rather long for a response to a AT+GMM (I know the modems I have worked on responded more quickly) however we don't know what the modem is doing or how much load the modems processor is under. I would recommend a timeout of 30 seconds, 90% of the commands (and 99% of the commands you are likely to use) should respond in this time.
You can afford a long timeout, remember you should only hit it if there is an error of some sort so in normal operation it should not effect speed. However have you also considered the possibility of an error in the code you are using to send/receive data as the cause of the problem?
Thanks a lot for replying.
I'm writing all the data being received by th Serial Port instance(ComPor t) to a string, and printing it.
ComPort.DataRec eived += DataRecievedHan dler;
.
.
.
private static void DataRecievedHan dler(object sender, SerialDataRecei vedEventArgs args)
{
data = ComPort.ReadLin e();
//Console.WriteLi ne("Response : " + data + Environment.New Line);
}
Please tell me if I'm wrong, or if there's any other way to do this.
Amey
Banfa 9,065
Recognized Expert Moderator Expert
I unfortunately do not know C# but I see no obvious error in that and I wouldn't expect there to be. However what is causing DataRecievedHan dler to be called?
In my (10 year) experience of Windows there is something wrong in its heart that causes it to not always notify the application of received data as expected.
I unfortunately do not know C# but I see no obvious error in that and I wouldn't expect there to be. However what is causing DataRecievedHan dler to be called?
In my (10 year) experience of Windows there is something wrong in its heart that causes it to not always notify the application of received data as expected.
No luck. Even after 30 secs of delay, the output is same.
I'm actually calling the DataReceivedHan dler in a method called OpenPort, where I'm initializing the Serial Port instance. In the handler, I'm reading the input(generated responses) to the Port line by line into a string called 'data'.
The 'DataReceived' in "ComPort.DataRe ceived" represents the method that handles the data received event of ComPort To that I'm simply passing the handler.
Amey
Banfa 9,065
Recognized Expert Moderator Expert
No luck. Even after 30 secs of delay, the output is same.
This is indicative of the problem being the program not receiving the data properly rather than the modem not sending the data.
I'm actually calling the DataReceivedHan dler in a method called OpenPort, where I'm initializing the Serial Port instance. In the handler, I'm reading the input(generated responses) to the Port line by line into a string called 'data'.
The 'DataReceived' in "ComPort.DataRe ceived" represents the method that handles the data received event of ComPort To that I'm simply passing the handler.
OK so you are trying to use the receive event handler to get the data received by the serial port. Unfortunately it would appear from your results that this is not working correctly.
There does seem to be any method for your event handler to signal the main thread of execution that data has arrived.
Ideally what your program should be doing is
<indent>
DO
<indent>
Send AT Command
Wait for Response
Read Response
</indent>
WHILE More Comands to Send
</indent>
If you look at the Microsoft help pages they do not use the data received event, they start a thread and poll the serial port.
I have to say that I have no evidence that serial port events generate by the Windows sub-systems ever worked and I have recently had to hack receiving data from a modem using com port events out of a program and replace it with a polled read because the events where unreliable.
So here are 2 alternitive approaches
1. Start a thread to handle the receive. In the thread poll the com port for data just like the MS example.
2. Actually you are lucky, generally you know you will only be receiving responses after you have sent a command and you know the response will end with OK or ERROR or another known error code. You do not need to start a thread, send the command and then just poll for the response in you main thread (with a timeout). This doesn't handle URCs (unsolicited response codes) and I don't know if you need to, if you do this wont work because it only receives in response to a command and a URC can happen at anytime.
This is indicative of the problem being the program not receiving the data properly rather than the modem not sending the data.
OK so you are trying to use the receive event handler to get the data received by the serial port. Unfortunately it would appear from your results that this is not working correctly.
There does seem to be any method for your event handler to signal the main thread of execution that data has arrived.
Ideally what your program should be doing is
<indent>
DO
<indent>
Send AT Command
Wait for Response
Read Response
</indent>
WHILE More Comands to Send
</indent>
If you look at the Microsoft help pages they do not use the data received event, they start a thread and poll the serial port.
I have to say that I have no evidence that serial port events generate by the Windows sub-systems ever worked and I have recently had to hack receiving data from a modem using com port events out of a program and replace it with a polled read because the events where unreliable.
So here are 2 alternitive approaches
1. Start a thread to handle the receive. In the thread poll the com port for data just like the MS example.
2. Actually you are lucky, generally you know you will only be receiving responses after you have sent a command and you know the response will end with OK or ERROR or another known error code. You do not need to start a thread, send the command and then just poll for the response in you main thread (with a timeout). This doesn't handle URCs (unsolicited response codes) and I don't know if you need to, if you do this wont work because it only receives in response to a command and a URC can happen at anytime.
Hi,
I've been trying this using threads, but no luck.
I got this sample code from the web which I modified a bit. Here it is: -
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.IO.Ports;
-
using System.Diagnostics;
-
-
namespace SmsManager
-
{
-
class PortUtilities
-
{
-
public delegate void OnMessageLog(object sender, PortUtilitiesEventArgs e);
-
public static event OnMessageLog OnMessageLogged;
-
-
public static void Main()
-
{
-
OpenPort("COM9");
-
}
-
-
-
-
/// <summary>
-
/// Opens the supplied port number if the port is not already open
-
/// </summary>
-
/// <param name="portName">The Name of the port, "COM?"</param>
-
/// <returns>Opened Serial Port Object</returns>
-
public static SerialPort OpenPort(string portName)
-
{
-
SerialPort serialPort = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
-
try
-
{
-
serialPort.Handshake = Handshake.RequestToSend;
-
serialPort.ReadTimeout = 5000;
-
if (!serialPort.IsOpen) { serialPort.Open(); }
-
SendCMGFCommand(serialPort);
-
SendATCommand(serialPort);
-
//SendCPINCommand(serialPort, pinNumber);
-
}
-
catch (Exception ex)
-
{
-
LogMessage(ex.Message, EventLogEntryType.Error);
-
}
-
return serialPort;
-
}
-
-
-
-
/// <summary>
-
/// Sends an AT Command to the supplied port
-
/// </summary>
-
/// <param name="serialPort">Port to use to send command</param>
-
/// <returns>True if successful</returns>
-
private static bool SendATCommand(SerialPort serialPort)
-
{
-
DateTime timeout = DateTime.Now.AddMinutes(5);
-
string buffer = "";
-
try
-
{
-
serialPort.Write("AT+GMM\r");
-
buffer = "";
-
do
-
{
-
buffer += serialPort.ReadExisting();
-
Console.WriteLine(buffer);
-
if (DateTime.Now > timeout)
-
{
-
throw new Exception("AT Command timed out without receiving 'OK'.");
-
}
-
}
-
while (!buffer.Contains("OK"));
-
}
-
catch (Exception ex)
-
{
-
LogMessage(ex.Message, EventLogEntryType.Error);
-
return false;
-
}
-
return true;
-
}
-
-
-
-
/// <summary>
-
/// Sends the CMGFCommand to set the input type
-
/// </summary>
-
/// <param name="serialPort">Port to use to send command</param>
-
/// <returns>True if successful</returns>
-
private static bool SendCMGFCommand(SerialPort serialPort)
-
{
-
DateTime timeout = DateTime.Now.AddMinutes(5);
-
string buffer = "";
-
try
-
{
-
serialPort.Write("AT+CMGF=1\r");
-
buffer = "";
-
do
-
{
-
buffer += serialPort.ReadExisting();
-
Console.WriteLine(buffer);
-
if (DateTime.Now > timeout)
-
{
-
throw new Exception("AT+CMGF=1 Command timed out without receiving 'OK'.");
-
}
-
}
-
while (!buffer.Contains("OK"));
-
}
-
catch (Exception ex)
-
{
-
LogMessage(ex.Message, EventLogEntryType.Error);
-
return false;
-
}
-
return true;
-
}
-
-
-
-
private static void LogMessage(string message, EventLogEntryType eventLogEntryType)
-
{
-
PortUtilitiesEventArgs e = new PortUtilitiesEventArgs();
-
e.Message = message;
-
e.EventLogEntryType = eventLogEntryType;
-
if (OnMessageLogged != null)
-
{
-
OnMessageLogged(new object(), e);
-
}
-
}
-
}
-
-
-
-
public class PortUtilitiesEventArgs : EventArgs
-
{
-
public string Message;
-
public EventLogEntryType EventLogEntryType;
-
}
-
}
-
However even here, " DateTime timeout = DateTime.Now.Ad dMinutes(5);" a random response time is being assumed. Is there any way to wait till all the responses for a command are received, without using a random response time?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Leo Breebaart |
last post by:
Hi all,
I have a question about Python and delayed evaluation.
Short-circuiting of Boolean expressions implies that in:
>>> if a() and b():
any possible side-effects the call to b() might have will not
happen of a() returns true, because then b() will never be
|
by: Andreas Suurkuusk |
last post by:
Hi,
I just noticed your post in the "C# memory problem: no end for our problem?"
thread.
In the post you implied that I do not how the garbage collector works and
that I mislead people. Since the thread is over a month old, I decided to
start a new one with my response.
Please see my comments inline.
|
by: Brian |
last post by:
Hi all... This question is more for the GURUs out there. It is not a
question on how to do something, but why it happens, and I am trying to
figure out if there is a pattern. I am using IE, but this pattern also
happens in Mozilla, but not always the same way... I am not as interested in
how the browsers are different, but the big question...
|
by: subi |
last post by:
Hi,
I don't know where's the best place to post my question.
I hope it suits this group.
I have created an assembly with a delay sign attribute
set to true in the AssemblyInfo.cs. And the key file
attribute points to a "snk" file contains only public key.
After I compile the assembly, I set it to skip
verification by using "sn.exe -Vr"...
|
by: CSharpUser |
last post by:
How do I pass in command line parameters to the program at
startup in Visual Studio.
This is similar to doing csc program arg1 arg2.
I want to achieve the same effect running the program in
Visual Studio and pressing F5
| |
by: Amey Agnihotri |
last post by:
Hi! I'm trying to run a few AT commands on a GSM modem. I'm doing this in C#.Net 2005.
When I run a few commands one after the other, the response generation takes a long time. The response for the 1st command is returned as blank, i.e nothing. And its actual response is returned as the response to the 3rd command.
That is, due to the...
|
by: Andy |
last post by:
I have an ASP.NET webpage that allows a user to upload a file from the
client browser. I want to display an animated gif and report on the
progress of the upload. The <INPUTtag used for uploading files
requires the form to be posted. Posts stop javascripts and GIF
animations from running for the duration of the upload until a
response to...
|
by: MJP |
last post by:
I have a button which kicks off the generation of a report after which
the file will be downloaded. The report generation can take a long
time, so client side onclick event of the button also makes a <span>
tag visible which contains a nice message to the user thanking them
for their patience. Of course after the report has been generated...
|
by: jx2 |
last post by:
can any1 give me a good example for cli or CGI or comand line usage
i've read the description on php.net but i just dont understand whats going on
what do you use it for and how - i managed to run a script from comand line and i get the results back but... what for? lol i am really confused lol
thanks a lot
jx2
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |