After I PPP.Disconnect, I then don’t reset the UART at all, however I do read and write to it to run through the AT command processing again.
I check all responses… so the UART is operational at the point of calling PPP.Connect.
Telit module code below:
using System;
using Microsoft.SPOT;
using System.IO.Ports;
using System.Text;
using System.Threading;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
using Microsoft.SPOT.Net.NetworkInformation;
namespace Link2.Hardware
{
public class Telit
{
public SerialPort port;
static byte[] cr = new byte[1];
Thread modem;
private bool restart = true;
public Telit(string serialPort)
{
cr[0] = Link2.Encoding.CR;
port = new SerialPort(serialPort, 115200);
PPP.Enable(port);
port.Handshake = Handshake.XOnXOff;
port.Open();
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
modem = new Thread(ModemThread);
modem.Start();
}
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
//Connected = e.IsAvailable;
}
public bool Connected = false;
private int restartcnt;
public void ModemThread()
{
while (true)
{
if (!Connected)
{
if (restart)
{
ResetModem();
restart = false;
restartcnt = 0;
}
if (ClientConnectModem())
{
NetworkInterface[] netif = NetworkInterface.GetAllNetworkInterfaces();
Connected = false;
// attempt PPP connection
Debug.Print("Connect PPP");
switch (PPP.Connect("", ""))
{
case PPP.ConnectionStatus.Authentication_Faild:
Debug.Print("Authentication_Failed");
break;
case PPP.ConnectionStatus.Connection_Faild:
Debug.Print("Connection_Failed");
break;
case PPP.ConnectionStatus.Connected:
// PPP setting will overload the first interface settings since only one interface is supported.
netif = NetworkInterface.GetAllNetworkInterfaces();
Debug.Print("PPP Network settings:");
Debug.Print("IP Address: " + netif[0].IPAddress);
Debug.Print("Subnet Mask: " + netif[0].SubnetMask);
Debug.Print("Default Getway: " + netif[0].GatewayAddress);
Debug.Print("DNS Server: " + netif[0].DnsAddresses[0]);
Connected = true;
restartcnt = 0;
break;
case PPP.ConnectionStatus.Disconnected:
Debug.Print("Disconnected");
break;
}
}
else
{
if (restartcnt++ > 3)
{
restart = true;
}
}
}
Thread.Sleep(1000);
}
}
OutputPort gsmpower;
OutputPort gsmreset;
public void ResetModem()
{
Debug.Print("Reset modem");
// bounce gsm reset
if (gsmpower == null)
{
gsmpower = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.IO23, false);
}
else
{
gsmpower.Write(false);
}
Thread.Sleep(3000);
gsmpower.Write(true);
if (gsmreset == null)
{
gsmreset = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.IO49, false);
}
else
{
gsmreset.Write(false);
}
Thread.Sleep(200);
gsmreset.Write(true);
}
byte[] reply = new byte[9];
public bool SendATCommand(string command, bool connect = false)
{
bool ret = false;
byte[] output = System.Text.Encoding.UTF8.GetBytes(command);
Debug.Print(command);
port.Flush();
port.Write(output, 0, output.Length);
port.Write(cr,0,1);
DateTime expire = DateTime.Now.AddSeconds(3);
// search for OK <CR>
string compare = "OK\r";
string sreply = "";
if (connect)
{
// search for CONNECT <CR>
compare = "CONNECT\r";
}
int expectedlen = compare.Length - 1;
reply[compare.Length] = 0;
while (!ret && (expire > DateTime.Now))
{
if (port.BytesToRead > 0)
{
// mini fifo as we want the last bit of data, not an echo etc.
Array.Copy(reply, 1, reply, 0, expectedlen);
port.Read(reply, expectedlen, 1);
try
{
sreply = new string(System.Text.Encoding.UTF8.GetChars(reply));
}
catch
{
// ignore
}
ret = (sreply == compare);
}
Thread.Sleep(20);
}
Debug.Print(sreply);
return ret;
}
public bool ClientConnectModem()
{
int retry = 0;
// PPP disconnect command?
port.Write(new byte[] { 0x7E, 0xFF, 0x7D, 0x23, 0xC0, 0x21, 0x7D, 0x25, 0x7D, 0x2A, 0x7D, 0x20, 0x7D, 0x30, 0x2B, 0xD3, 0x45, 0xAF, 0x7D, 0x20, 0x3C, 0xCD, 0x74, 0x7D, 0x20, 0x7D, 0x20, 0x7D, 0x20, 0x7D, 0x20, 0x2A, 0x9D, 0x7E }, 0, 34);// Terminate request
// try and disconnect
SendATCommand("\r+++");
SendATCommand("ATH");
// Soft Reset, basic default restore
bool ret = SendATCommand("ATZ");
if (ret)
{
LED.RadioFlash();
}
// Echo Disabled
ret = ret && SendATCommand("ATE");
// select US frequencies
ret = ret && SendATCommand("AT#BND=3");
// XonXOff flow control
ret = ret && SendATCommand("AT&K=1");
if (ret)
{
do
{
ret = SendATCommand("AT+cgdcont=2,\"IP\",\"broadband\"\r");
}
while (!ret && (retry++ < 20));
}
if (ret)
{
retry = 0;
do
{
ret = SendATCommand("ATDT*99***2#", true);
}
while (!ret && (retry++ < 20));
}
return ret;
}
public void Restart()
{
PPP.Disconnect();
restart = true;
Connected = false;
}
}
}