Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 76 additions & 35 deletions Development/GXTerminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Gurux.Common;
using System.IO;
using Gurux.Shared;
#if !NETSTANDARD2_0 && !NETSTANDARD2_1 && !NETCOREAPP2_0 && !NETCOREAPP2_1 && !NETCOREAPP3_1 && !NET6_0
using System.Windows.Forms;
#endif //!NETSTANDARD2_0 && !NETSTANDARD2_1 && !NETCOREAPP2_0 && !NETCOREAPP2_1 && !NETCOREAPP3_1 && !NET6_0
using System.Xml;
using System.Diagnostics;
using System.IO.Ports;
Expand All @@ -65,8 +61,9 @@ public class GXTerminal : IGXMedia2, IGXVirtualMedia, INotifyPropertyChanged, ID
{
bool IsVirtual, VirtualOpen;
int hangsUpDelay = 0;
int connectionWaitTime = 30000;
int connectionWaitTime = 60000;
int commandWaitTime = 3000;
string initializeCommands = null;
private object m_sync = new object();

Progress progress;
Expand Down Expand Up @@ -1235,6 +1232,9 @@ public int WriteTimeout
/// <summary>
/// Get or set how long (ms) is waited before hang up is called.
/// </summary>
[MonitoringDescription("HangsUpDelay")]
[Browsable(true)]
[DefaultValue(3000)]
public int HangsUpDelay
{
get
Expand Down Expand Up @@ -1267,6 +1267,44 @@ public int HangsUpDelay
}
}

/// <summary>
/// Get or set modem initial settings separated by semicolons
/// </summary>
[MonitoringDescription("InitializeCommands")]
[Browsable(true)]
[DefaultValue(null)]
public string InitializeCommands
{
get
{
if (IsVirtual && m_OnGetPropertyValue != null)
{
string value = m_OnGetPropertyValue("InitializeCommands");
if (value != null)
{
return value;
}
}
lock (baseLock)
{
return initializeCommands;
}
}
set
{
bool change;
lock (baseLock)
{
change = initializeCommands != value;
initializeCommands = value;
}
if (change)
{
NotifyPropertyChanged("InitializeCommands");
}
}
}

/// <summary>
/// Closes the port connection, sets the System.IO.Ports.SerialPort.IsOpen property to false, and disposes of the internal System.IO.Stream object.
/// </summary>
Expand Down Expand Up @@ -1308,18 +1346,7 @@ public void Close()
{
if (progress == Progress.Connected)
{
//We are not expecting reply.
Thread.Sleep(1000);
Gurux.Common.ReceiveParameters<string> p = new Gurux.Common.ReceiveParameters<string>()
{
WaitTime = connectionWaitTime,
Count = 3
};
SendBytes(ASCIIEncoding.ASCII.GetBytes("+++"));
//It's OK if this fails.
Receive(p);
Thread.Sleep(HangsUpDelay);
SendCommand("ATH0\r", connectionWaitTime, null, false);
SendHangup();
}
}
}
Expand Down Expand Up @@ -1352,6 +1379,23 @@ public void Close()
}
}

public void SendHangup()
{
//We are not expecting reply.
Thread.Sleep(1500);
Gurux.Common.ReceiveParameters<string> p = new Gurux.Common.ReceiveParameters<string>()
{
WaitTime = connectionWaitTime,
Count = 3
};
SendBytes(ASCIIEncoding.ASCII.GetBytes("+++"));
//It's OK if this fails.
//Receive(p);
Thread.Sleep(HangsUpDelay);
DiscardInBuffer();
SendCommand("ATH0\r", connectionWaitTime, null, false);
}

/// <summary>
/// Discards data from the Terminal driver's receive buffer.
/// </summary>
Expand Down Expand Up @@ -1456,7 +1500,8 @@ public void Open()
{
if (InitializeCommands != null)
{
foreach (string it in InitializeCommands)
string[] initCommands = InitializeCommands.Split(new char[] { ';' });
foreach (string it in initCommands)
{
SendCommand(it + "\r\n", commandWaitTime, null, true);
}
Expand Down Expand Up @@ -1491,13 +1536,15 @@ public void Open()
}
}
progress = Progress.Connecting;
DiscardInBuffer();
reply = SendCommand("ATD" + PhoneNumber + "\r\n", connectionWaitTime, null, true);
progress = Progress.Connected;
}
}
}
catch (Exception)
{
//SendHangup();
Close();
throw;
}
Expand Down Expand Up @@ -1528,7 +1575,7 @@ void SendBytes(byte[] value)
}
}

