C#实现CAT021报⽂格式解析
这⼏天接到⼀个⼩项⽬,需要解析 CAT021报⽂。费了九⽜⼆虎之⼒终于看懂了报⽂解析的规则,接下来我就对我从⼆进制数据到实例出各数据项相应的对象的过程做⼀个⼩结,希望能帮到有需要的朋友。
⾸先是总体⽅法:
/// <summary>
/// 接收到的数据解析
/// </summary>
/// <param name="datas"></param>
/// <returns></returns>孙子兵法详解
private static DataBlock DateParr(byte[] datas)
{
int cat = 0;黄豆发芽的过程
int len = 0;
byte[] fspecbytes;
byte[] temp = new byte[4];
DataBlock datablock = new DataBlock();
Array.Copy(datas, 0, temp, 0, 1);
cat = BitConverter.ToInt32(temp, 0);
Array.Clear(temp, 0, temp.Length);
Array.Copy(datas, 1, temp, 0, 2);
Array.Rever(temp, 0, 2);
len = BitConverter.ToInt32(temp, 0);
Array.Clear(temp, 0, temp.Length);
荞麦面条
fspecbytes = getFspecBytes(datas);
List<DatagramUap> dataitems = DatagramParr(fspecbytes, len,datas);
datablock.Cat = cat;
datablock.Len = len;
datablock.Dataitem = dataitems;
return datablock;
}
接下来是核⼼⽅法:
/// 解析出数据项的实体列表
/// </summary>
/// <param name="fspecbytes"></param>
/
// <param name="len"></param>
/// <param name="datas"></param>
/// <returns></returns>
private static List<DatagramUap> DatagramParr(byte[] fspecbytes, int len, byte[] datas) {
int count = 0;
List<DatagramUap> dataitems = new List<DatagramUap>();
while (count < fspecbytes.Length)
{
int index = 7;
while (index > 0)
{
byte[] tempbytes = new byte[fspecbytes.Length];
Array.Copy(fspecbytes, tempbytes, fspecbytes.Length);
tempbytes[count] <<= 7 - index;
tempbytes[count] >>= 7;
byte[] temp1 = new byte[4];
temp1[0] = tempbytes[count];
if (BitConverter.ToInt32(temp1, 0) != 0)
{
DatagramUap datagramuap = InitDategram(count, index);
dataitems.Add(datagramuap);
}
草莓种植时间
index--;
}
count++;
}
int currnetbytenum = 0;
byte[] databytes = new byte[len - 3 - fspecbytes.Length];
Array.Copy(datas, 3 + fspecbytes.Length, databytes, 0, databytes.Length);
foreach (DatagramUap dataitem in dataitems)
{
byte[] bytes = new byte[dataitem.Len];
Array.Copy(databytes, currnetbytenum, bytes, 0, bytes.Length);
dataitem.Databytes = bytes;
currnetbytenum += dataitem.Len;
}
return dataitems;
}
根据位置确定数据项:
/// 根据位置确定数据项
别让我放不下/// </summary>
/// <param name="count">符号字节第⼏个字节</param>
/// <param name="index">字节第⼏位</param>
/// <returns></returns>
private static DatagramUap InitDategram(int count, int index) {
DatagramUap datagram = new DatagramUap();
switch (count)
{
ca 0:
Dategram0(index,datagram);
break;
ca 1:
Dategram1(index, datagram);
break;
ca 2:
Dategram2(index, datagram);
break;
ca 3:
Dategram3(index, datagram);
break;
ca 4:
Dategram4(index, datagram);
break;
}
return datagram;
}
解析出标识符所占的字节:
/// 解析出标识符所占的字节
/// </summary>
/// <param name="dates"></param>
/// <returns></returns>
private static byte[] getFspecBytes(byte[] dates)
{
int count = 3;
byte[] fspecbytes;
//如果下⼀个字节是标识符
while (IsMoreFspec(dates[count]))
{
熟女良家count++;
}
//确定标识符字节数
fspecbytes = new byte[count - 2];
Array.Copy(dates, 3, fspecbytes, 0, fspecbytes.Length); return fspecbytes;
}
判断下⼀个字节是否是符号字节:
/// <summary>
/// 判断下⼀个字节是否是符号字节
/// </summary>
/
// <param name="temp"></param>
/// <returns></returns>王子公主历险记
private static Boolean IsMoreFspec(byte temp)
{
Boolean ismore = fal;
byte[] tempbytes = new byte[4];
temp <<= 7;
temp >>= 7;
tempbytes[0] = temp;
if (BitConverter.ToInt32(tempbytes, 0) == 0)
{
政策的英文
ismore = fal;
}
el { ismore = true; }
return ismore;
}
当为第1个描述符字节时,初始化对象,其他初始化对象过程⼀样,看源代码就知道:
/// <summary>
/// 当为第1个描述符字节时,初始化对象
/// </summary>
/// <param name="index"></param>
/// <param name="datagram"></param>
private static void Dategram0(int index, DatagramUap datagram)
{
switch (index)
{
ca 7:
datagram.Dateitemname = "数据源识别";
datagram.DateitemNum = "1021/010";
datagram.Fra = 1;
datagram.Len = 2;
break;
ca 6:
datagram.Dateitemname = "⽬标报告描述符";
datagram.DateitemNum = "1021/040";
datagram.Fra = 2;
datagram.Len = 2;
break;
ca 5:
datagram.Dateitemname = "⽇时间";
datagram.DateitemNum = "1021/030";
datagram.Fra = 3;
datagram.Len = 3;
break;
ca 4:
datagram.Dateitemname = "在WGS-84坐标中的位置";
datagram.DateitemNum = "1021/130";
datagram.Fra = 4;
datagram.Len = 8;
break;
ca 3:
datagram.Dateitemname = "⽬标地址";
datagram.DateitemNum = "1021/080";
datagram.Fra = 5;
datagram.Len = 3;
break;
ca 2:
datagram.Dateitemname = "⼏何⾼度";
datagram.DateitemNum = "1021/140";
datagram.Fra = 6;
datagram.Len = 2;
break;
ca 1:
datagram.Dateitemname = "品质因素";
datagram.DateitemNum = "1021/090";
datagram.Fra = 7;
datagram.Len = 2;
break;
}
}