📄 sentenceextractor.cs
字号:
using System;
using System.Text;
using System.Threading;
namespace BLT
{
/// <summary>
/// Used to Collect and break down gps-input in to single gps sentences
/// </summary>
public class GPSSentenceExtractor
{
/// <summary>
/// received content
/// </summary>
string strReceivedContent;
public GPSSentenceExtractor()
{
this.strReceivedContent= String.Empty;
}
/// <summary>
/// Length of received content
/// </summary>
int Length
{
get
{
return this.strReceivedContent.Length;
}
}
public bool Get(out string strGPSSentence)
{
strGPSSentence=String.Empty;
bool bValid=false;
try
{
// lock the resource
Monitor.Enter(strReceivedContent);
if(this.strReceivedContent!=String.Empty)
{
// trim the uncomplete sentence
// it is used to only at the begin of reading process
if(this.strReceivedContent.StartsWith("$")!=true)
{
int nCStart=this.strReceivedContent.IndexOf('$',0);
strReceivedContent=this.strReceivedContent.Remove(0,nCStart);
}
if(this.Length>0)
{
int nCEnd=this.strReceivedContent.IndexOf('$',1);
if(nCEnd>0) // exists a full sentence
{
// extract the sentence
strGPSSentence=this.strReceivedContent.Substring(0,nCEnd);
strGPSSentence=strGPSSentence.Replace("\r\n",String.Empty);
bValid=true;
// remove the extracted sentence from the content
this.strReceivedContent=this.strReceivedContent.Remove(0,nCEnd);
}
}
}
}
catch(Exception oException)
{
throw oException;
}
finally
{
Monitor.Exit(strReceivedContent);
}
return bValid;
}
/// <summary>
/// Collects received data from GPS
/// </summary>
/// <param name="strInput"></param>
public void Add(string strInput)
{
try
{
// lock
Monitor.Enter(strReceivedContent);
// add
this.strReceivedContent+=strInput;
}
catch(Exception oException)
{
throw oException;
}
finally
{
// unlock
Monitor.Exit(strReceivedContent);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -