Hello,
The modem is a Verizon CDMA modem - MultiConnect Cell 100 Series MTC-EV3. I am using the fixed paddle shaped antenna that was supplied with the modem. The modem is using its own power supply. The board is also using a 12v 1.2A power supply, not drawing from the USB connection. The modem has good signal strength and I’m able to connect it to my PC and use the windows dialer to establish an internet connection.
I started to consolidate my code into a better example for posting on the forum. During this process I noticed something which I didn’t notice in my previous solution. After sending the ATD#777 command I have no bytes to read from the serial port until I issue an escape “+++” then I see my “CONNECT 3100000” that I was expecting.
When I connect to the modem with Putty and do a ATD#777 after a second or two I get a response of “CONNECT 3100000” which indicates the data connection is established and you’re free to establish a PPP connection. I don’t know much about serial communications, but there is a difference in behavior between Putty and my code. I believe after sending the ATD#777 the modem will raise the Carrier Detect. Is that why I’m not reading any bytes after the ATD command? Obviously I’m still able to send data to the port and modem responds as expected to the escape command.
Here is my simplified program:
public partial class Program
{
private SerialPort port;
private PPPSerialModem modem;
private void ProgramStarted()
{
Debug.Print("Program Started");
NetworkChange.NetworkAvailabilityChanged += (x, y) => { Debug.Print("Network Available: " + y.IsAvailable); };
NetworkChange.NetworkAddressChanged += (x, y) => { Debug.Print("Network Address Changed."); };
//Port is a K socket UART with handshake.
string serialPortName = GT.Socket.GetSocket(4, false, null, "").SerialPortName;
port = new SerialPort(serialPortName, 115200, Parity.None, 8, StopBits.One);
port.ReadTimeout = 100;
port.Open();
port.DiscardInBuffer();
port.DiscardOutBuffer();
modem = new PPPSerialModem(port);
new Thread(Work).Start();
}
private void Work()
{
Debug.Print("Thread Start");
try
{
SendATCommand("ATE0", 1000);
//Modem response "ATE0<CR><CR><LF>OK<CR><LF>"
SendATCommand("AT+CSQ", 1000);
//Modem response "<CR><LF>+CSQ: 24,99<CR><LF><CR><LF>OK<CR><LF>"
SendATCommand("AT+CREG?", 2000);
//Modem response "<CR><LF>+CREG: 0,1<CR><LF><CR><LF>OK<CR><LF>"
SendATCommand("ATD#777", 5000);
//Got no reponse, but expected "CONNECT 3100000"
//At this point modem is all fired up and ready for data transfer.
modem.Open();
modem.Connect(PPPSerialModem.AuthenticationType.Pap, "", "");
Thread.Sleep(300000); //Wait 5 minutes for NetworkAvailabilityChanged
//After 4 minutes I see these:
//NetworkAddressChanged raised.
//NetworkAvailabilityChanged raised. IsAvailabe = False
HTTPRequest(); //Obviously this fails.
modem.Disconnect();
modem.Close();
SendATCommand("+++", 5000);
//Here I get "<CR><LF>CONNECT 3100000<CR><LF><CR><LF>NO CARRIER<CR><LF>"
SendATCommand("ATH", 1000);
//Modem response "<CR><LF>OK<CR><LF>"
port.Close();
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
}
Debug.Print("Thread End");
}
private void SendATCommand(string command, int timeout)
{
Debug.Print("Command Sent: " + command);
if (command != "+++")
command += "\r";
var sendBuffer = Encoding.UTF8.GetBytes(command);
port.Write(sendBuffer, 0, sendBuffer.Length);
Thread.Sleep(timeout);
ReadResponse();
}
private void ReadResponse()
{
byte[] data = new byte[port.BytesToRead];
int bytesRead = port.Read(data, 0, data.Length);
if (bytesRead > 0)
{
string response = new string(Encoding.UTF8.GetChars(data, 0, bytesRead));
response = new StringBuilder(response).Replace("\r", "<CR>").Replace("\n", "<LF>").ToString();
Debug.Print("Received: " + response);
}
else
{
Debug.Print("No response from modem.");
}
}
private void HTTPRequest()
{
try
{
string url = "https://www.google.com/";
using (var req = System.Net.HttpWebRequest.Create(url))
{
using (var res = req.GetResponse())
{
Debug.Print("HTTP Response was this long: " + res.ContentLength.ToString());
}
}
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
}
}
}
Sorry if there is a bunch of whitespace. Not sure why its doing that. Hopefully that’s just the preview…
Thanks for your help.
-Phil