📄 ddata.cs
字号:
#endregion
#region "重新设置数据结构,清除原有数据"
/// <summary>
/// 重新设置数据结构,清除原有数据
/// </summary>
/// <param name="timepoints">时间点</param>
/// <param name="channels">道数</param>
public void ResetNew(int timePoints, int channels)
{
_data = new int[timePoints][];
for (int i = 0; i < timePoints; i++)
{
_data[i] = new int[channels];
}
_overflowtable = new int[timePoints];
_tmpdata = new byte[channels * 4];
_tmpvalue = new int[channels];
_receivedbyte = 0;
_dataindex = TimePoints;
}
#endregion
#region "清除数据"
/// <summary>
/// 清除数据
/// </summary>
public void Clean()
{
for (int i = 0; i < _data.GetLength(0); i++)
{
Array.Clear(_data[i], 0, _data[i].Length);
}
Array.Clear(_overflowtable, 0, _overflowtable.Length);
_receivedbyte = 0;
_dataindex = TimePoints;
}
#endregion
#region "返回指定时间点的数据"
/// <summary>
/// 返回指定时间点的数据
/// </summary>
/// <param name="time">时间点</param>
/// <returns>一维数组,下标表示道址,数组元素是计数数据</returns>
public void GetData(out int[] data, int timePoint)
{
if(timePoint < 1 || timePoint > TimePoints)
{
throw new Exception("timepoint overflow!");
}
if(_overflow != null)//检查事件是否有预定
{
double max = _islinecoordinate? _overflowtable[timePoint - 1] : Math.Log10(_overflowtable[timePoint - 1]);
if(max > _maxcount)//如果全谱区的数据溢出则产生全谱区溢出事件
{
CountOverFlowEventArgs e = new CountOverFlowEventArgs(OverFlowType.Normal, max);
_overflow(this, e);
}
if(max > _zmaxcount)//如果放大区的数据溢出则产生放大区溢出事件
{
CountOverFlowEventArgs e = new CountOverFlowEventArgs(OverFlowType.Zoom, max);
_overflow(this, e);
}
}
data = _data[timePoint - 1];
}
#endregion
#region "返回最后使用的时间点的数据"
/// <summary>
/// 返回最后使用的时间点的数据
/// </summary>
/// <returns>一维数组,下标表示道址,数组元素是计数数据</returns>
public void GetData(out int[] data)
{
if (_dataindex == TimePoints)
{
throw new Exception("No data.");
}
if(_overflow != null)//检查事件是否有预定
{
double max = _islinecoordinate ? _overflowtable[_dataindex] : Math.Log10(_overflowtable[_dataindex]);
if(max > _maxcount)//如果全谱区的数据溢出则产生全谱区溢出事件
{
CountOverFlowEventArgs e = new CountOverFlowEventArgs(OverFlowType.Normal, max);
_overflow(this, e);
}
if(max > _zmaxcount)//如果放大区的数据溢出则产生放大区溢出事件
{
CountOverFlowEventArgs e = new CountOverFlowEventArgs(OverFlowType.Zoom, max);
_overflow(this, e);
}
}
data = _data[_dataindex];
if (_dataindex < TimePoints - 1)
{
_dataindex++;
}
}
#endregion
#region "开始或停止串口接收数据"
/// <summary>
/// 开始或停止串口接收数据
/// </summary>
/// <param name="start">开始或停止</param>
public void ReceiveData(bool start)
{
if (start)
{
if (_stop)
{
_serialport.Write("1");
_stop = false;
}
}
else
{
_stop = true;
}
}
#endregion
#region "从文件读入数据"
/// <summary>
/// 从文件读入数据
/// </summary>
/// <param name="filename">要读入的文件名</param>
/// <param name="filename">读入的属性</param>
public void BuildData(string fileName, out Hashtable attributes)
{
int timepoints = 0;
int channels = 0;
int timepoint = 0;
int channel = 0;
int count = 0;
Hashtable tmpattributes = new Hashtable();
StreamReader filereader = new StreamReader(fileName);
XmlTextReader datareader = new XmlTextReader(filereader);
datareader.WhitespaceHandling = WhitespaceHandling.None;
while(datareader.Read())
{
if(datareader.NodeType == XmlNodeType.Element)
{
if(datareader.Name == "ddata")
{
datareader.MoveToAttribute("timepoints");//读入时间点数
if (datareader.Name == "timepoints")
{
timepoints = Convert.ToInt32(datareader.Value);
_data = new int[timepoints][];
_overflowtable = new int[timepoints];
}
else
{
throw (new XmlException());
}
datareader.MoveToAttribute("channels");//读入道数
if (datareader.Name == "channels")
{
for (int i = 0; i < timepoints; i++)
{
channels = Convert.ToInt32(datareader.Value);
_data[i] = new int[channels];
}
_tmpdata = new byte[channels * 4];
_tmpvalue = new int[channels];
_receivedbyte = 0;
}
else
{
throw (new XmlException());
}
while (datareader.MoveToNextAttribute())
{
tmpattributes[datareader.Name] = datareader.Value;
}
}else
if(datareader.Name == "timepoint")//时间点
{
datareader.MoveToAttribute("index");//读入时间点
if (datareader.Name == "index")
{
timepoint = Convert.ToInt32(datareader.Value);
}
else
{
throw (new XmlException());
}
}
else
if(datareader.Name == "data")//时间点//道址和计数
{
datareader.Read();//读入计数
string[] data = datareader.Value.Split(',');
for(channel = 1; channel < data.Length; channel++)
{
count = Convert.ToInt32(data[channel - 1]);
SetData(timepoint, channel, count);
}
}
}
}
_dataindex = 0;
datareader.Close();
filereader.Close();
attributes = tmpattributes;
}
/// <summary>
/// 从文件读入数据
/// </summary>
/// <param name="filename">要读入的文件名</param>
public void BuildData(string fileName)
{
StreamReader datareader = new StreamReader(fileName);
ArrayList tmpdata = new ArrayList();
while (!datareader.EndOfStream)
{
tmpdata.Add(datareader.ReadLine());
}
_data = new int[1][];
_data[0] = new int[tmpdata.Count];
_overflowtable = new int[1];
for (int i = 0; i < _data[0].Length; i++)
{
SetData(1, i + 1, Convert.ToInt32(tmpdata[i]));
}
datareader.Close();
}
#endregion
#region "保存谱数据"
/// <summary>
/// 保存谱数据
/// </summary>
/// <param name="filename">要保存的文件名</param>
/// <param name="attributes">要保存的属性</param>
public void SaveData(string fileName, Hashtable attributes)
{
//保存数据
XmlTextWriter datawriter = new XmlTextWriter(fileName, System.Text.Encoding.Default);
datawriter.Formatting = Formatting.Indented;
datawriter.WriteStartDocument();
datawriter.WriteStartElement("ddata");
datawriter.WriteAttributeString("timepoints", _data.GetLength(0).ToString());
datawriter.WriteAttributeString("channels", _data[0].GetLength(0).ToString());
foreach (string s in attributes.Keys)
{
datawriter.WriteAttributeString(s, attributes[s].ToString());
}
//写入每个时间点下的数据
for(int i = 1; i <= _data.Length; i++)
{
datawriter.WriteStartElement("timepoint");
datawriter.WriteAttributeString("index", i.ToString());
StringBuilder data = new StringBuilder();
for(int j = 0; j < _data[0].GetLength(0); j++)
{
//写入每道的道址及其计数
data.Append(_data[i - 1][j]);
data.Append(',');
}
datawriter.WriteElementString("data", data.ToString());
datawriter.WriteEndElement();//timepoint
}
datawriter.WriteEndElement();//ddata
datawriter.WriteEndDocument();
datawriter.Flush();
datawriter.Close();
}
/// <summary>
/// 保存谱数据
/// </summary>
/// <param name="fileName">要保存的文件名</param>
public void SaveData(string fileName)
{
StreamWriter datawriter = new StreamWriter(fileName);
foreach (int n in _data[_data.Length - 1])
{
datawriter.WriteLine(n);
}
datawriter.Close();
}
#endregion
#region "返回指定时间点指定道的数据"
/// <summary>
/// 返回指定时间点指定道的数据
/// </summary>
/// <param name="time">时间点</param>
/// <param name="channel">道址</param>
/// <returns>计数</returns>
public int GetValue(int timePoint, int channel)
{
if(timePoint < 1 || timePoint > TimePoints)
{
throw new Exception("timepoint overflow!");
}
if (channel < 0 || channel > _data[0].Length)
{
throw new Exception("channel overflow!");
}
return _data[timePoint - 1][channel - 1];
}
#endregion
#region "返回最后使用的时间点指定道的数据"
/// <summary>
/// 返回最后时间点指定道的数据
/// </summary>
/// <param name="channel">道址</param>
/// <returns>计数</returns>
public int GetValue(int channel)
{
if (channel < 0 || channel > _data[0].Length)
{
throw new Exception("channel overflow!");
}
return _data[_dataindex][channel - 1];
}
#endregion
#endregion
#endregion
#region "事件"
/// <summary>
/// 计数溢出事件
/// </summary>
private event CountOverFlowEventHandler _overflow;
/// <summary>
/// 数据到达事件
/// </summary>
private event EventHandler _datareceived;
/// <summary>
/// 增加或删除计数溢出事件
/// </summary>
public event CountOverFlowEventHandler OverFlow
{
add
{
_overflow += value;
}
remove
{
_overflow -= value;
}
}
/// <summary>
/// 增加或删除数据到达事件
/// </summary>
public event EventHandler DataReceived
{
add
{
_datareceived += value;
}
remove
{
_datareceived -= value;
}
}
#endregion
}
#endregion
#region "计数溢出事件的委托"
/// <summary>
/// 计数溢出事件的委托
/// </summary>
public delegate void CountOverFlowEventHandler(object sender, CountOverFlowEventArgs e);
#endregion
#region "计数溢出事件类"
/// <summary>
/// 计数溢出事件类
/// </summary>
public class CountOverFlowEventArgs : EventArgs
{
/// <summary>
/// 计数溢出类型
/// </summary>
private OverFlowType _oftype;
private double _maxcount;
public CountOverFlowEventArgs(OverFlowType oftype, double maxcount)
{
_oftype = oftype;
_maxcount = maxcount;
}
/// <summary>
/// 获取计数溢出类型
/// </summary>
public OverFlowType Oftype
{
get
{
return _oftype;
}
}
/// <summary>
/// 获取最大计数
/// </summary>
public double MaxCount
{
get
{
return _maxcount;
}
}
}
#endregion
#region "计数溢出枚举"
/// <summary>
/// 表示是哪个区的计数溢出
/// </summary>
public enum OverFlowType
{
/// <summary>
/// Normal表示全谱区,Zoom表示放大曲
/// </summary>
Normal, Zoom
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -