if(textBox_Udp.Text=="" || textBox_port.Text=="")
{
MessageBox.Show("ip地址或端口号为空", "异常问题", MessageBoxButtons.OKCancel);
return;
}
textBox_disp.Text = "";
str00 = "";
str3 = "";
UInt16 pack_cnt = 0;
string[] iplist = textBox_Udp.Text.Split('.');
try
{
//地址检查
if (int.Parse(iplist[0]) < 224 || int.Parse(iplist[0]) > 239)
{
throw new Exception("IP地址不是组播");
}
if (int.Parse(iplist[1]) < 0 || int.Parse(iplist[1]) > 225)
{
throw new Exception("IP地址越界");
}
if (int.Parse(iplist[2]) < 0 || int.Parse(iplist[2]) > 225)
{
throw new Exception("IP地址越界");
}
if (int.Parse(iplist[3]) < 0 || int.Parse(iplist[3]) > 225)
{
throw new Exception("IP地址越界");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "异常问题", MessageBoxButtons.OKCancel);
return;
}
int port = int.Parse(textBox_port.Text);
UdpClient client = new UdpClient(int.Parse(textBox_port.Text));
client.JoinMulticastGroup(System.Net.IPAddress.Parse(textBox_Udp.Text));
System.Net.IPEndPoint multicast = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
udpConn = true;// 代表udp 已经连接
try
{
while (true)
{
byte[] buf = client.Receive(ref multicast);
for (int i = 0; i < buf.Length; i++)
{
str00 += buf[i].ToString("X02") + " ";
if (rx_data_pos < 10000000)
{
disp_byte10M[rx_data_pos++] = buf[i];
}
else
{
rx_data_pos = 0;
F_rx_buf_cycle_to_head = true;
disp_byte10M[rx_data_pos++] = buf[i];
}
}
disp_text = str00;
textBox_disp.Text = disp_text; //显示数据
Int32 frame_length = 0; //帧长
Int32 decode_start_pos = 0;
Int16 buff;
//收到数据后开始解析
if (F_rx_buf_cycle_to_head == false) //还没存储的翻过来
{
if (rx_data_pos >= (decode_data_pos + 65))//有足够的数据来解析 (33*2)
{
for (Int32 i = decode_data_pos; i < (rx_data_pos - 59); i++)
{
if ((disp_byte10M[i] == 0x20) && (disp_byte10M[i + 1] == 0x01)) //定位信息包
{
frame_length = (disp_byte10M[i + 1 * 2 + 0]) | (disp_byte10M[i + 1 * 2 + 1] << 8); //帧长
if ((disp_byte10M[i + frame_length - 2] == 0xaa) && (disp_byte10M[i + frame_length - 1] == 0x55))
{
decode_start_pos = i;
//30 校验和,第0字到第29字累加,取低16位
UInt16 buff_16, mm;
for (mm = 0, buff_16 = 0; mm < 32; mm++)
{
buff_16 += (UInt16)((disp_byte10M[i + mm * 2 + 0]) | (disp_byte10M[i + mm * 2 + 1] << 8));
}
if (buff_16 == (UInt16)((disp_byte10M[i + 32 * 2 + 0]) | (disp_byte10M[i + 32 * 2 + 1] << 8)))
{ //校验和正确后
buff = disp_byte10M[i + 6 * 2 + 0];
year1 = (buff >> 4) * 10 + (buff & 0x000F);//年
buff = disp_byte10M[i + 6 * 2 + 1];
month1 = (buff >> 4) * 10 + (buff & 0x000F);//月
buff = disp_byte10M[i + 7 * 2 + 0];
date1 = (buff >> 4) * 10 + (buff & 0x000F);//日
buff = disp_byte10M[i + 7 * 2 + 1];
hour1 = (buff >> 4) * 10 + (buff & 0x000F);//时
buff = disp_byte10M[i + 8 * 2 + 0];
minute1 = (buff >> 4) * 10 + (buff & 0x000F);//分
buff = disp_byte10M[i + 8 * 2 + 1];
second1 = (buff >> 4) * 10 + (buff & 0x000F);//秒
usecond1 = (disp_byte10M[i + 9 * 2 + 0] + (disp_byte10M[i + 9 * 2 + 1] << 8)) * 25;//微秒
latitude = (double)((disp_byte10M[i + 12 * 2 + 0] << 0) | (disp_byte10M[i + 12 * 2 + 1] << 8)
| (disp_byte10M[i + 13 * 2 + 0] << 16) | (disp_byte10M[i + 13 * 2 + 1] << 24)) / 10000000;
if (disp_byte10M[i + 14 * 2 + 0] == 1) char_South_Nouth = "N";
else char_South_Nouth = "S";
longitude = (double)((disp_byte10M[i + 15 * 2 + 0] << 0) | (disp_byte10M[i + 15 * 2 + 1] << 8)
| (disp_byte10M[i + 16 * 2 + 0] << 16) | (disp_byte10M[i + 16 * 2 + 1] << 24)) / 10000000;
if (disp_byte10M[i + 17 * 2 + 0] == 1) char_East_West = "E";
else char_East_West = "W";
altitude = (double)((disp_byte10M[i + 18 * 2 + 0] << 0) | (disp_byte10M[i + 18 * 2 + 1] << 8)
| (disp_byte10M[i + 19 * 2 + 0] << 16) | ((disp_byte10M[i + 19 * 2 + 1] & 0x7F) << 24)) / 10000;
if ((disp_byte10M[i + 19 * 2 + 1] & 0x80) != 0) altitude = -altitude;
shuiping_speed = (double)((disp_byte10M[i + 20 * 2 + 0] << 0) | (disp_byte10M[i + 20 * 2 + 1] << 8)
) / 100;
yundong_direction = (double)((disp_byte10M[i + 21 * 2 + 0] << 0) | (disp_byte10M[i + 21 * 2 + 1] << 8)
| (disp_byte10M[i + 22 * 2 + 0] << 16) | (disp_byte10M[i + 22 * 2 + 1] << 24)) / 1000000;
chuizhi_speed = (double)((disp_byte10M[i + 23 * 2 + 0] << 0) | ((disp_byte10M[i + 23 * 2 + 1] & 0x7F) << 8)
) / 100;
if ((disp_byte10M[i + 23 * 2 + 1] & 0x80) != 0) chuizhi_speed = -chuizhi_speed;
fuyang = (double)((disp_byte10M[i + 24 * 2 + 0] << 0) | (disp_byte10M[i + 24 * 2 + 1] << 8)
| (disp_byte10M[i + 25 * 2 + 0] << 16) | ((disp_byte10M[i + 25 * 2 + 1] & 0x7F) << 24)) / 1000000;
if ((disp_byte10M[i + 25 * 2 + 1] & 0x80) != 0) fuyang = -fuyang;
jixian = (double)((disp_byte10M[i + 26 * 2 + 0] << 0) | (disp_byte10M[i + 26 * 2 + 1] << 8)
| (disp_byte10M[i + 27 * 2 + 0] << 16) | (disp_byte10M[i + 27 * 2 + 1] << 24)) / 1000000;
fangwei = (double)((disp_byte10M[i + 28 * 2 + 0] << 0) | (disp_byte10M[i + 28 * 2 + 1] << 8)
| (disp_byte10M[i + 29 * 2 + 0] << 16) | (disp_byte10M[i + 29 * 2 + 1] << 24)) / 1000000;
F_decode_ok = true; decode_data_pos = 0;
rx_new_F = true;
total_pack++;
pack_cnt = (UInt16)((disp_byte10M[i + 2 * 2 + 0] << 0) | (disp_byte10M[i + 2 * 2 + 1] << 8));
if ((last_pack_cnt + 1) != pack_cnt) lost_pack++;
last_pack_cnt = pack_cnt;
str_time = "20" + year1.ToString("D2") + "年" + month1.ToString("D2") + "月" + date1.ToString("D2") + "日" +
hour1.ToString("D2") + ":" + minute1.ToString("D2") + ":" + second1.ToString("D2") + "." + usecond1.ToString("D6") + "\r\n";
//str_time_text += str_time;
str_build0.Append(str_time);
if (str_build0.Length > 50000)
str_build0.Remove(0, 100);
if (F_str_build0_disping == false) //没有显示就更新 sb_buf
{
F_str_build0_moving = true;
sb_buf.Clear();
sb_buf.Append(str_build0);
F_str_build0_moving = false;
}
}
break;
}
}
else if ((disp_byte10M[i] == 0xF8) && (disp_byte10M[i + 1] == 0xF0))
{
frame_length = (disp_byte10M[i + 1 * 2 + 0]) | (disp_byte10M[i + 1 * 2 + 1] << 8); //帧长
if ((disp_byte10M[i + frame_length - 2] == 0xAA) && (disp_byte10M[i + frame_length - 1] == 0x55))
{
decode_start_pos = i;
//30 校验和,第0字到第29字累加,取低16位
UInt16 buff_16, mm;
for (mm = 0, buff_16 = 0; mm < 32; mm++)
{
buff_16 += (UInt16)((disp_byte10M[i + mm * 2 + 0]) | (disp_byte10M[i + mm * 2 + 1] << 8));
}
if (buff_16 == (UInt16)((disp_byte10M[i + 32 * 2 + 0]) | (disp_byte10M[i + 32 * 2 + 1] << 8)))
{ //校验和正确后
buff = disp_byte10M[i + 6 * 2 + 0];
year1 = (buff >> 4) * 10 + (buff & 0x000F);//年
buff = disp_byte10M[i + 6 * 2 + 1];
month1 = (buff >> 4) * 10 + (buff & 0x000F);//月
buff = disp_byte10M[i + 7 * 2 + 0];
date1 = (buff >> 4) * 10 + (buff & 0x000F);//日
buff = disp_byte10M[i + 7 * 2 + 1];
hour1 = (buff >> 4) * 10 + (buff & 0x000F);//时
buff = disp_byte10M[i + 8 * 2 + 0];
minute1 = (buff >> 4) * 10 + (buff & 0x000F);//分
buff = disp_byte10M[i + 8 * 2 + 1];
second1 = (buff >> 4) * 10 + (buff & 0x000F);//秒
usecond1 = (disp_byte10M[i + 9 * 2 + 0] + (disp_byte10M[i + 9 * 2 + 1] << 8)) * 25;//微秒
str_time = "20" + year1.ToString("D2") + "年" + month1.ToString("D2") + "月" + date1.ToString("D2") + "日\t" +
hour1.ToString("D2") + ":" + minute1.ToString("D2") + ":" + second1.ToString("D2") + "." + usecond1.ToString("D6") + "\r\n";
if (disp_byte10M[i + 12 * 2 + 0] == 0) ;
else if (disp_byte10M[i + 12 * 2 + 0] == 1) ;
else if (disp_byte10M[i + 12 * 2 + 0] == 2) ;
else if (disp_byte10M[i + 12 * 2 + 0] == 3) ;
if (disp_byte10M[i + 12 * 2 + 1] == 0);
else if (disp_byte10M[i + 12 * 2 + 1] == 1);
else if (disp_byte10M[i + 12 * 2 + 1] == 2) ;
star_all = disp_byte10M[i + 13 * 2 + 0];
star_use = disp_byte10M[i + 13 * 2 + 1];
S_ANT2 = disp_byte10M[i + 14 * 2 + 0];
S_ANT1 = disp_byte10M[i + 14 * 2 + 1];
if (disp_byte10M[i + 15 * 2 + 0] == 0) ;
else if (disp_byte10M[i + 15 * 2 + 0] == 1) ;
else if (disp_byte10M[i + 15 * 2 + 0] == 2) ;
if (disp_byte10M[i + 16 * 2 + 0] == 0) ;
else if (disp_byte10M[i + 16 * 2 + 0] == 1);
if (disp_byte10M[i + 17 * 2 + 0] == 0) ;
else if (disp_byte10M[i + 17 * 2 + 0] == 1) ;
else if (disp_byte10M[i + 17 * 2 + 0] == 2) ;
total_pack++;
pack_cnt = (UInt16)((disp_byte10M[i + 2 * 2 + 0] << 0) | (disp_byte10M[i + 2 * 2 + 1] << 8));
if ((last_pack_cnt + 1) != pack_cnt) lost_pack++;
last_pack_cnt = pack_cnt;
F_decode_ok = true; decode_data_pos = 0;
rx_new_F = true;
}
break;
}
}else if((disp_byte10M[i] == 0x01) && (disp_byte10M[i + 1] == 0xF8))
{
frame_length = (disp_byte10M[i + 1 * 2 + 0]) | (disp_byte10M[i + 1 * 2 + 1] << 8); //帧长
if ((disp_byte10M[i + frame_length - 2] == 0xAA) && (disp_byte10M[i + frame_length - 1] == 0x55))
{
decode_start_pos = i;
//30 校验和,第0字到第17字累加,取低16位
UInt16 buff_16, mm;
for (mm = 0, buff_16 = 0; mm < 18; mm++)
{
buff_16 += (UInt16)((disp_byte10M[i + mm * 2 + 0]) | (disp_byte10M[i + mm * 2 + 1] << 8)); //校验和
}
if (buff_16 == (UInt16)((disp_byte10M[i + 18 * 2 + 0]) | (disp_byte10M[i + 18 * 2 + 1] << 8)))
{
}
}
}
}//检测一下是否有数据头,有帧长,有数据尾,并解析
if (F_decode_ok == true)//解析过数据,进行数据搬移
{
F_decode_ok = false;
Int32 j = 0;
for (Int32 i = (decode_start_pos + frame_length); i < (rx_data_pos); j++, i++)
{
disp_byte10M[j] = disp_byte10M[i];
}
rx_data_pos = j;
decode_data_pos = 0;
}
}
}
else
{
F_rx_buf_cycle_to_head = false;
rx_data_pos = 0;
decode_data_pos = 0;
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "异常问题", MessageBoxButtons.OKCancel);
}
C# 网口接受数据,udp组播,报文解析
最新推荐文章于 2024-09-24 09:22:53 发布