string SendCommand(string cmd, int wt, string eop, bool throwError)
public string SendCommand(string cmd, int wt, string eop, bool throwError)
{
Gurux.Common.ReceiveParameters<string> p = new Gurux.Common.ReceiveParameters<string>()
{
Expand All @@ -1540,6 +1587,8 @@ string SendCommand(string cmd, int wt, string eop, bool throwError)
p.Eop = null;
p.Count = cmd.Length;
}
//Console.Write(">>>" + cmd);

SendBytes(ASCIIEncoding.ASCII.GetBytes(cmd));
StringBuilder sb = new StringBuilder();
int index = -1;
Expand Down Expand Up @@ -1573,6 +1622,7 @@ string SendCommand(string cmd, int wt, string eop, bool throwError)
}
else if (reply.Length > 5)
{
//Console.Write("<<<" + reply);
index = reply.LastIndexOf("\r\nOK\r\n");
if (index == -1)
{
Expand Down Expand Up @@ -1601,7 +1651,7 @@ string SendCommand(string cmd, int wt, string eop, bool throwError)
str += Environment.NewLine + SendCommand("AT+CEER\r", wt, null, false);
throw new Exception(str);
}
if (reply.LastIndexOf("ERROR") != -1)
if (reply.LastIndexOf("ERROR") != -1 || reply.LastIndexOf("DELAYED") != -1)
{
throw new Exception(Resources.ConnectionFailedErrorWhenTelephoneCallWasBeingEstablished);
}
Expand Down Expand Up @@ -1969,15 +2019,6 @@ public object Eop
}
}

/// <summary>
/// Modem initial settings.
/// </summary>
string[] InitializeCommands
{
get;
set;
}

/// <summary>
/// Media settings as a XML string.
/// </summary>
Expand Down Expand Up @@ -2016,9 +2057,9 @@ public string Settings
}
if (this.InitializeCommands != null)
{
tmp += "<Init>" + string.Join(";", InitializeCommands) + "</Init>";
tmp += "<Init>" + InitializeCommands + "</Init>";
}
if (this.ConnectionWaitTime != 30000)
if (this.ConnectionWaitTime != 60000)
{
tmp += "<ConnectionWaitTime>" + ConnectionWaitTime + "</ConnectionWaitTime>";
}
Expand All @@ -2041,7 +2082,7 @@ public string Settings
string str;
int result;

ConnectionWaitTime = 30000;
ConnectionWaitTime = 60000;
CommandWaitTime = 3000;
HangsUpDelay = 0;
using (XmlReader xmlReader = XmlReader.Create(new System.IO.StringReader(value), settings))
Expand All @@ -2053,7 +2094,7 @@ public string Settings
switch (xmlReader.Name)
{
case "Init":
InitializeCommands = xmlReader.ReadString().Split(new char[] { ';' });
InitializeCommands = xmlReader.ReadString();
break;
case "Number":
phoneNumber = xmlReader.ReadString();
Expand Down Expand Up @@ -2163,7 +2204,7 @@ public void Send(object data)
{
((Gurux.Common.IGXMedia)this).Send(data, null);
}
#if !NETSTANDARD2_0 && !NETSTANDARD2_1 && !NETCOREAPP2_0 && !NETCOREAPP2_1 && !NETCOREAPP3_1 && !NET6_0
#if NET462_OR_GREATER || WINDOWS
/// <summary>
/// Shows the serial port Properties dialog.
/// </summary>
Expand All @@ -2188,7 +2229,7 @@ public System.Windows.Forms.Form PropertiesForm
return new Settings(this);
}
}
#endif //!NETSTANDARD2_0 && !NETSTANDARD2_1 && !NETCOREAPP2_0 && !NETCOREAPP2_1 && !NETCOREAPP3_1 && !NET6_0
#endif //NET462_OR_GREATER || WINDOWS

/// <inheritdoc cref="IGXMedia.Receive"/>
public bool Receive<T>(Gurux.Common.ReceiveParameters<T> args)
Expand Down
Loading